I have two identical tables and need to copy rows from table to another. What is the best way to do that? (I need to programmatically copy just a few rows, I don't need to use the bulk copy utility).
If you right-click the table in SSMS then script the table as INSERT, then do the same again and script the tables as SELECT. Paste this SELECT statement at the end of the INSERT replacing the VALUES clause, then just edit that statement a bit to get the current maximum ID and add a ROW_NUM to it.
By holding down the Ctrl key, you can copy a Word table's row or column to create a new row or column. Word tables are great tools for listing and comparing values.
As long as there are no identity columns you can just
INSERT INTO TableNew SELECT * FROM TableOld WHERE [Conditions]
Alternative syntax:
INSERT tbl (Col1, Col2, ..., ColN) SELECT Col1, Col2, ..., ColN FROM Tbl2 WHERE ...
The select query can (of course) include expressions, case statements, constants/literals, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With