views:

67

answers:

5

I am learning how to use SQL and Stored Procedures. I know the syntax is incorrect:

Copy data from one table into another table on another Database with a Stored Procedure. The problem is I don't know what table or what database to copy to. I want it to use parameters and not specify the columns specifically.

I have 2 Databases (Master_db and Master_copy) and the same table structure on each DB.

I want to quickly select a table in Master_db and copy that table's data into Master_copy table with same name. I have come up with something like this:

USE Master_DB
CREATE PROCEDURE TransferData
DEFINE @tableFrom, @tableTo, @databaseTo;

INSERT INTO @databaseTo.dbo.@databaseTo
SELECT * FROM Master_DB.dbo.@tableFrom
GO;
+1  A: 

My suggestion is to write function which return table than use insert select statement

Differance between procedure and fucntion than you cannot write select proceudre but you can write select function

Pranay Rana
Ok, But that doesn't help me alot more since I have SQL language problems and syntax is very wrong i think. And I don't know if this is even possible?
Niike2
+2  A: 

Object names in SQL queries cannot be parameterised in the way you are attempting.

To get this to work, you need to use dynamic SQL:

USE Master_DB
CREATE PROCEDURE TransferData
 @tableFrom   SYSNAME
 ,@tableTo    SYSNAME
 ,@databaseTo SYSNAME
AS
DECLARE @sql NVARCHAR(MAX)
SET @sql = 'INSERT INTO ' + @databaseTo+ '.dbo.' + @tableTo + '
            SELECT * FROM Master_DB.dbo.' + @tableFrom

EXEC sp_executesql @sql
GO;
Ed Harper
"Dynamic SQL" was new to me, and thank you for pointing me in the correct direction. There was one mistake i believe in your query. But your answer was correct! wrong (@databaseTo+ '.dbo.' + @databaseTo)
Niike2
@Niike2 - corrected. (I copied the error in from your original post)
Ed Harper
I recommend using the QuoteName function around the object names whenever doing something like this.
CodeByMoonlight
@CodeByMoonligth - good point
Ed Harper
+1  A: 

Only dynamic SQL allows variables as table names. For examp0le:

create procedure dbo.TransferData(
    @src varchar(50),
    @dest varchar(50))
as
declare @query varchar(500)
set @query = 'INSERT INTO ' + @src +
    'SELECT * FROM ' + @dest
exec @query
go
Andomar
+1  A: 

I would agree with @EdHarper that building a string and then running sp_executesql is probably your best answer.

Two things to bear in mind that I can't see in the previous answers:

1) You may have already copied the data into the Master_copy database tables previously, so you probably need to delete the data in the target table before you start your copy operation.

2) If you have any identity columns in the Master_copy tables (because you used the same Sql script to generate the Master_db and Master_copy tables for example) then you need to ensure that you can overwrite identity values (this can be done by the set identity_insert on command).

So as an alternative (there is no point to another repeated answer) and as you know the database names you could simply denormalize this approach and write a simple stored procedure for each table.

Say you have a table called 'person' you could write:

create procedure TransferDataPerson 
as

delete from Master_copy..Person

set identity_insert Master_copy..Person on

insert into Master_copy..Person
select * from Master_db..Person

... and so on for each table.

amelvin
I have taken your points into consideration and added them to the query. Thank you, very good answer! The problem now is added to an answer on this page.
Niike2
A: 

The closest I could get was this. The problem now is that I have a Timestamp column and that rendered an error:

"Cannot insert an explicit value into a timestamp column. Use INSERT with a column list to exclude the timestamp column, or insert a DEFAULT into the timestamp column."

USE Kenneth_Integration
GO
CREATE PROCEDURE TransferData
  @table  SYSNAME
  ,@databaseTo SYSNAME
AS
DECLARE @sql NVARCHAR(MAX)

SET @sql = 'DELETE FROM ' + @databaseTo + '..' + @table + ' set identity_insert ' + @databaseTo + '.dbo.' + @table + 
    ' on INSERT INTO ' + @databaseTo + '.dbo.' + @table + ' SELECT * FROM Kenneth_Integration.dbo.' + @table

EXEC sp_executesql @sql
GO

How can I go around this error with this SP?

Niike2