Errors while creating stored procedure in sql server

时间:2016-04-07 10:53:57

标签: sql-server stored-procedures

Below errors appear when i am trying to create simple stored procedure in SQL Server

Msg 102, Level 15, State 1, Procedure test, Line 2 Incorrect syntax near 'varchar'. Msg 137, Level 15, State 1, Procedure test, Line 27 Must declare the scalar variable "@".

Very much new to writing stored procedures. Kindly guide

USE [database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[TEST]
    @[CUSTNAME] VARCHAR(100),                   
    @[CUSTOMER_SHORT_NAME] VARCHAR(25),
    @[COUNTRY] VARCHAR(50),
    @[region] VARCHAR(25),
    @[Area] VARCHAR(25),
AS BEGIN

    SET NOCOUNT ON;

    UPDATE [dbo].[TEST]
    SET
          @[CUSTNAME]=[CUSTNAME]
        , @[CUSTOMER_SHORT_NAME]=[CUSTOMER_SHORT_NAME]
        , @[COUNTRY]=[COUNTRY]
        , @[region]=[region]
        , @[Area]=[Area]
    WHERE period=@period
        AND customer=@customer

END

3 个答案:

答案 0 :(得分:3)

我认为你需要摆脱变量中的[]。而且您的更新查询也是错误的:

USE [database]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[TEST]

   @CUSTNAME VARCHAR(100),                   
   @CUSTOMER_SHORT_NAME VARCHAR(25),
   @COUNTRY VARCHAR(50),
   @region VARCHAR(25),
   @Area VARCHAR(25),

AS BEGIN

   SET NOCOUNT ON;

   UPDATE [dbo].[TEST]
   SET
          [CUSTNAME] = @CUSTNAME
        , [CUSTOMER_SHORT_NAME] = @CUSTOMER_SHORT_NAME
        , [COUNTRY] = @COUNTRY
        , [region] = @region
        , [Area] = @Area
  WHERE period = @period
    AND customer = @customer

END

您还需要为@period@customer添加参数。

答案 1 :(得分:1)

USE [my_db]

    GO
    SET ANSI_NULLS ON
    GO
   SET QUOTED_IDENTIFIER ON
   GO

   create PROCEDURE [dbo].[TEST]

   @CUSTNAME varchar(100),                   
   @CUSTOMER_SHORT_NAME varchar(25),
   @COUNTRY varchar(50),
   @region varchar(25),
   @Area varchar(25),
   @period varchar(50),
   @customer varchar(50)
   AS
   BEGIN
   SET NOCOUNT ON;

   update [dbo].[TEST]
   set @CUSTNAME=[CUSTNAME]
  ,@CUSTOMER_SHORT_NAME=[CUSTOMER_SHORT_NAME]
  ,@COUNTRY=[COUNTRY]
  ,@region=[region]
  ,@Area=[Area]
  where period=@period
  and customer=@customer
  END
GO

删除[]第一个错误, 添加2参数@Period和@customer

答案 2 :(得分:0)

您正以相反的方式分配值。

错误就在这里

  

update [dbo].[TEST] set @[CUSTNAME]=[CUSTNAME] > ,@[CUSTOMER_SHORT_NAME]=[CUSTOMER_SHORT_NAME] ,@[COUNTRY]=[COUNTRY] > ,@[region]=[region] ,@[Area]=[Area] where period=@period and > customer=@customer END

尝试使用此代码而非上述代码

 update [dbo].[TEST]
   set [CUSTNAME]=@CUSTNAME
  ,[CUSTOMER_SHORT_NAME]=@CUSTOMER_SHORT_NAME
  ,[COUNTRY]=@COUNTRY
  ,[region]=@region
  ,[Area]=@Area
  where period=@period
  and customer=@customer
  END