Method 1 : INSERT INTO SELECT
Use this method if the destination table is already created in the destination database.
USE MyDb
GO
INSERT INTO DestinationTable(FirstName, LastName)
SELECT FirstName, LastName
FROM SourceDatabase.SourceTable
WHERE condition= 1
Method 2 : SELECT INTO
Use this method if the destination table is not already created in the destination database. The SELECT statement would create the new table with same data types in the destination database along with copying the data.
SELECT FirstName, LastName
INTO DestinationTable
FROM SourceDatabase.SourceTable
WHERE condition= 1
In this case new table 'DestinationTable' will be created with same data type as the selected columns.
Monday, August 6, 2012
Insert Multiple Records Using One Insert Statement - SQL Server
How to insert multiple records using one insert statement(using UNION ALL ) instead of going for individual INSERT statements for each row. Here is the sample:
USE YourDbName
GO
INSERT INTO YourTable(FirstColumn, SecondColumn)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
USE YourDbName
GO
INSERT INTO YourTable(FirstColumn, SecondColumn)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
Wednesday, December 8, 2010
Select Top 10% of rows - SQL Server 2008
Execute the following SQL Server T-SQL scripts in SSMS Query Editor to list the top 5 percent result set with ties and without ties:
USE AdventureWorks;
SELECT TOP ( 10 ) PERCENT WITH TIES Employee = FirstName + ' ' + Lastname,
e.Title,
Gender,
Rate
FROM HumanResources.Employee e
JOIN HumanResources.EmployeePayHistory eph
ON e.EmployeeID = eph.EmployeeID
JOIN Person.Contact c
ON c.ContactID = E.ContactID
ORDER BY Rate DESC;
GO
=====================================================================
SELECT TOP ( 10 ) PERCENT Employee = FirstName + ' ' + Lastname,
e.Title,
Gender,
Rate
FROM HumanResources.Employee e
JOIN HumanResources.EmployeePayHistory eph
ON e.EmployeeID = eph.EmployeeID
JOIN Person.Contact c
ON c.ContactID = E.ContactID
ORDER BY Rate DESC;
USE AdventureWorks;
SELECT TOP ( 10 ) PERCENT WITH TIES Employee = FirstName + ' ' + Lastname,
e.Title,
Gender,
Rate
FROM HumanResources.Employee e
JOIN HumanResources.EmployeePayHistory eph
ON e.EmployeeID = eph.EmployeeID
JOIN Person.Contact c
ON c.ContactID = E.ContactID
ORDER BY Rate DESC;
GO
=====================================================================
SELECT TOP ( 10 ) PERCENT Employee = FirstName + ' ' + Lastname,
e.Title,
Gender,
Rate
FROM HumanResources.Employee e
JOIN HumanResources.EmployeePayHistory eph
ON e.EmployeeID = eph.EmployeeID
JOIN Person.Contact c
ON c.ContactID = E.ContactID
ORDER BY Rate DESC;
Tuesday, November 30, 2010
PrintDialog in C#
A PrintDialog control is used to open the Windows Print Dialog and let user select the printer, set printer and paper properties and print a file. A typical Open File Dialog where you select a printer from available printers, set printer properties, set print range, number of pages and copies and so on. Clicking on OK button sends the document to the printer.
Creating a PrintDialog
We can create a PrintDialog at design-time as well as at run-time
Design-time
To create a PrintDialog control at design-time, you simply drag and drop a PrintDialog control from Toolbox to a Form in Visual Studio.
Run-time
Creating a PrintDialog control at run-time is simple. First step is to create an instance of PrintDialog class and then call the ShowDialog method. The following code snippet creates a PrintDialog control.
PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.ShowDialog();
Printing Documents
PrintDocument object represents a document to be printed. Once a PrintDocument is created, we can set the Document property of PrintDialog as this document. After that we can also set other properties. The following code snippet creates a PrintDialog and sends some text to a printer.
private void PrintButton_Click(object sender, EventArgs e)
{
PrintDialog printDlg = new PrintDialog();
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "Print Document";
printDlg.Document = printDoc;
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
//Call ShowDialog
if (printDlg.ShowDialog() == DialogResult.OK)
printDoc.Print();
}
Creating a PrintDialog
We can create a PrintDialog at design-time as well as at run-time
Design-time
To create a PrintDialog control at design-time, you simply drag and drop a PrintDialog control from Toolbox to a Form in Visual Studio.
Run-time
Creating a PrintDialog control at run-time is simple. First step is to create an instance of PrintDialog class and then call the ShowDialog method. The following code snippet creates a PrintDialog control.
PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.ShowDialog();
Printing Documents
PrintDocument object represents a document to be printed. Once a PrintDocument is created, we can set the Document property of PrintDialog as this document. After that we can also set other properties. The following code snippet creates a PrintDialog and sends some text to a printer.
private void PrintButton_Click(object sender, EventArgs e)
{
PrintDialog printDlg = new PrintDialog();
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "Print Document";
printDlg.Document = printDoc;
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
//Call ShowDialog
if (printDlg.ShowDialog() == DialogResult.OK)
printDoc.Print();
}
Wednesday, October 27, 2010
Use of Progress Bar control in C#
A Progress Bar control is used to display the progress of some activity.
The project is developed using C#, Windows Forms, and Visual Studio 2005.
As you can see below, we can add a timer control to the form and on the timer tick event handler, we can increment the value of the progress bar.
namespace Progress_Bar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Call the function of tick event of the timer:_...
// Set the timer Intervel at 60:_
// Set the timer enabled "True" by the timer properties:_
private void timer1_Tick(object sender, EventArgs e)
{
fn_prbar_();
}
//Create the function for progress bar:_
public void fn_prbar_()
{
progressBar1.Increment(1);
label1.Text = "Connecting to server_ " + progressBar1.Value.ToString() + "%";
if (progressBar1.Value == progressBar1.Maximum)
{
timer1.Stop();
MessageBox.Show("Server has been connected");
this.Close();
timer1.Stop();
}
}
}
}
The project is developed using C#, Windows Forms, and Visual Studio 2005.
As you can see below, we can add a timer control to the form and on the timer tick event handler, we can increment the value of the progress bar.
namespace Progress_Bar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Call the function of tick event of the timer:_...
// Set the timer Intervel at 60:_
// Set the timer enabled "True" by the timer properties:_
private void timer1_Tick(object sender, EventArgs e)
{
fn_prbar_();
}
//Create the function for progress bar:_
public void fn_prbar_()
{
progressBar1.Increment(1);
label1.Text = "Connecting to server_ " + progressBar1.Value.ToString() + "%";
if (progressBar1.Value == progressBar1.Maximum)
{
timer1.Stop();
MessageBox.Show("Server has been connected");
this.Close();
timer1.Stop();
}
}
}
}
Working with the Generic Dictionary Class In C#
This Example Demonstrates the use Of a Generic Dictionary Class Which is an Improved HashTable Class and performs much better than a HashTable.
Example :
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Dictionary< int, string > students = new Dictionary< int, string >();
students.Add(1, "Hefin");
students.Add(2, "Abbas");
students.Add(3, "Rinso");
students.Add(4, "Sachin");
Dictionary< string, string > Employees = new Dictionary< string, string >();
Employees.Add("E01", "Hefin Dsouza");
Employees.Add("E02", "Abbas Electriwala");
Employees.Add("E03", "Rinso Joseph");
Employees.Add("E04", "Sachin Gaikar");
Console.WriteLine("List of all Students");
foreach (KeyValuePair< int,string > n in students)
{
Console.WriteLine("|Roll Number : " + n.Key + "t| Name : " + n.Value);
}
Console.WriteLine("List of all Employees");
foreach (KeyValuePair< string, string > n in Employees)
{
Console.WriteLine("|EMP ID : " + n.Key + "tt| EMP Name : " + n.Value);
}
}
}
This is an Improved HashTable which restricts what data type will the Key be and What Data Type the value should be.So it makes this class much Type Safe.
Example :
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Dictionary< int, string > students = new Dictionary< int, string >();
students.Add(1, "Hefin");
students.Add(2, "Abbas");
students.Add(3, "Rinso");
students.Add(4, "Sachin");
Dictionary< string, string > Employees = new Dictionary< string, string >();
Employees.Add("E01", "Hefin Dsouza");
Employees.Add("E02", "Abbas Electriwala");
Employees.Add("E03", "Rinso Joseph");
Employees.Add("E04", "Sachin Gaikar");
Console.WriteLine("List of all Students");
foreach (KeyValuePair< int,string > n in students)
{
Console.WriteLine("|Roll Number : " + n.Key + "t| Name : " + n.Value);
}
Console.WriteLine("List of all Employees");
foreach (KeyValuePair< string, string > n in Employees)
{
Console.WriteLine("|EMP ID : " + n.Key + "tt| EMP Name : " + n.Value);
}
}
}
This is an Improved HashTable which restricts what data type will the Key be and What Data Type the value should be.So it makes this class much Type Safe.
Get number of rows in file in C#
This small one line of code will give you total number of lines or rows in a normal text file in c#.
Just save result of this in normal int variable and you ready to go..
int lines = System.IO.File.ReadAllLines("path").Length
Hope this helps in File read operations
Just save result of this in normal int variable and you ready to go..
int lines = System.IO.File.ReadAllLines("path").Length
Hope this helps in File read operations
Subscribe to:
Posts (Atom)