触发器中的 sybase 游标
我正在尝试在 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)
That 实际上工作得很好,但我不这样做不知道如何解决这个问题。看起来 update() 函数不知何故不允许包含变量。我没有在 sybase 书籍或 google 等其他地方找到任何其他信息。有人可以找到解决方案吗?这可能是一个错误吗?是否有针对光标的解决方法?
感谢您的任何建议
I am trying to use a cursor in a trigger on a sybase ASE 15.0.3 system running on Solaris. The purpose for this is that I want to know which column of a table is getting updated. This information I then save in an admin table for further lookups.
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
Unfortunately when trying to run this in isql I get the following error:
Msg 102, Level 15, State 1:
Server 'my_sybase_server', Procedure 'test_trigger', Line 34:
Incorrect syntax near '@colname'.
I did some investigations and found out that line 34 means the following statement:
if update(@colname)
then I tried to just check on 1 column and replaced it by
if update(some_column_name)
That actually worked fine and I don't have any other idea how to fix that. It looks like the update() function somehow not allows to contain a variable. I did not find any additional information on the sybase books or anywhere else in google ect. Does anybody may find a solution for this? Is it may a bug? Are there workarounds for the cursor?
Thanks for any advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
update(@colname)
类似于update('colname')
并且需要是update(colname)
。为了实现这一目标,您需要使用动态 SQL。我已经看到 文档并且可以使用:
查看这篇文章了解如何使用动态sql的示例!
The problem is that
update(@colname)
is something likeupdate('colname')
and needs to beupdate(colname)
. In order to you achieve that, you need to use Dynamic SQL.I've already saw the documentation and it's possible to use:
Check this article for an example on how to use dynamic sql!
如果每次更改表(添加/删除列)时重新创建触发器不是问题,您可以使用此类查询为触发器生成正文
If it is not a problem to recreate the trigger every time the table is altered (columns added/dropped) you may just generate the body for your trigger with such query