SQL Server 存储过程语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14804247/
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 13:36:56  来源:igfitidea点击:

SQL Server Stored Procedure Syntax

sqlsql-server-2008stored-procedures

提问by MFB

I am not really a SQL man, so I'm struggling. What is wrong with "program"below that is preventing me from creating this stored procedure? Any help or clean up suggestions would be great.

我不是一个真正的 SQL 人,所以我很挣扎。"program"下面有什么问题阻止我创建这个存储过程?任何帮助或清理建议都会很棒。

ALTER PROCEDURE [dbo].[CreateHaystackExportJob_Program]
    -- Add the parameters for the stored procedure here
    @ID AS VARCHAR
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @JobCount AS INT

    SELECT 
        @JobCount = COUNT (*) 
    FROM 
        dbo.HaystackExportJobs
    WHERE 
        ID = @ID AND ExportType = "program" 

    IF @JobCount = 0
        INSERT INTO dbo.HaystackExportJobs (ID,ExportType) VALUES (@ID,"program")

END

回答by Taryn

A few things, first you should include a length on the @idparameter.

有几件事,首先你应该在@id参数中包含一个长度。

Second, string values in SQL Server should be in single quotes so remove the double quotes and replace them with single quotes around "program". So your code will be similar to this:

其次,SQL Server 中的字符串值应该放在单引号中,因此删除双引号并将它们替换为"program". 所以你的代码将类似于:

CREATE PROCEDURE [dbo].[CreateHaystackExportJob_Program]
    -- Add the parameters for the stored procedure here
    @ID AS VARCHAR(50) -- place a length on this parameter or an int if this is numeric
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @JobCount AS INT

    SELECT 
        @JobCount = COUNT (*) 
    FROM 
        dbo.HaystackExportJobs
    WHERE 
        ID = @ID 
        AND ExportType = 'program' 

    IF @JobCount = 0
        INSERT INTO dbo.HaystackExportJobs (ID,ExportType) 
        VALUES (@ID,'program')

END

See SQL Fiddle with Demo

参见SQL Fiddle with Demo

回答by UnhandledExcepSean

Odd things:

奇怪的事情:

  1. the parameter is declared varchar without a size.
  2. Can't use quotes to encapsulate strings. You need to use aprostrophes.
  1. 参数被声明为没有大小的 varchar。
  2. 不能使用引号来封装字符串。您需要使用撇号。

回答by Dan Bracuk

Use single quotes, not double quotes. Here is an example of where you use double quotes. ExportType = "program"

使用单引号,而不是双引号。这是使用双引号的示例。导出类型 = "程序"