SQL 如何保存存储过程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2670286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:01:26  来源:igfitidea点击:

How to save a Stored Procedure?

sqlsql-server-2008

提问by Richard77

I've been playing today with stored procedures. I'm stoked that I picked the basic of it up so easily. (I'll also try triggers very soon).

我今天一直在玩存储过程。我很高兴我如此轻松地掌握了它的基本知识。(我也会很快尝试触发器)。

Now, I'd like to know how to save my SPROC under the stored procedure folder (Programmability -> Stored Procedure) so that I can access it by name (eventually from my C# application). So far, when I press the save icon, I'm proposed to save the whole query. That's not what I want.

现在,我想知道如何将我的 SPROC 保存在存储过程文件夹(可编程性 -> 存储过程)下,以便我可以按名称(最终从我的 C# 应用程序)访问它。到目前为止,当我按下保存图标时,建议我保存整个查询。那不是我想要的。

Thanks for helping

谢谢你的帮助

回答by Justin Niessner

You actually have to run the CREATE PROCEDUREcommand so that the Stored Procedure builds.

您实际上必须运行该CREATE PROCEDURE命令才能构建存储过程。

Create Procedure - MSDN

创建过程 - MSDN

Here's an example straight from the MSDN page:

这是直接来自 MSDN 页面的示例:

USE AdventureWorks;
GO

-- If procedure exists already, drop it
IF OBJECT_ID ( 'HumanResources.uspGetAllEmployees', 'P' ) IS NOT NULL 
    DROP PROCEDURE HumanResources.uspGetAllEmployees;
GO

-- Create (or Re-create) the procedure
CREATE PROCEDURE HumanResources.uspGetAllEmployees
AS
    SET NOCOUNT ON;
    SELECT LastName, FirstName, JobTitle, Department
    FROM HumanResources.vEmployeeDepartment;
GO

Remember that after you create the Stored Procedure, you will have to Right Click -> Refreshthe Stored Procedure folder for the new procedure to appear.

请记住,在创建存储过程后,您必须进入Right Click -> Refresh存储过程文件夹才能显示新过程。

I would also suggest saving the *.sql file somewhere so you have the CREATE PROCEDUREscript somewhere in case you need to run again.

我还建议将 *.sql 文件保存在某处,以便CREATE PROCEDURE在需要再次运行时将脚本保存在某处。

回答by Mladen Prajdic

you have to actually run the create proc statement.

您必须实际运行 create proc 语句。

回答by systempuntoout

You need to run CREATE PROCEDUREcommand.
Try with this simple Stored Procedure example:

您需要运行CREATE PROCEDURE命令。
试试这个简单的存储过程示例:

CREATE PROCEDURE TestSP
AS
SELECT GETDATE() AS MyDate
GO

回答by KM.

Based on the OP's comment:

根据 OP 的评论:

I've run it using EXEC command, and it worked. When I didn't see the SPROC under the Stored Procedure folder, I though I didn't create it. So I tried to create it again, but I was told that the object with the same name exists already. I guess I was able to create it. But, why it is not visible under the Programmability --> Stored Procedure folder?

我已经使用 EXEC 命令运行它,并且它起作用了。当我没有看到存储过程文件夹下的 SPROC 时,我虽然没有创建它。所以我试图再次创建它,但我被告知同名的对象已经存在。我想我能够创造它。但是,为什么它在 Programmability --> Stored Procedure 文件夹下不可见?

When you run the stored procedure CREATE PROCEDURE....for the first time, you may need to refresh your stored procedure list in SSMS. Right click then REFRESH.

CREATE PROCEDURE....首次运行存储过程时,您可能需要刷新 SSMS 中的存储过程列表。右键单击然后刷新。