Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to insert some data from one table to another and want dynamic insert scripts to be generated.
How can I do that.

Please help me...
Posted

That's the job of INSERT INTO SELECT statement.
Syntax:
SQL
INSERT INTO table2
SELECT * FROM table1;

OR
SQL
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

Example:
SQL
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers; 


Reference:
SQL INSERT INTO SELECT Statement[^]

Hope, it helps :)
 
Share this answer
 
Comments
PIEBALDconsult 28-Apr-15 0:51am    
Yes, that should be what he wants.
One way to do this would be to use a cursor.
Try these links for additional information
How to use Cursor in Sql[^]
MSDN[^]
 
Share this answer
 
Comments
Suvendu Shekhar Giri 28-Apr-15 0:26am    
Forgive me if I am wrong, experts always advice not to use cursor unless it is the only way left.
May be because:
1. They use more server resources
2. Can cause leaks if used incorrectly (eg. Open without corresponding Close)
3. Increases load on tempdb
etc.
Abhinav S 28-Apr-15 0:36am    
I agree with you. If your answer is useful to the user, that is obviously the better choice.
However, if the user wants to loop through the rows in a table, cursor can be one of the ways to do this.
PIEBALDconsult 28-Apr-15 0:52am    
Nah, "if the user wants to loop through the rows in a table", he requires guidance back to the true path.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900