我正在使用 sql server 2008,我在其中编写了一个存储过程,它在表中插入一些值,然后返回这些插入的值。我用nvarchar
作主键。因此,为了选择最后插入的值,我正在使用scopeidentity()
函数,但问题是它正在插入值但没有返回最后插入的值。代码如下:
CREATE PROCEDURE cms_AddOwnerSession
@sessionId nvarchar(max),
@ownerId int,
@ownerName nvarchar(50),
@username nvarchar(30),
@password nvarchar(30)
AS
BEGIN
IF(@sessionId <> NULL)
BEGIN
INSERT INTO OwnerSession
(SessionId,
OwnerId,
OwnerName,
Username,
[Password])
VALUES
(@sessionId,
@ownerId,
@ownerName,
@username,
@password)
SET @sessionId = SCOPE_IDENTITY()
END
SELECT
ISNULL (SessionId,'NULL') 'sessionId',
ISNULL (OwnerId,0) 'ownerId',
ISNULL (OwnerName,'') 'ownerName',
ISNULL (Username,'') 'username',
ISNULL ([Password],'') 'password'
FROM OwnerSession
WHERE SessionId = @sessionId
END
GO