在触发器中调用扩展存储过程一问,多谢各位:)

小小的一株含羞草 2004-11-12 05:47:37
触发器中就是调用master库中的一个扩展存储过程,将新增的行数传递给它,然后扩展存储过程调用外部dll将新增行数写到一个文本中。

如果直接执行扩展存储过程,执行都是正确的;但如果通过触发器来执行时,有如下错误提示:

Server: Msg 15002, Level 16, State 1, Procedure sp_addextendedproc, Line 12
The procedure 'sp_addextendedproc' cannot be executed within a transaction.
Server: Msg 15002, Level 16, State 1, Procedure sp_addextendedproc, Line 12
The procedure 'sp_addextendedproc' cannot be executed within a transaction.
Server: Msg 2812, Level 16, State 62, Line 13
Could not find stored procedure 'SetFileName'.
The statement has been terminated.

请问这是什么错误?如何修改呢?多谢!
...全文
152 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
多谢zjcxc(邹建) ,问题解决啦:)
zjcxc 元老 2004-11-13
  • 打赏
  • 举报
回复
--即做这样的调整

--注册扩展存储过程
exec sp_addextendedproc 'SetFileName', 'storeproc.dll' --声明函数
exec sp_addextendedproc 'addLine', 'storeproc.dll'
go

--触发器源码:
CREATE TRIGGER tele_Itg ON dbo.telemetercfgx
FOR INSERT
AS

if @@rowcount = 0
return

EXEC master..sp_testdll @num=@@rowcount
go

--扩展存储过程代码:
CREATE PROCEDURE sp_testdll @num int
AS
declare @szFileName varchar(200)
declare @szText varchar(200)
declare @rt int

Select @szFileName = 'c:\welcome.txt'

EXEC @rt = SetFileName @szFileName --调用SetFileName函数,参数为--szFileName;

if @rt = 0
begin
select @szText = 'inserted number:' + CONVERT(varchar(5),@num)
Exec @rt = addLine @szText --调用addLine
--select @szText = 'welcome 02'
--Exec @rt = addLine @szText
end

dbcc SetFileName(free)
dbcc addLine(free)
GO

--以后扩展存储过程有变化的时候,再执行这两句删除扩展存储过程
--exec sp_dropextendedproc 'SetFileName'
--exec sp_dropextendedproc 'addLine'

zjcxc 元老 2004-11-13
  • 打赏
  • 举报
回复
exec sp_addextendedproc 'SetFileName', 'storeproc.dll' --声明函数
exec sp_addextendedproc 'addLine', 'storeproc.dll'

这个你预先执行一次就行了,存储过程中不需要每次都添加再删除的
  • 打赏
  • 举报
回复
哪位知道怎么修改啊?多谢啦
  • 打赏
  • 举报
回复
那这个怎么修改啊?
wzh1215 2004-11-12
  • 打赏
  • 举报
回复
应该是事务嵌套所导致的!
fxcl2006 2004-11-12
  • 打赏
  • 举报
回复
上面不是写了吗?存储过程sp_addextendedproc不能在事务中执行。
  • 打赏
  • 举报
回复
触发器源码:
CREATE TRIGGER tele_Itg ON dbo.telemetercfgx
FOR INSERT
AS

if @@rowcount = 0
return

EXEC master..sp_testdll @num=@@rowcount

扩展存储过程代码:
CREATE PROCEDURE sp_testdll @num int
AS

exec sp_addextendedproc 'SetFileName', 'storeproc.dll' --声明函数
exec sp_addextendedproc 'addLine', 'storeproc.dll'

declare @szFileName varchar(200)
declare @szText varchar(200)
declare @rt int

Select @szFileName = 'c:\welcome.txt'

EXEC @rt = SetFileName @szFileName --调用SetFileName函数,参数为--szFileName;

if @rt = 0
begin
select @szText = 'inserted number:' + CONVERT(varchar(5),@num)
Exec @rt = addLine @szText --调用addLine
--select @szText = 'welcome 02'
--Exec @rt = addLine @szText
end

exec sp_dropextendedproc 'SetFileName'
exec sp_dropextendedproc 'addLine'

dbcc SetFileName(free)
dbcc addLine(free)
GO

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧