关键字' DECLARE'附近的语法不正确。关键字' AS'附近的语法不正确

时间:2016-02-25 22:17:51

标签: sql-server tsql stored-procedures

使用以下代码,我不断收到以下错误:

  

关键字' DECLARE'附近的语法不正确   关键字' AS'附近的语法不正确。

非常感谢任何帮助!

DROP PROCEDURE [dbo].[csp_WellTestValidationResultsTEST_GetData]
GO
CREATE PROCEDURE [dbo].[csp_WellTestValidationResultsTEST_GetData]

DECLARE @ColumnName varchar(max);       --Column Name that Maps to AF Attribute
DECLARE @ColumnValue varchar(max);      --Specific Value for that Column
DECLARE @LeftNumber varchar(50) = SUBSTRING(@ColumnValue,0,CHARINDEX('-',@ColumnValue,0));
DECLARE @RightNumber varchar(50) = SUBSTRING(@ColumnValue,CHARINDEX('-',@ColumnValue,0)+1,LEN(@ColumnValue));
DECLARE @API varchar(50) = SUBSTRING(@ColumnName,0,CHARINDEX('-',@ColumnName,0));


AS
BEGIN
SET NOCOUNT ON;

IF (@ColumnName = 'API14-2')
       Exec('Select * from dbo.SSIS_RTRM_WellTestValidationResults where '+@API+' = '+@LeftNumber+' OR '+@API+' = '+@RightNumber+' order by TestDate Desc')   
Else 
    Select * From dbo.SSIS_RTRM_WellTestValidationResults where WellId = ''
  END

2 个答案:

答案 0 :(得分:1)

要将变量传递给sporc,请使用语法create procedure {schema.procedure_name} {@ variable1} {variable1_data_type},{@ variable2} {variable2_data_type} 如 {SPROC here}

如果您想要一个变量的默认值,这也使变量成为可选项,{variable} {vairable_data_type} = {variable_default_value}

这仅适用于调用sproc时传递的变量。您在sproc中声明的sproc中定义的任何变量。

CREATE PROCEDURE [dbo].[csp_WellTestValidationResultsTEST_GetData]
@ColumnName varchar(max);       --Column Name that Maps to AF Attribute
,@ColumnValue varchar(max);      --Specific Value for that Column
AS
DECLARE @LeftNumber varchar(50) = SUBSTRING(@ColumnValue,0,CHARINDEX('-',@ColumnValue,0));
DECLARE @RightNumber varchar(50) = SUBSTRING(@ColumnValue,CHARINDEX('-',@ColumnValue,0)+1,LEN(@ColumnValue));
DECLARE @API varchar(50) = SUBSTRING(@ColumnName,0,CHARINDEX('-',@ColumnName,0));

答案 1 :(得分:0)

As' Gordon Linoff'指出,你的创建过程语句应该是

CREATE PROCEDURE [dbo].[csp_WellTestValidationResultsTEST_GetData]

AS
BEGIN
SET NOCOUNT ON;

DECLARE @ColumnName varchar(max);       --Column Name that Maps to AF Attribute
........

另一方面,我不确定您是否可以在同一语句中声明和赋值,可能需要将其替换为:

DECLARE @LeftNumber varchar(50);
SET @LeftNumber = SUBSTRING(@ColumnValue,0,CHARINDEX('-',@ColumnValue,0));

等等