创建存储过程在关键字'Declare'附近获取不正确的语法

时间:2016-09-08 22:26:50

标签: sql sql-server

我不知道为什么会收到此错误

  

Msg 156,Level 15,State 1,Procedure usp_cloud_ClientAllSearch,Line 2
  关键字'Declare'附近的语法不正确。

因为我已经声明了变量。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[usp_cloud_ClientAllSearch]
    DECLARE @Search NVARCHAR(30)

    SELECT 
        client_Name, client_Surname, client_CompanyName, 
        clientContact_TelephoneNo, clientContact_MobileNo, 
        clientAddress_PostalCode
    FROM 
        cloud_Client c
    JOIN
        dbo.cloud_ClientAddresses a ON a.client_ID = c.client_ID
    LEFT JOIN 
        cloud_ClientContact cc ON cc.client_ID = c.client_ID
    WHERE 
        c.client_Name LIKE @Search + '%' OR
        c.client_Surname LIKE @Search + '%' OR
        cc.clientContact_MobileNo LIKE @Search + '%' OR
        a.clientAddress_PostalCode LIKE @Search + '%'

2 个答案:

答案 0 :(得分:0)

@search应该是存储过程的输入参数,(不需要Declare),并且在存储过程的签名块和主体之间需要As。过程。

Create PROCEDURE [dbo].[usp_cloud_ClientAllSearch]
 @Search NVARCHAR(30)
As

   SELECT client_Name, client_Surname, client_CompanyName, 
        clientContact_TelephoneNo, clientContact_MobileNo, 
        clientAddress_PostalCode
   FROM cloud_Client c
       Join dbo.cloud_ClientAddresses a
          ON a.client_ID = c.client_ID
       LEFT JOIN cloud_ClientContact cc
          ON cc.client_ID = c.client_ID
   WHERE c.client_Name LIKE @Search + '%' or 
         c.client_Surname LIKE @Search + '%' or 
        cc.clientContact_MobileNo LIKE @Search + '%' or 
         a.clientAddress_PostalCode LIKE @Search + '%'

答案 1 :(得分:0)

这是SQL Proc的基本结构 如果你宣布一些变量,它应该在' as begin'之间。并且'结束'。

CREATE PROCEDURE procedure_name
[(parameter_name [IN | OUT] )]
AS
BEGIN

   Declare .....
   <Proc Body>

END ;

看了你的proc后我得出结论,@ search不是变量。它是一个输入参数。

CREATE PROCEDURE [dbo].[usp_cloud_ClientAllSearch]
(
@Search NVARCHAR(30)
)
As begin 
    .....
END