Kapil
Kapil

Reputation: 1931

I need to loop through table and insert new record in another table

I need to loop through table Organisation and insert new record in User table

Select Code,Organisationid from organisation 

INSERT INTO User(userlogin,Organisationid,emailaddress,username,userpassword)
VALUES('AGT'+ Code, organisationid,'[email protected]','User'+ Code,'123')

I need to dynamically pass the Organisationid and Code to the User table to loop through and insert new record in user.

Upvotes: 0

Views: 159

Answers (1)

Giorgos Betsos
Giorgos Betsos

Reputation: 72165

Just use INSERT INTO ... SELECT:

INSERT INTO User (userlogin, Organisationid, emailaddress, username, userpassword)
SELECT 'AGT' + Code, organisationid, '[email protected]', 'User' + Code, '123'
FROM organisation;

Upvotes: 3

Related Questions