SQL Server中的存储过程(变量声明)

时间:2021-12-05 16:40:46

I'm trying to create a stored procedure in SQL Server, but I keep getting an error whenever I try to execute it:

我正在尝试在SQL Server中创建存储过程,但每当我尝试执行它时,我都会收到错误:

Msg 137, Level 15, State 2, Line 48
Must declare the scalar variable "@FirstName"

消息137,级别15,状态2,行48必须声明标量变量“@FirstName”

I thought I declared FirstName in WHERE condition, but it doesn't seem right.

我以为我在WHERE条件中声明了FirstName,但它似乎不正确。

CREATE PROCEDURE Example6
    @FirstName VARCHAR(50),
    @LastName VARCHAR(50),
    @MiddleName VARCHAR(50)
AS 
    SELECT  
        [employee_id], [employee_type_id], 
        [HR_status_id], [employer_id],
        [fName], [mName], [lName],
        [address], [city], [state_id], [zip],
        [hireDate], [currDate],
        [ssn], [ext_emp_id],
        [terminationDate], [dob],
        [initialMeasurmentEnd], 
        [plan_year_id], [limbo_plan_year_id], [meas_plan_year_id],
        [modOn], [modBy],
        [plan_year_avg_hours], [limbo_plan_year_avg_hours],
        [meas_plan_year_avg_hours], [imp_plan_year_avg_hours],
        [classification_id], [aca_status_id], [ResourceId]
    FROM 
        dbo.employee
    WHERE 
       [fName] = @FirstName 
       AND [lName] = @LastName 
       AND [mName] = @MiddleName
       AND @FirstName LIKE '%son%' OR @LastName LIKE '%son%';

3 个解决方案

#1


2  

You should add parentheses in the parameters clause.
Try that one:

您应该在parameters子句中添加括号。试试那个:

CREATE PROCEDURE Example6

(@FirstName varchar(50),
@LastName varchar(50),
@MiddleName varchar(50))
AS 
SELECT [employee_id]
      ,[employee_type_id]
      ,[HR_status_id]
      ,[employer_id]
      ,[fName]
      ,[mName]
      ,[lName]
      ,[address]
      ,[city]
      ,[state_id]
      ,[zip]
      ,[hireDate]
      ,[currDate]
      ,[ssn]
      ,[ext_emp_id]
      ,[terminationDate]
      ,[dob]
      ,[initialMeasurmentEnd]
      ,[plan_year_id]
      ,[limbo_plan_year_id]
      ,[meas_plan_year_id]
      ,[modOn]
      ,[modBy]
      ,[plan_year_avg_hours]
      ,[limbo_plan_year_avg_hours]
      ,[meas_plan_year_avg_hours]
      ,[imp_plan_year_avg_hours]
      ,[classification_id]
      ,[aca_status_id]
      ,[ResourceId]
     FROM dbo.employee
    WHERE  @FirstName LIKE '%son%' OR @LastName LIKE '%son%';

#2


2  

You need to wrap the parameters for the stored procedure in (), then call the stored procedure such as:

您需要在()中包装存储过程的参数,然后调用存储过程,例如:

EXEC Procedure 'Son','Son','Son'

执行程序'儿子','儿子','儿子'

       CREATE PROCEDURE Example6
        (
        @FirstName varchar(50),
        @LastName varchar(50),
        @MiddleName varchar(50)
        )
        AS 
        SELECT [employee_id]
              ,[employee_type_id]
              ,[HR_status_id]
              ,[employer_id]
              ,[fName]
              ,[mName]
              ,[lName]
              ,[address]
              ,[city]
              ,[state_id]
              ,[zip]
              ,[hireDate]
              ,[currDate]
              ,[ssn]
              ,[ext_emp_id]
              ,[terminationDate]
              ,[dob]
              ,[initialMeasurmentEnd]
              ,[plan_year_id]
              ,[limbo_plan_year_id]
              ,[meas_plan_year_id]
              ,[modOn]
              ,[modBy]
              ,[plan_year_avg_hours]
              ,[limbo_plan_year_avg_hours]
              ,[meas_plan_year_avg_hours]
              ,[imp_plan_year_avg_hours]
              ,[classification_id]
              ,[aca_status_id]
              ,[ResourceId]
             FROM dbo.employee
            WHERE 
            [FirstName] LIKE @FirstName OR [LastName] LIKE @LastName

#3


0  

In SSMS, if you right-click Stored Procedures and select New Stored Procedure, you'll get this template generated for you. Notice the BEGIN and END that are missing from your SP:

在SSMS中,如果右键单击“存储过程”并选择“新建存储过程”,则将为您生成此模板。注意SP中缺少的BEGIN和END:

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> 
    -- Add the parameters for the stored procedure here
    <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
    <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO

#1


2  

You should add parentheses in the parameters clause.
Try that one:

您应该在parameters子句中添加括号。试试那个:

CREATE PROCEDURE Example6

(@FirstName varchar(50),
@LastName varchar(50),
@MiddleName varchar(50))
AS 
SELECT [employee_id]
      ,[employee_type_id]
      ,[HR_status_id]
      ,[employer_id]
      ,[fName]
      ,[mName]
      ,[lName]
      ,[address]
      ,[city]
      ,[state_id]
      ,[zip]
      ,[hireDate]
      ,[currDate]
      ,[ssn]
      ,[ext_emp_id]
      ,[terminationDate]
      ,[dob]
      ,[initialMeasurmentEnd]
      ,[plan_year_id]
      ,[limbo_plan_year_id]
      ,[meas_plan_year_id]
      ,[modOn]
      ,[modBy]
      ,[plan_year_avg_hours]
      ,[limbo_plan_year_avg_hours]
      ,[meas_plan_year_avg_hours]
      ,[imp_plan_year_avg_hours]
      ,[classification_id]
      ,[aca_status_id]
      ,[ResourceId]
     FROM dbo.employee
    WHERE  @FirstName LIKE '%son%' OR @LastName LIKE '%son%';

#2


2  

You need to wrap the parameters for the stored procedure in (), then call the stored procedure such as:

您需要在()中包装存储过程的参数,然后调用存储过程,例如:

EXEC Procedure 'Son','Son','Son'

执行程序'儿子','儿子','儿子'

       CREATE PROCEDURE Example6
        (
        @FirstName varchar(50),
        @LastName varchar(50),
        @MiddleName varchar(50)
        )
        AS 
        SELECT [employee_id]
              ,[employee_type_id]
              ,[HR_status_id]
              ,[employer_id]
              ,[fName]
              ,[mName]
              ,[lName]
              ,[address]
              ,[city]
              ,[state_id]
              ,[zip]
              ,[hireDate]
              ,[currDate]
              ,[ssn]
              ,[ext_emp_id]
              ,[terminationDate]
              ,[dob]
              ,[initialMeasurmentEnd]
              ,[plan_year_id]
              ,[limbo_plan_year_id]
              ,[meas_plan_year_id]
              ,[modOn]
              ,[modBy]
              ,[plan_year_avg_hours]
              ,[limbo_plan_year_avg_hours]
              ,[meas_plan_year_avg_hours]
              ,[imp_plan_year_avg_hours]
              ,[classification_id]
              ,[aca_status_id]
              ,[ResourceId]
             FROM dbo.employee
            WHERE 
            [FirstName] LIKE @FirstName OR [LastName] LIKE @LastName

#3


0  

In SSMS, if you right-click Stored Procedures and select New Stored Procedure, you'll get this template generated for you. Notice the BEGIN and END that are missing from your SP:

在SSMS中,如果右键单击“存储过程”并选择“新建存储过程”,则将为您生成此模板。注意SP中缺少的BEGIN和END:

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> 
    -- Add the parameters for the stored procedure here
    <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
    <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO