Miguel Moura
Miguel Moura

Reputation: 39364

Create records with other table values

I have two SQL tables, Users and Claims:

Users > Id (PK Identity), Name
Claims > Id (PK Identity), UserId (FK), Name, Value

I need to create a Claim with Name="Plan" and Value="XK" for all Users.

I got the Id of each User since it will be the UserId of a Claim:

SELECT Id
From Users;

Now for each Id I need to insert a Claim as:

Claim.UserId = User.Id
Claim.Name = "Plan"
Claim.Value = "XK"

Upvotes: 0

Views: 45

Answers (2)

Shareef
Shareef

Reputation: 86

Try this:

Insert into Claims (userid, name, value)Select id, 'Plan','XK' from Users

Upvotes: 1

Anthony Horne
Anthony Horne

Reputation: 2522

insert into claims(userid, [name], [value])
select userid, 'plan', 'XK'
from users
left join claims
on users.id = claims.userid
where claims.userid is null

Upvotes: 2

Related Questions