我是SQL的新手。我正在创建一个虚拟项目。在我的项目中,我有一个注册页面,我想创建一个UserName(FirstName + LastName + UserID)。但问题是,我无法捕获UserID我使用了After Trigger和替代Trigeer。以下是我的脚本 -
表 -
CREATE TABLE UserInfo
(
UserID INT IDENTITY(1,1),
FirstName NVARCHAR(500),
LastName NVARCHAR(500),
[Password] NVARCHAR(200),
EmailID NVARCHAR(200),
[Address] NVARCHAR(500),
CountryID INT,
StateID INT,
UserName NVARCHAR(500)
)
插入数据的过程 -
CREATE PROC Create_User
@FirstName NVARCHAR(500),
@LastName NVARCHAR(500),
@Password NVARCHAR(200),
@EmailID NVARCHAR(200),
@Address NVARCHAR(500),
@CountryID INT,
@StateID INT,
@UserID INT OUTPUT
AS
BEGIN
INSERT INTO UserInfo (FirstName,LastName,[Password],EmailID,[Address],CountryID,StateID)
VALUES
(@FirstName,@LastName,@Password,@EmailID,@Address,@CountryID,@StateID)
SET @UserID = SCOPE_IDENTITY();
END
触发后 -
CREATE TRIGGER Create_UserName
ON UserInfo
AFTER INSERT
AS
BEGIN
DECLARE @_UName VARCHAR(200);
SET @_UName = (SELECT FirstName+LastName+Cast(UserID AS NVARCHAR(100)) from INSERTED )
INSERT INTO UserInfo (UserName) VALUES (@_UName)
END
GO
输出 - 插入两行
取代触发器 -
CREATE TRIGGER Create_UserName
ON UserInfo
INSTEAD OF INSERT
AS
BEGIN
DECLARE @UName NVARCHAR(200);
DECLARE @FName NVARCHAR(200);
DECLARE @LName NVARCHAR(200);
DECLARE @UPassword NVARCHAR(200);
DECLARE @UEmailID NVARCHAR(200);
DECLARE @UAddress NVARCHAR(200);
DECLARE @UCountryID INT;
DECLARE @UStateID INT;
SET @UName = (SELECT FirstName+LastName+Cast(UserID AS NVARCHAR(100)) from INSERTED )
SET @FName = (SELECT FirstName from INSERTED )
SET @LName = (SELECT LastName from INSERTED )
SET @UPassword = (SELECT [Password] from INSERTED )
SET @UEmailID = (SELECT EmailID from INSERTED )
SET @UAddress = (SELECT [Address] from INSERTED )
SET @UCountryID = (SELECT CountryID from INSERTED )
SET @UStateID = (SELECT StateID from INSERTED )
INSERT INTO UserInfo (FirstName,LastName,[Password],EmailID,[Address],CountryID,StateID,UserName)
VALUES (@FName,@LName,@UPassword,@UEmailID,@UAddress,@UCountryID,@UStateID,@UName)
END
GO
输出 - 这样可以正常工作,但身份是0。
请告诉我怎么做?
提前致谢。
答案 0 :(得分:5)
你有问题
至于原因:
存储过程没有INSERT来捕获IDENTITY值:INSERT的范围实际上是而不是触发器。如果你切换到@@ IDENTITY(不良练习),你将从AFTER触发器获得IDENTITY值。
怎么做:
删除两个触发器:它们不添加任何值
如果无法更改UserName,请将计算列添加到表中
例如
ALTER TABLE UserInfo ADD UserName AS FirstName+LastName+Cast(UserID AS NVARCHAR(100)
例如
CREATE PROC Create_User
...
AS
SET NOCOUNT, XACT_ABORT ON
BEGIN TRY
BEGIN TRAN
INSERT INTO UserInfo
(FirstName,LastName,[Password],EmailID,[Address],CountryID,StateID)
VALUES
(@FirstName,@LastName,@Password,@EmailID,@Address,@CountryID,@StateID)
SET @UserID = SCOPE_IDENTITY();
UPDATE UserInfo
SET UserName = @FirstName+@LastName+Cast@UserID AS NVARCHAR(100)
WHERE UserID = @UserID
COMMIT TRAN
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0 ROLLBACK TRAN
END CATCH
GO
答案 1 :(得分:1)
尝试此触发器:
CREATE TRIGGER Create_UserName
ON UserInfo
AFTER INSERT
AS
BEGIN
--DECLARE @_UName VARCHAR(500), @user_id INT
--SELECT @_UName = FirstName + LastName + Cast(UserID AS NVARCHAR(100)), @user_id = UserId FROM INSERTED
UPDATE UserInfo
SET UserName = i.FirstName + i.LastName + Cast(i.UserID AS NVARCHAR(100))
FROM UserInfo u
INNER JOIN Inserted I ON (i.UserId = u.UserId)
--WHERE UserId = @user_id
END
答案 2 :(得分:0)
如果您有SQL-Server 2005+,则可以使用insertstatement的output子句。
CREATE PROC Create_User
@FirstName NVARCHAR(500),
@LastName NVARCHAR(500),
@Password NVARCHAR(200),
@EmailID NVARCHAR(200),
@Address NVARCHAR(500),
@CountryID INT,
@StateID INT,
@UserID INT OUTPUT
AS
BEGIN
DECLARE @OV Table (id int);
INSERT INTO UserInfo (FirstName,LastName,[Password],EmailID,[Address],CountryID,StateID)
inserted.id into @OV
VALUES (@FirstName,@LastName,@Password,@EmailID,@Address,@CountryID,@StateID)
SELECT @UserID = id FROM @OV;
update UserInfo set UserName = FirstName + LastName + Cast(UserID AS NVARCHAR) where UserID = @userid
END
编辑:添加了丢失的关键字,更新了UserName列