SQL Server存储过程语法

MFB*_*MFB 0 sql stored-procedures sql-server-2008

我不是一个真正的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
Run Code Online (Sandbox Code Playgroud)

Tar*_*ryn 5

首先,您应该在@id参数上包含一个长度.

其次,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
Run Code Online (Sandbox Code Playgroud)

请参阅SQL Fiddle with Demo