In our db there is a table that has a little over 80 columns. It has a primary key and Identity insert is turned on. I'm looking for a way to insert into this table every column EXCEPT the primary key column from an identical table in a different DB.
Is this possible?
Why not just create a VIEW of the original data, removing the unwanted fields? Then 'Select * into' your hearts desire.
You could query Information_Schema to get a list of all the columns and programatically generate the column names for your query. If you're doing this all in t-sql it would be cumbersome, but it could be done. If you're using some other client language, like C# to do the operation, it would be a little less cumbersome.
In answer to a related question (SELECT * EXCEPT), I point out the truly relational language Tutorial D allows projection to be expressed in terms of the attributes to be removed instead of the ones to be kept e.g.
However its
INSERT
syntax requires tuple value constructors to include attribute name / value pairs e.g.Of course, using this syntax there is no column ordering (because it is truly relational!) e.g. this is semantically equivalent:
The alternative would be to rely fully on attribute ordering, which SQL does partially e.g. this is a close SQL equivalent to the the above:
Once the commalist of columns has been specified the
VALUES
row constructors have the maintain this order, which is not ideal. But at least the order is specified: your proposal would rely on some default order which may be possibly non-deterministic.You can do this quite easily actually:
Provided you have Identity Insert On in "YourOtherBigTable" and columns are absolutely identical you will be okay.
No, that's not possible. You could be tempted to use
But that would not work, because your identity column would be included in the *.
You could use
first you enable inserting identity values, than you copy the records, then you enable the identity column again.
But this won't work neither. SQL server won't accept the * in this case. You have to explicitly include the Id in the script, like :
So we're back from where we started.
The easiest way is to right click the table in Management Studio, let it generate the INSERT and SELECT scripts, and edit them a little to let them work together.
This works in SQL2012