Reputation: 638
Im trying to insert the primary key column value from table A to non primary nullable column column in table B. Is it possible.
Thanks
Upvotes: 0
Views: 361
Reputation: 30651
It is as simple as you think it should be, as long as the column types match. I am presuming you mean your PK only covers one column as well.
INSERT INTO TableB (destinationColumn)
SELECT pkcolumn
FROM TableA
Upvotes: 0
Reputation: 142
You shouldnt have any problem so long as you havent put any contrary contraints in your target tables ID column. You need to add column names into your code above though if the table already exists.
Mac
Upvotes: 0
Reputation: 4697
Of course it's possible to insert one tables value to another. From your question it's not really clear what you're trying to accomplish but to insert from one table to another (no matter if they are keys or not) you can do:
INSERT INTO Table (Column)
SELECT Id FROM AnotherTable;
Since you're mentioning primary keys maybe you're trying to read the auto increment ID you just inserted if so you can do it by using SCOPE_IDENTITY()
INSERT INTO Table (Column)
SELECT SCOPE_IDENTITY()
Upvotes: 1
Reputation: 1994
As long as the data fits into the target column (type-wise), yes.
Upvotes: 1