Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy a Table's data from a Stored Procedure
    primarykey
    data
    text
    <p>I am learning how to use SQL and Stored Procedures. I know the syntax is incorrect:</p> <p>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.</p> <p>I have 2 Databases (Master_db and Master_copy) and the same table structure on each DB.</p> <p>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:</p> <pre><code>USE Master_DB CREATE PROCEDURE TransferData DEFINE @tableFrom, @tableTo, @databaseTo; INSERT INTO @databaseTo.dbo.@databaseTo SELECT * FROM Master_DB.dbo.@tableFrom GO; </code></pre> <hr> <p>The closest I could get was this. The problem now is that I have a Timestamp column and that rendered an error:</p> <p>"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." </p> <pre><code>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 </code></pre> <p>How can I go around this error with this SP?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload