Reputation: 18087
I have the SQL below. SQL return a Users.Id
which are not found in Order
table. I need to create Insert SQL and insert default order for these users. How to do that?
SELECT Id
FROM [User] WHERE Id NOT IN (SELECT UserId FROM dbo.[Order])
Order table columns are
Id , -- Random Number
UserId , -- Id from Select above
FirstName ,
LastName ,
Credits
Upvotes: 1
Views: 118
Reputation: 66687
Assuming dbo.[User]
has the FirstName
, LastName
and Credits
columns you can do it like this:
Insert Into dbo.[Order] (UserId, FirstName, LastName, Credits)
SELECT u.Id, u.FirstName, u.LastName, u.Credits
FROM [User] u WHERE u.Id NOT IN (SELECT o.UserId FROM dbo.[Order] o)
You can see here for another example. You might understand it :)
To generate the Unique Id
use the Identity Property.
Upvotes: 2