Robin clave
Robin clave

Reputation: 638

Insert primary key column value to Non Primary key column

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

Answers (4)

Bridge
Bridge

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

Andrew McLintock
Andrew McLintock

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

Fedor Hajdu
Fedor Hajdu

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

kaiz.net
kaiz.net

Reputation: 1994

As long as the data fits into the target column (type-wise), yes.

Upvotes: 1

Related Questions