将插入查询中的最后一个标识用于另一个查询

时间:2014-05-12 02:55:18

标签: sql-server insert unique identity

我想在sql-server上插入两个表,并在第一个查询中使用@@ identity:

INSERT INTO idcust(username, Email, Password, STATUS,
                   fullname, address, verificationCode, PhoneNo,
                   BOD, KTP, Gender, CreateDate)
VALUES(@UserName, @Email, dbo.fnMD5(@Password), @Status,
       @FullName, @Address, @VerificationCode, @PhoneNo,
       @BOD, @KTP, @Gender, getdate())

SET @IDAcc = @@IDENTITY

INSERT INTO Accounts(AccountNo, Provider, firstid, Status, 
                     FullName, [Address], Email, BOD, KTP,
                     PhoneNo, Gender)
VALUES(@Account, 'provider', @FirstIDAcc, @Status,
       @FullName, @Address, @Email, @bod, @KTP,
       @PhoneNo, @Gender)

SET @UserID = @@IDENTITY

IF @UserID > 0 BEGIN
   EXEC Email_FMCust @VerificationCode, @Email

   SET @Result = 1
   SET @RCode = 101
END
ELSE BEGIN
   SET @RCode = 216                 
END

但不幸的是,两个表中的两个标识值都没有任何值,但是对于第一个查询,数据插入到表中。

桌面设计有问题吗?还是查询?

0 个答案:

没有答案