INSERT EXEC Statement cannot be nested(INSERT EXEC 语句不能嵌套)
问题描述
我有三个程序 MainProcedure,Procedure1,Procedure2
I have three Procedures MainProcedure,Procedure1,Procedure2
1) 在程序 1 中,我只有一个选择语句,
1) In Procedure1 I just have a select statement ,
2) 在程序 2 中调用程序 1 并将输出插入 #table
2) In Procedure2 am calling the Procedure1 and inserting the Output to a #table
3) 在主程序中,我正在调用程序 2 并尝试将输出插入到引发错误的 #table 中
3) In the main Procedure I am calling the Procedure2 and iam trying to insert the Output to a #table which throws an error
消息 8164,级别 16,状态 1,过程程序 2,第 10 行INSERT EXEC 语句不能嵌套.
我可以使用 Openrowset 解决这个问题,我需要使用指定服务器名称,有没有其他方法可以通过不指定服务器名称详细信息来解决这个问题
I can resolve this using Openrowset where I need to use specify Server Name ,is there any other way to solve this by not specifying the servername details
请查找示例程序以供参考
please find the sample procedure for reference
Create Proc Procedure1
As
Begin
Select 'Arun' Name, 'Pollachi' Place
Union
Select 'Vedaraj' Name, 'Devakottai' Place
End
Go
Create Proc Procedure2
As
Begin
Create Table #Table1
(
Name Varchar(50), Place Varchar(50)
)
INSERT #Table1
Exec Procedure1
SELECT 'Procedure2' [Source], * FROM #Table1
DROP TABLE #Table1
End
Go
Create Proc MainProcedure
As
Begin
Create Table #Table1
(
[Source] Varchar(50), Name Varchar(50), Place Varchar(50)
)
INSERT #Table1
Exec Procedure2
select * from #Table1
DROP TABLE #Table1
End
Go
任何人都可以更改我的主要程序并使其执行谢谢!!
can any one change my main procedure and make it to get executed Thanks!!
推荐答案
就像你说的,openrowset 可以工作,但除此之外我能想到的唯一方法是:
Like you said, openrowset will work, but other than that the only ways I can think of would be:
- 将 proc 1 和 proc 2 都更改为基于表的函数
- 将 proc 2 更改为 CLR 并将所有逻辑放在那里
- 将表作为表值参数传递
这里有更多关于这个原因的信息:
There's more info about the reasoning for this here:
https://connect.microsoft.com/SQLServer/feedback/details/294571/improve-insert-exechttp://dataeducation.com/revisiting-isnull-coalesce-and-the-perils-of-micro-optimization/
这篇关于INSERT EXEC 语句不能嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:INSERT EXEC 语句不能嵌套
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
