编写复杂的触发器

发布于 2024-08-08 16:21:07 字数 2369 浏览 5 评论 0原文

这篇文章是我昨天发布的更新(带有触发器代码)。

我正在使用 SQL Server 2000。我正在编写一个触发器,当字段 Applicant.AppStatusRowID

表申请人链接到表 Location、表 Company & 时执行该触发器。表应用程序状态。

我的问题是在查询中创建联接。

当 Applicant.AppStatusRowID 更新时,我想从 Applicant.AppStatusRowID、Applicant.FirstName、Applicant.Lastname、Location.LocNumber、Location.LocationName、Company.CompanyCode、AppStatus.DisplayText 获取值

连接将是:

Select * from Applicant A 
Inner Join AppStatus ast on ast.RowID = a.AppStatusRowID 
Inner Join Location l on l.RowID = a.LocationRowID 
Inner Join Company c on c.RowID = l.CompanyRowID 

这是插入审核表(字段为申请人 ID、姓氏、名字、日期、时间、公司、地点编号、地点名称、状态处置、用户)

触发器查询是:

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE TRIGGER tri_UpdateAppDispo ON dbo.Test_App
For Update 
AS

declare @Approwid int
        Declare @triggername sysname
        Set @rowCnt = @@rowcount
        Set @triggername = object_name(@@procid)  

        If @rowCnt = 0
                    RETURN

        If Update(appstatusrowid)
        BEGIN

        -----------------------------------------------------------------------------
                    -- insert a record to the AppDispo table, if AppstatusRowid
                    -- is being Updated
                    -----------------------------------------------------------------------------
                    Insert AppDispo(AppID, LastName, FirstName, [DateTime],Company,Location,LocationName,
                    StatusDispo,[Username])

                                Select d.Rowid,d.LastName, d.FirstName, getDate(),C.CompanyCode,l.locnum,l.locname, ast.Displaytext,
                             SUSER_SNAME()+' '+User  
                                From     deleted d with(nolock) 
                            Inner join Test_App a with (nolock) on a.RowID = d.rowid
                            inner join location l with (nolock) on l.rowid = d.Locationrowid
                                            inner join appstatus ast with (nolock) on ast.rowid = d.appstatusrowid 
                                            inner join company c with (nolock) on c.rowid = l.CompanyRowid
                                            --Inner Join Deleted d ON a.RowID = d.RowID
                                            --Where (a.rowid = @Approwid)
        END
GO

有什么想法吗?

This posting is an update (with trigger code) from my earlier posting yesterday

I am using SQL Server 2000. I am writing a trigger that is executed when a field Applicant.AppStatusRowID

Table Applicant is linked to table Location, table Company & table AppStatus.

My issue is creating the joins in my query.

When Applicant.AppStatusRowID is updated, I want to get the values from Applicant.AppStatusRowID, Applicant.FirstName, Applicant.Lastname, Location.LocNumber, Location.LocationName, Company.CompanyCode, AppStatus.DisplayText

The joins would be :

Select * from Applicant A 
Inner Join AppStatus ast on ast.RowID = a.AppStatusRowID 
Inner Join Location l on l.RowID = a.LocationRowID 
Inner Join Company c on c.RowID = l.CompanyRowID 

This is to be inserted into an Audit table (fields are ApplicantID, LastName, FirstName, Date, Time, Company, Location Number, Location Name, StatusDisposition, User)

The trigger query is:

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE TRIGGER tri_UpdateAppDispo ON dbo.Test_App
For Update 
AS

declare @Approwid int
        Declare @triggername sysname
        Set @rowCnt = @@rowcount
        Set @triggername = object_name(@@procid)  

        If @rowCnt = 0
                    RETURN

        If Update(appstatusrowid)
        BEGIN

        -----------------------------------------------------------------------------
                    -- insert a record to the AppDispo table, if AppstatusRowid
                    -- is being Updated
                    -----------------------------------------------------------------------------
                    Insert AppDispo(AppID, LastName, FirstName, [DateTime],Company,Location,LocationName,
                    StatusDispo,[Username])

                                Select d.Rowid,d.LastName, d.FirstName, getDate(),C.CompanyCode,l.locnum,l.locname, ast.Displaytext,
                             SUSER_SNAME()+' '+User  
                                From     deleted d with(nolock) 
                            Inner join Test_App a with (nolock) on a.RowID = d.rowid
                            inner join location l with (nolock) on l.rowid = d.Locationrowid
                                            inner join appstatus ast with (nolock) on ast.rowid = d.appstatusrowid 
                                            inner join company c with (nolock) on c.rowid = l.CompanyRowid
                                            --Inner Join Deleted d ON a.RowID = d.RowID
                                            --Where (a.rowid = @Approwid)
        END
GO

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2024-08-15 16:21:07

尝试使用此代码

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE TRIGGER tri_UpdateAppDispo ON dbo.Test_App
For Update 
AS

declare @Approwid int
    Declare @triggername sysname
    Set @rowCnt = @@rowcount
    Set @triggername = object_name(@@procid)  

    If @rowCnt = 0
                RETURN

    If Update(appstatusrowid)
    BEGIN
    -----------------------------------------------------------------------------
                -- insert a record to the AppDispo table, if AppstatusRowid
                -- is being Updated
                -----------------------------------------------------------------------------
                Insert AppDispo(AppID, LastName, FirstName, [DateTime],Company,Location,LocationName,
                StatusDispo,[Username])
                            Select d.Rowid,d.LastName, d.FirstName, getDate(),C.CompanyCode,l.locnum,l.locname, ast.Displaytext,
                         SUSER_SNAME()+' '+User  
                            From     deleted d with(nolock),location l with (nolock),appstatus ast with (nolock),company c with (nolock)
                            where d.Locationrowid =l.rowid and
                                  d.appstatusrowid = ast.rowid  and
                                  c.rowid = l.CompanyRowid
    END GO

,使用此代码您会看到更新删除的行(old_value)

Try with this code

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE TRIGGER tri_UpdateAppDispo ON dbo.Test_App
For Update 
AS

declare @Approwid int
    Declare @triggername sysname
    Set @rowCnt = @@rowcount
    Set @triggername = object_name(@@procid)  

    If @rowCnt = 0
                RETURN

    If Update(appstatusrowid)
    BEGIN
    -----------------------------------------------------------------------------
                -- insert a record to the AppDispo table, if AppstatusRowid
                -- is being Updated
                -----------------------------------------------------------------------------
                Insert AppDispo(AppID, LastName, FirstName, [DateTime],Company,Location,LocationName,
                StatusDispo,[Username])
                            Select d.Rowid,d.LastName, d.FirstName, getDate(),C.CompanyCode,l.locnum,l.locname, ast.Displaytext,
                         SUSER_SNAME()+' '+User  
                            From     deleted d with(nolock),location l with (nolock),appstatus ast with (nolock),company c with (nolock)
                            where d.Locationrowid =l.rowid and
                                  d.appstatusrowid = ast.rowid  and
                                  c.rowid = l.CompanyRowid
    END GO

whith this code you get the update-deleted row(the old_value)

see you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文