简体   繁体   English

SQL Server存储过程语法

[英]SQL Server Stored Procedure Syntax

I am not really a SQL man, so I'm struggling. 我不是一个真正的SQL专家,所以我很挣扎。 What is wrong with "program" below that is preventing me from creating this stored procedure? 下面的"program"有什么问题使我无法创建此存储过程? Any help or clean up suggestions would be great. 任何帮助或清理建议都很好。

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

A few things, first you should include a length on the @id parameter. 首先,您应该在@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" . 其次,SQL Server中的字符串值应使用单引号引起来,因此请删除双引号,并在"program"周围将其替换为单引号。 So your code will be similar to this: 因此,您的代码将与此类似:

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

Odd things: 奇怪的事情:

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

Use single quotes, not double quotes. 使用单引号,而不是双引号。 Here is an example of where you use double quotes. 这是使用双引号的示例。 ExportType = "program" ExportType =“程序”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM