Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i want to copy a table to another database table using
SQL
Select * into DestinationDB.dbo.tableName from SourceDB.dbo.SourceTable
statement in SQL according to this article: Copy Table Schema and Data From One Database to Another Database in SQL Server
The thing is, i want to use a parameter for a table name instead of giving it from the statement like this:
SQL
SELECT * INTO DestinationDatabase.dbo."@copiedTableName" FROM SourceDatabase.dbo."@copiedTableName"

But apparently i can't use a parameterized table name for SourceDatabase. Any other ways to do it with table name parameter? Thanks in advance.
Posted

Have a look at MSSQL's procedure sp_executesql.

sp_executesql allows you create a dynamic string containing the SQL statement to run, so you should be able to incorporate you dynamic table name into the SQL statement.

Hope it helps.
 
Share this answer
 
i dont know about table copy,

but you can read data from first one and insert the read data to the second table.


read one{
put data in an array.
}


insert data to the second table
use for loop.
 
Share this answer
 
Comments
Herman<T>.Instance 8-Sep-14 7:23am    
slow in performance when copying large tables

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