开发者

how to insert data in table using select statement in sql server

开发者 https://www.devze.com 2023-04-10 06:35 出处:网络
I have two tablesLogin table anduserinfo table. UserInfo table is primary key(UserId) table and login table is foreign key(U开发者_开发知识库serId) table. So my problem is when i insert data in login

I have two tables Login table and userinfo table. UserInfo table is primary key(UserId) table and login table is foreign key(U开发者_开发知识库serId) table. So my problem is when i insert data in login table value of userid column should come from Userinfo table and value of other columns of log in table like username and password should be inserted directly . Is it possible in single insert statement.

i did the following but it didnt work

insert into login(Userid,username,password)
values(select max(userid) from userinfo,sumit,sumit123) 


insert into login(Userid,username,password)
values((select max(userid) from userinfo),'sumit','sumit123');


insert into login (Userid, username, password) 
select max(userid), 'sumit', 'sumit123'
from userinfo

[Please note: while that is syntactically correct, I probably wouldn't do it that way.]


Have you tried using a inner JOIN?

INSERT INTO Insurance (Name) SELECT Employee.Username FROM Employee INNER JOIN Project ON Employee.EmployeeID = Project.EmployeeID WHERE Project.ProjectName = 'Hardwork';

0

精彩评论

暂无评论...
验证码 换一张
取 消