SQL Server - Updating a table data from another table

Asked By a nia on 03-Oct-05 01:36 PM
Hi all,
I have two database exactly the same. One of them is active, meaning data is being added and deleted to it. The other one is only for my  testing. I don't have backup permissions on the main one but I can do select statements. How would you keep updating the data on the second database from the first one?I know that you can do this:
SELECT *  INTO [secondDB.Table1] FROM [firstDB.Table1]
but this statement won't work if you already have the tables built. It only works the first time. Any solutions?
Thanks

INSERT INTO SELECT FROM

Asked By F Cali on 03-Oct-05 01:40 PM
Hi a nia,
To copy the contents of a table from another database to your database where the destination table already exists, you use the INSERT INTO ... SELECT FROM statement as follows:
INSERT INTO SecondDB.dbo.Table1
SELECT * FROM FirstDB.dbo.Table1
Hope this helps.

INSERT INTO SELECT FROM

Asked By a nia on 03-Oct-05 01:45 PM
Yes, that's it. Thanks Ronald.

Truncate Then Insert

Asked By F Cali on 03-Oct-05 01:47 PM
Hi a nia,
To be sure that you won't have too many records in your table, make sure you delete the contents of your destination table first before doing the insert.  If your table is big, the fastest way to clean up the table is to use the TRUNCATE TABLE command instead of the DELETE FROM command because the TRUNCATE command doesn't log the transaction.
Hope this helps.
Truncate Then Insert
Asked By a nia on 03-Oct-05 02:30 PM
Ok Ronald. Thanks for the comment.
what is column list?
Asked By a nia on 04-Oct-05 02:14 PM
Hi Ronald,
When I try using the "insert into select from"....for some of my tables I get this error:
An explicit value for the identity column in table 'tblFees' can only be specified when a column list is used and IDENTITY_INSERT is ON
Any idea how to fix this?
Identity Insert
Asked By F Cali on 04-Oct-05 02:17 PM
Hi a nia,
Since some of your tables have an identity column, you have to set the IDENTITY_INSERT option ON first then after the insert, set it back to OFF.  Here's how you would do it:
SET IDENTITY_INSERT YourTable ON
INSERT INTO YourTable
SELECT * FROM YourOtherTable
SET IDENTITY_INSERT YourTable OFF
You have to do this for each of your table that has an identity column.
Hope this helps.
I still get the same error
Asked By a nia on 04-Oct-05 02:23 PM
No that didn't work. I still get the same error :(
Set for each Table
Asked By F Cali on 04-Oct-05 02:28 PM
You have to set the IDENTITY_INSERT for each table that you are trying to copy.  So in the case of the 'tblFees' table, your script should look like this:
SET IDENTITY_INSERT tblFees ON
INSERT INTO tblFees
SELECT * FROM OtherDB.dbo.tblFees
SET IDENTITY_INSERT tblFees OFF
Set for each Table
Asked By a nia on 04-Oct-05 02:36 PM
Yes, I did exactly the same. But it still gives me that same error!!
problem solved
Asked By a nia on 04-Oct-05 02:43 PM
Oh it was just that I had to list the names of the columns in my table in addition to having the IDENTITY_INSERT ON. Thanks for your help