Wednesday, June 10, 2009

how to insert data from one table to another table in sql server

Every DBA needs to transfer data between tables.
lets we have create a table that has the following structure,

create table tbl_product_Information(
product_name char(50),
price float,
EntryDate datetime

and now we wish to insert one additional row into the table of the product data.

We will hence use the following SQL script:

INSERT INTO tbl_product_Information(product_name, price,Entry Date)
VALUES ('ABC', 900, 'Jan-10-1999')

Above is the very simple insert command.

The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike the previous example, where we insert a single row by

specifying its values for all columns, we now use a SELECT statement to specify the data that we want to insert into the table. If you are thinking

whether this means that you are using information from another table, you are correct. The syntax is as follows:

INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2"

So this is the way you can insert data from one table to another table in sql server.

1 comment: