有没有办法做这种子查询:
38
我知道如何:DELETE (INSERT (SELECT (INSERT)))
:
INSERT (SELECT)
我的问题是:
用户在验证其帐户时出现在请求表中,系统必须:
答案 0 :(得分:3)
您可以执行INSERT INTO .... SELECT ...
的原因是SELECT
被用作INSERT
查询的输入。
但是,INSERT
查询不会以这种方式返回任何内容。你只需编写3个不同的查询就好了:
--Create user in user table
INSERT INTO UserTable VALUES (...)
--Create user role in role table
INSERT INTO UserRoles VALUES (...)
--Delete user from request table
DELETE FROM Requests WHERE ...
您甚至可以在事务中包装所有查询以确保所有查询都运行:
BEGIN TRAN
--Create user in user table
INSERT INTO UserTable VALUES (...)
--Create user role in role table
INSERT INTO UserRoles VALUES (...)
--Delete user from request table
DELETE FROM Requests WHERE ...
COMMIT
我怀疑您还想使用您已创建的用户的ID。为此,假设您的UserTable
列有IDENTITY
列,则可以使用SCOPE_IDENTITY
功能:
BEGIN TRAN
--Create user in user table
INSERT INTO UserTable VALUES (...)
DECLARE @UserID INT
SET @UserID = SCOPE_IDENTITY()
--Create user role in role table
INSERT INTO UserRoles (UserID, RoleID) VALUES (@UserID, ...)
--Delete user from request table
DELETE FROM Requests WHERE ...
COMMIT