Pét*_*ter 6 sql stored-procedures exec user-defined-functions
First I try to explain the circumstances. I store the the filter expression in one column separated by line breaks. The base idea was this:
SELECT
'SELECT ''' + REPLACE(topic_filter,CHAR(10),''' UNION ALL SELECT ''') + ''''
FROM dbo.topic_filter T
WHERE
T.id = @id
FOR XML PATH('')
Run Code Online (Sandbox Code Playgroud)
在此之后,我只需执行此字符串即可将数据放入临时表中.我的问题从这里开始.该片段位于存储过程中,并由多个存储过程使用以生成要填充的基本源.
方法1:
从另一个SP调用此sp以填充临时表.
结果1:
无法嵌套INSERT EXEC语句.(如果我只是用exec dbo调用...样式代码正在工作.如果我尝试在存储过程中调用,我只会收到错误)
方法2:
我把上面的代码放到表值函数中.
结果2:
在函数内无效使用副作用运算符'INSERT EXEC'.(函数本身不编译)
谢谢,
Péter
在此期间,我设法解决了问题(帮助:)).解决方案很简单:
exec('insert into t2 ' + @str)
Run Code Online (Sandbox Code Playgroud)
其中@str包含一个select语句.
我不知道为什么,但这种方式没有错误.我调用存储过程的方法:
SET @exec = 'exec dbo.trFilterTopic ''' + @id+ ''',null,null,1'
INSERT INTO #filtered
exec (@exec)
Run Code Online (Sandbox Code Playgroud)
我希望通过这个解决方案为其他人节省一些时间.
再见,
彼得