我正面临这个问题,尽管我也设置了@output变量,但我的存储过程始终返回NULL。我想从表中获取最后插入的作用域ID。
有人在我错了的地方帮我吗?
ALTER PROC spAddOrUpdateMember
@pMemberId INT = 0 ,
@pFirstName VARCHAR(50) = 'aa',
@pLastName VARCHAR(50)='aa' ,
@pMemberCode VARCHAR(15) = '12312',
@pDOB DATE = '03/10/2019',
@pGrade INT = 2 ,
@pCNIC VARCHAR(14) = '3423434',
@pFatherName VARCHAR(50) = 'asdasd' ,
@pCurrentAddress VARCHAR(MAX) = 'asds' ,
@pPermanentAddress VARCHAR(MAX) = 'fgdf',
@pEmploymentAddress VARCHAR(MAX) = 'ytuyu' ,
@pNationality INT =2
@output int = 0 output
AS
BEGIN
IF @pMemberId > 0
BEGIN
---UPDATE ME
UPDATE [dbo].[QC_Member_Profile]
SET
[FirstName] = @pFirstName
,[LastName] = @pLastName
,[DOB] = @pDOB
,[CNIC] = @pCNIC
,[FatherName] = @pFatherName
,[CurrentAddress] = @pCurrentAddress
,[PermanentAddress] = @pPermanentAddress
,[Nationality] = @pNationality
,[MemberTypeId] =@pMemberTypeId
WHERE MemberId = @pMemberId
END
ELSE
BEGIN
---INSERT ME
INSERT INTO QC_Member_Profile VALUES(
dbo.PIdentityKey(0),
@pFirstName,
@pLastName,
@pDOB,
@pCNIC,
@pFatherName,
@pCurrentAddress,
@pPermanentAddress,
@pNationality,
)
set @output = SCOPE_IDENTITY();
SELECT @output = SCOPE_IDENTITY();
select @output
END
END
答案 0 :(得分:3)
我猜到了您的ID列的名称,但是应该可以。如果ID列的名称不为MemberID
或不具有数据类型int
,则需要修改ID列的名称:
ALTER PROC spAddOrUpdateMember @pMemberId int = 0,
@pFirstName varchar(50) = 'aa',
@pLastName varchar(50) = 'aa',
@pMemberCode varchar(15) = '12312',
@pDOB date = '03/10/2019',
@pGrade int = 2,
@pCNIC varchar(14) = '3423434',
@pFatherName varchar(50) = 'asdasd',
@pCurrentAddress varchar(MAX) = 'asds',
@pPermanentAddress varchar(MAX) = 'fgdf',
@pEmploymentAddress varchar(MAX) = 'ytuyu',
@pNationality int = 2,
@output int = 0 OUTPUT
AS
BEGIN
IF @pMemberId > 0
BEGIN
UPDATE [dbo].[QC_Member_Profile]
SET [FirstName] = @pFirstName,
[LastName] = @pLastName,
[DOB] = @pDOB,
[CNIC] = @pCNIC,
[FatherName] = @pFatherName,
[CurrentAddress] = @pCurrentAddress,
[PermanentAddress] = @pPermanentAddress,
[Nationality] = @pNationality,
[MemberTypeId] = @pMemberTypeId
WHERE MemberId = @pMemberId;
END;
ELSE
BEGIN
DECLARE @ins table (OutputID int);
INSERT INTO QC_Member_Profile
OUTPUT Inserted.MemberID --guessed name
INTO @Ins
VALUES (dbo.PIdentityKey(0), @pFirstName, @pLastName, @pDOB, @pCNIC, @pFatherName, @pCurrentAddress, @pPermanentAddress, @pNationality);
SELECT @output = OutputID
FROM @ins;
SELECT @Output;
END;
END;
我还修复了您原始问题中的语法错误。