如果用户名和密码在users表中匹配,我想生成访问令牌。但是,如何将以前返回的值用于下一个插入查询?我用###标记了缺失值我不知道###。
CREATE PROCEDURE [dbo].[GetAccessToken]
@userId varchar(50),
@password varchar(50)
AS
SELECT Id
FROM Users
WHERE UserId = @userId AND Password = @password
IF @@ROWCOUNT != 0
BEGIN
SET @token = NEWID();
INSERT INTO AccessTokens (Token, UserId)
VALUES(CONVERT(varchar(255), @token), ###I Don't Know###)
END
RETURN 0
END
请帮帮我。提前谢谢。
答案 0 :(得分:2)
只需将@userId
作为INSERT
INSERT INTO AccessTokens (Token, UserId)
VALUES(CONVERT(varchar(255), @token), @userId)
答案 1 :(得分:1)
试试这个:
CREATE PROCEDURE [dbo].[GetAccessToken]
@userId varchar(50),
@password varchar(50)
AS
DECLARE @ID INT
SELECT @ID = Id
FROM Users
WHERE UserId = @userId AND Password = @password
IF (@ID IS NOT NULL )
BEGIN
SET @token = NEWID();
INSERT INTO AccessTokens (Token, UserId)
VALUES(CONVERT(varchar(255), @token), @ID)
RETURN 0
END
答案 2 :(得分:1)
使用选项INSERT .. SELECT .. pattern
CREATE PROCEDURE [dbo].[GetAccessToken]
@userId varchar(50),
@password varchar(50)
AS
BEGIN
INSERT INTO AccessTokens (Token, UserId)
SELECT CONVERT(varchar(255), NEWID()), Id
FROM Users
WHERE UserId = @userId AND Password = @password
END