2013-02-11 47 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」