2013-02-11 77 views
0

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

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 
+3

如果你发布错误信息,你会得到更好的反馈 – 2013-02-11 00:24:11

+0

你是对的,抱歉。 – MFB 2013-02-11 00:45:48

回答

3

奇怪的事情:

  1. 这个参数被声明为varchar没有大小。
  2. 不能使用引号来封装字符串。你需要使用aprostrophes。
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 

SQL Fiddle with Demo

0

使用单引号,不是双引号。这里是你使用双引号的例子。 ExportType =“program”