SQL Server存储过程语法
我不是一个真正的SQL人,所以我在挣扎。下面的程序有什么问题阻止我创建此存储过程?任何帮助或清理建议都会很好SQL Server存储过程语法,sql,sql-server-2008,stored-procedures,Sql,Sql Server 2008,Stored Procedures,我不是一个真正的SQL人,所以我在挣扎。下面的程序有什么问题阻止我创建此存储过程?任何帮助或清理建议都会很好 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 set
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
奇怪的事情:
该参数声明为varchar,但没有大小。
无法使用引号来封装字符串。你需要用一种反义词。
首先,您应该在@id参数中包含一个长度 第二,SQLServer中的字符串值应该用单引号括起来,所以删除双引号,并用程序周围的单引号替换它们。因此,您的代码与此类似:
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
请参见使用单引号,而不是双引号。下面是一个使用双引号的示例。ExportType=program如果发布错误消息,您将获得更好的反馈