Thomas Stockbruegger
asked on
Need some help with copy data from one table to another
SQL Server 2000
First I did this:
CString str_CustomerNo_from="1000"
CString str_CustomerNo_to ="4000"
sql.Format("SELECT * INTO Sicherung_Thomas.dbo.Custo mer FROM OMSDaten.dbo.Customer WHERE CustomerNo>='%s' AND CustomerNo<='%s'ORDER BY CustomerNo ASC",str_CustomerNo_from, str_CustomerNo_to);
Then I deleted the data that I saved before:(in OMSDaten database)
sql.Format("DELETE Customer FROM Customer WHERE CustomerNo>='%s' AND CustomerNo<='%s'",str_Cust omerNo_fro m,str_Cust omerNo_to) ;
Now my question: How can I copy the data back from Sicherung_Thomas.dbo.Custo mer to the existing table OMSDaten.dbo.Customer?
I can not use SELECT * INTO because the table already exist.
Please let me know.
Thank you.
Best regards,
Thomas
First I did this:
CString str_CustomerNo_from="1000"
CString str_CustomerNo_to ="4000"
sql.Format("SELECT * INTO Sicherung_Thomas.dbo.Custo
Then I deleted the data that I saved before:(in OMSDaten database)
sql.Format("DELETE Customer FROM Customer WHERE CustomerNo>='%s' AND CustomerNo<='%s'",str_Cust
Now my question: How can I copy the data back from Sicherung_Thomas.dbo.Custo
I can not use SELECT * INTO because the table already exist.
Please let me know.
Thank you.
Best regards,
Thomas
ASKER
Having said that, perhaps it would also be better not to delete any data at all, but to rather just flag the data as deleted. So create a bit column called isDeleted and if you "Delete" then set this to true. Then in your code, you just show records where isDeleted = false. This way you aren't moving data around and you maintain that original record ID for the Customer. "Undeleting" it is also then easier. Just set the isDeleted flag to false.Hallo Dirk,
thank you for your answer. I would like to do this your way....but I have not enough space in OMSDaten database.
This is the reason I will make a copy of the data in Sicherung_Thomas (if you have to use some of the data it any time in the future).
And if I do need the data anymore I will delete the tables from Sicherung_Thomas. The only reason for Sicherung_Thomas is that the
OMSDatabase is on it limits.
ASKER
You have an Identity column so you need to turn off IDENTITY_INSERT Then perform your insert, then turn on IDENTITY_INSERT.
rough translate from German to English
rough translate from German to English
An explicit value for the identity column in the OMS data can only be specified if a column list is used and INDENTITY_INSERT is set to ON
SET IDENTITY_INSERT dbo.Tool OFF;
GO
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
GO
SET IDENTITY_INSERT dbo.Tool ON;
ASKER
Hi Jeff, thank your for your answer.
I tried this: same error
I tried this: same error
sql.Format("SET IDENTITY_INSERT OMSDaten.dbo.LogBuch OFF INSERT INTO OMSDaten.dbo.LogBuch SELECT * FROM %s SET IDENTITY_INSERT OMSDaten.dbo.LogBuch ON",str_Table);
What are you using to execute the SQL? It looks like you are not doing it from SQL Server Management Studio.
Is that Java or C#.NET or something else?
Is that Java or C#.NET or something else?
ASKER
Visual Studio c++
After reviewing your question history it appears to be C++
I just noticed that you don't have the semi colons after each statement.
Sample link that has a similar solution
https://stackoverflow.com/ questions/ 3373037/ho w-to-put-s et-identit y-insert-d bo-mytable -on-statem ent
I just noticed that you don't have the semi colons after each statement.
sql.Format("SET IDENTITY_INSERT OMSDaten.dbo.LogBuch OFF; INSERT INTO OMSDaten.dbo.LogBuch SELECT * FROM %s; SET IDENTITY_INSERT OMSDaten.dbo.LogBuch ON;",str_Table);
Sample link that has a similar solution
https://stackoverflow.com/
ASKER
Jeff, I did the change with the ;
same result....same error
same result....same error
ASKER
Do I have to use the word GO, in my query?
I don't think GO is required from C++ with semi colons, but I don't know. I don't have VC++ to test myself.
Another possibility to help troubleshoot would be to issue only the SET IDENTITY_INSERT OMSDaten.dbo.LogBuch OFF; command and see if that is successful.
Another possibility to help troubleshoot would be to issue only the SET IDENTITY_INSERT OMSDaten.dbo.LogBuch OFF; command and see if that is successful.
ASKER
Another possibility to help troubleshoot would be to issue only the SET IDENTITY_INSERT OMSDaten.dbo.LogBuch OFF; command and see if that is successful.
sql.Format("SET IDENTITY_INSERT OMSDaten.dbo.LogBuch OFF");
will work without an error message
Ok, maybe it is a c++ specific issue then. I'm not qualified for c++ at the moment. added Visual C++ tag.
ASKER
Visual C++.Net tag is already on
Yes. I just added it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jeff, I did the following:
I have used any column without the IDX = Index This will work !!!
sql.Format("SET IDENTITY_INSERT OMSDaten.dbo.LogBuch OFF; INSERT INTO OMSDaten.dbo.LogBuch SELECT Zeitstempel, Datum_Zeit, Programm, BenutzerName, IP_Adresse FROM %s ; SET IDENTITY_INSERT OMSDaten.dbo.LogBuch ON;",str_Table);
but this is a small table with only a few columns, I would like to the same with big tables that have many many columns, do much work to add them all by name
I have used any column without the IDX = Index This will work !!!
sql.Format("SET IDENTITY_INSERT OMSDaten.dbo.LogBuch OFF; INSERT INTO OMSDaten.dbo.LogBuch SELECT Zeitstempel, Datum_Zeit, Programm, BenutzerName, IP_Adresse FROM %s ; SET IDENTITY_INSERT OMSDaten.dbo.LogBuch ON;",str_Table);
but this is a small table with only a few columns, I would like to the same with big tables that have many many columns, do much work to add them all by name
ASKER
Are you able to create Stored procedures on the SQL database?I do not know how to do this
ASKER
is there not a trick or so.... SELECT * FROM but without IDX....so that I have not to write down every column name in the query?
ASKER
Even if I do not get a complete solution I will close this question and give you my points.
Thank you.
Best regards,
Thomas
Thank you.
Best regards,
Thomas
SQL INSERT INTO SELECT Statement
Open in new window
Having said that, perhaps it would also be better not to delete any data at all, but to rather just flag the data as deleted. So create a bit column called isDeleted and if you "Delete" then set this to true. Then in your code, you just show records where isDeleted = false. This way you aren't moving data around and you maintain that original record ID for the Customer. "Undeleting" it is also then easier. Just set the isDeleted flag to false.