sql - 避免 "An INSERT EXEC statement cannot be nested"

标签 sql sql-server insert-into

我知道不可能嵌套 insert ... exec 语句,但我仍然感兴趣 - 有没有办法检查我是否已经有事件的 insert .. .exec 以避免实际错误?

所以我想要这样的东西:

....
if <check if I have outer insert into exec> = 0
    insert into <#some temporary table>
    exec <stored procedure>

换句话说 - insert ... exec 是可选的,拥有它很好,但如果有人尝试使用外部 insert ... 调用我的过程,我想跳过它执行

最佳答案

如上所述here

This is a common issue when attempting to 'bubble' up data from a chain of stored procedures. A restriction in SQL Server is you can only have one INSERT-EXEC active at a time. I recommend looking at How to Share Data Between Stored Procedures which is a very thorough article on patterns to work around this type of problem.

尝试使用OpenRowset

INSERT INTO #YOUR_TEMP_TABLE
SELECT * FROM OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;','set fmtonly off EXEC [ServerName].dbo.[StoredProcedureName] 1,2,3')

关于sql - 避免 "An INSERT EXEC statement cannot be nested",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35010612/

相关文章:

php - ZF2 使用 INSERT INTO .... SELECT 从表到同一个表进行批量插入

mysql - 单个 MySQL 查询返回不同分类的平均值

sql - 如何乘以一行?

sql - 如何进行条件连接

sql-server - 可以仅为存储过程设置 MAXDOP 吗?

php - 无法使用(PHP、AJAX、JQ)将数据插入 MySQL 数据库

database - 减少应用程序中的数据库调用并改进数据插入

mysql - SQLAlchemy 可以独立于框架单独使用吗?是否推荐这样做?

sql - 在没有分组依据的情况下在 CASE 中使用字段

c# - 如何使 EF 核心将自定义排序转换为纯 'When Then' 而不是分层