Insert Data In To Two Tables In Single Insert Statement
Below is the simple
code to insert data in to two tables in single Insert statement
CREATE TABLE #registration (RegistrationId INT IDENTITY(1,1), FirstName NVARCHAR(50), LastName NVARCHAR(50))
CREATE TABLE #logindetails (LoginId INT IDENTITY(100,1), RegistrationId INT, UserName NVARCHAR(50), Pwd NVARCHAR(50))
Here
RegistrationId is related in both tables.
Inserting two
records in to both tables
INSERT INTO #registration(FirstName, LastName)
OUTPUT INSERTED.RegistrationId, 'admin', 'admin@123' INTO #logindetails
SELECT 'Jawala', 'Parsad'
INSERT INTO #registration(FirstName, LastName)
OUTPUT INSERTED.RegistrationId, 'mylogin', '123@testone' INTO #logindetails
SELECT 'Happy', 'Singh'
SELECT * FROM #registration
SELECT * FROM #logindetails
DROP TABLE #registration,
#logindetails
After
selecting data from both tables you will fin that RegistrationId is same in
both the tables. This solves the problem of selecting RegistrationId from
#registration table and the inserting into #logindetails for maintaining relationship among both tables.
Copy the above code and execute..!
Copy the above code and execute..!
Comments
Post a Comment