Triggers 触发器中的sybase游标

Triggers 触发器中的sybase游标,triggers,cursor,sap-ase,Triggers,Cursor,Sap Ase,我试图在Solaris上运行的sybase ASE 15.0.3系统的触发器中使用光标。这样做的目的是想知道表的哪一列正在更新。然后,我将这些信息保存在管理表中,以便进一步查找 create trigger test_trigger on my_table for update as set nocount on /* declare cursor */ declare @colname varchar(64) declare column_name cursor for sel

我试图在Solaris上运行的sybase ASE 15.0.3系统的触发器中使用光标。这样做的目的是想知道表的哪一列正在更新。然后,我将这些信息保存在管理表中,以便进一步查找

create trigger test_trigger on my_table for update as

set nocount on

/* declare cursor */
declare @colname varchar(64)

declare column_name cursor for 
    select syscolumns.name from syscolumns join sysobjects on (sysobjects.id = syscolumns.id) where sysobjects.name = 'my_table'

/* open the cursor */
open column_name

/* fetch the first row */
fetch column_name into @colname

/* now loop, processing all the rows
** @@sqlstatus = 0 means successful fetch
** @@sqlstatus = 1 means error on previous fetch
** @@sqlstatus = 2 means end of result set reached
*/
while (@@sqlstatus != 2)
begin    
    /* check for errors */
    if (@@sqlstatus = 1)
    begin
        print "Error in column_names cursor"
        return
    end

    /* now do the insert if colum was updaed  */
    if update(@colname)
    begin
        insert into my_save_table (login,tablename,field,action,pstamp) 
            select suser_name(),'my_table',@colname,'U',getdate() from inserted
    end

    /* fetch the next row */
    fetch column_name into @colname
end

/* close the cursor and return */
close column_name   
go
不幸的是,在isql中尝试运行此操作时,我遇到以下错误:

 Msg 102, Level 15, State 1:
 Server 'my_sybase_server', Procedure 'test_trigger', Line 34:
 Incorrect syntax near '@colname'.
我做了一些调查,发现第34行表示以下声明:

if update(@colname)
然后我试着只检查1列,并用

if update(some_column_name)
这实际上很好,我不知道如何解决这个问题。看起来update()函数不允许包含变量。我没有在sybase books或google ect的任何其他地方找到任何其他信息。有人能找到解决办法吗?它可能是一只虫子吗?光标是否有解决方法


感谢您的建议

问题在于
update(@colname)
类似于
update('colname')
,需要
update(colname)
。为了实现这一点,您需要使用动态SQL

我已经看到了,可以使用:

动态执行Transact-SQL

当与字符串或char_变量选项一起使用时,execute将连接提供的字符串和变量以执行 生成的Transact-SQL命令。这种形式的execute命令可以 可以在SQL批处理、过程和触发器中使用



查看有关如何使用动态sql的示例

如果在每次更改表(添加/删除列)时重新创建触发器不是问题,那么您可以使用此类查询生成触发器的主体

select 
'if update('+c.name+')
   set @colname = '''+c.name+'''
'
from syscolumns c where id = object_id('my_table')

当您使用
if update(某些列\u name)
时,
某些列\u name
部分在引号之间?否,根据我在sybase网站上找到的sybase示例,不需要引号。我发现在update()函数中不可能使用任何变量。不幸的是,使用动态sql也是不可能的,因为此更新需要在触发器中运行,而当我使用exec()时,它不再在触发器中运行……还有其他想法吗?!是的,问题可能就在那里。你熟悉动态sql吗?是的,我熟悉。我尝试了它,但它不起作用:-(这是真的,但问题出在其他地方:要运行动态sql,您需要一个exec()函数来执行该命令。但这不知何故在子进程中,因此不再在触发器本身中。然后问题是该update()函数不再在触发器中,sybase ase会向我发出警报。。。