How to execute a stored procedure after it is created?(存储过程创建后如何执行?)
问题描述
我试图在创建后直接执行存储过程,但是它没有被调用.在执行调用期间似乎尚未创建存储过程.
I'm trying to execute a stored procedure directly after its creation however it is not getting called. It looks like the stored procedure is not yet created during the execution call.
脚本如下所示:
CREATE PROCEDURE sp_Transfer_RegionData
AS
BEGIN
INSERT INTO Region (regionName)
SELECT column1
FROM openquery(ITDB, 'select * from db.table1')
END
EXEC sp_Transfer_RegionData
脚本运行良好,但未填充所需的表.替换执行部分后:
The script runs fine however the needed table is not populated. After replacing the execution part with:
IF OBJECT_ID('sp_Transfer_RegionData') IS NOT NULL
begin
exec [dbo].[sp_Transfer_RegionData]
print 'tada'
end
我可以看到存储过程在必须执行时不存在.在互联网上找不到解决方案...
I could see that the stored procedure does not exist when it has to be executed. Couldn't find a solution for this in the internet...
那么如何让SQL脚本同步运行,让存储过程在执行部分就已经存在了?
So how to make the SQL script run sync so that the stored procedure would already exist during the execution part?
推荐答案
在创建 SP 后需要一个 GO,否则你创建了一个递归的 SP,它无限期地"调用自己,这在 SQL Server 中是 32 次.
You need a GO after you created the SP otherwise you have created a recursive SP that calls itself "indefinitely" which is 32 times in SQL Server.
最大存储过程、函数、触发器或视图嵌套级别超出(限制 32).
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).
试试这个:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE sp_Transfer_RegionData
AS
BEGIN
INSERT INTO Region (regionName)
SELECT column1
FROM openquery(ITDB, 'select * from db.table1')
END
GO
EXEC sp_Transfer_RegionData
这篇关于存储过程创建后如何执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:存储过程创建后如何执行?
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
