我有一个名为Students的表,它有StudentID,它是主键,另一个表是StdAccounts,它也有StudentID。
当我注册某人时,我希望程序将StudentID(来自学生)复制到StudentID(StdAccounts) 我在互联网上搜索了我的问题。找到了一些东西并试图改变。这就是我做了什么,不起作用
INSERT INTO (StdAccounts(StudentID), StudentPassword)
select s.StudentID FROM Students s
WHERE NOT EXISTS (SELECT StudentID FROM StdAccounts std
WHERE s.StudentID = std.StudentID) VALUES (@Password)
答案 0 :(得分:0)
将@Password放入选择列表中:
INSERT INTO StdAccounts (StudentID, StudentPassword)
select s.StudentID, @Password FROM Students s
WHERE NOT EXISTS (SELECT StudentID FROM StdAccounts std
WHERE s.StudentID = std.StudentID)
答案 1 :(得分:0)
嗯,也许:
INSERT INTO
StdAccounts
(
StudentID
,StudentPassword
)
select top 1
StudentID,
@Password
FROM Students
WHERE
Password = @Password
虽然你真的不应该在一个select语句中这样做。
我的意思是你可以看到迫使你使用'前1'的明显问题显然不适合称为'密码'的变量
答案 2 :(得分:0)
INSERT INTO StdAccounts(StudentID, StudentPassword)
select StudentID ,@Password FROM Students
WHERE StudentID not in (SELECT StudentID FROM StdAccounts std
inner join Students s
on s.StudentID = std.StudentID
)