使用 SQL Server 2008 R2,我收到错误:
消息 311,级别 16,状态 1,过程 ad_user,第 28 行
不能在“已插入”和“已删除”表中使用 text、ntext 或 image 列。
触发器的目的是在插入新用户时更新用户组表。我只包含了错误发生位置的 SQL。)。让我感到困惑的是,如果我删除其中一个整数声明,我不会得到同样的错误(只是关于没有声明变量的错误)。
CREATE trigger [dbo].[ad_user] on [dbo].[tps_user]
FOR INSERT
AS
DECLARE @UserGuid uniqueidentifier
DECLARE @EndUserTypeGuid uniqueidentifier
DECLARE @UserTypeGuid uniqueidentifier
DECLARE @saGuid uniqueidentifier
DECLARE @GroupGuid uniqueidentifier
DECLARE @NewUser VarChar(250)
DECLARE @deptnum VarChar(250)
DECLARE @locnum VarChar(250)
DECLARE @CN VarChar(250)
DECLARE @NewOU VarChar(250)
DECLARE @pos1 integer
DECLARE @pos2 integer
BEGIN
SELECT @EndUserTypeGuid=tps_guid FROM tps_user_type WHERE tps_name='EndUser'
SELECT @saGuid = tps_guid FROM tps_user WHERE tps_title = 'SA'
SELECT @UserGuid=tps_guid,
@UserTypeGuid=tps_user_type_guid,
@NewUser=tps_title,
@deptnum=usr_departmentnumber,
@locnum=usr_locationnumber,
@CN=usr_ou
FROM inserted
IF @UserTypeGuid=@EndUserTypeGuid
BEGIN
SELECT @GroupGuid=tps_guid FROM tps_group WHERE usr_departmentnumber=@deptnum
IF @GroupGuid IS NOT NULL
BEGIN
IF @UserGuid NOT IN (SELECT tps_user_id
FROM tps_user_group WHERE tps_group_id = @GroupGuid)
BEGIN
-- Remove the user from other groups
DELETE FROM tps_user_group WHERE tps_user_id = @UserGuid;
-- Create Customer Group Membership from department
INSERT INTO tps_user_group(tps_user_id, tps_group_id, tps_creation_user_guid,
tps_last_update_user_guid, tps_creation_date, tps_last_update)
VALUES(@UserGuid, @GroupGuid, @saGuid, @saGuid, GetDate(), GetDate());
END
END
END
END