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
这篇关于存储过程创建后如何执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:存储过程创建后如何执行?
基础教程推荐
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01