【问题标题】:how to excute a Sp with dynamic parameter如何使用动态参数执行 Sp
【发布时间】:2014-02-12 06:01:10
【问题描述】:

我创建了一个存储过程,通过提供员工的收入来查找姓名和职务

Create  PROCEDURE GetEmployeessalaryInOutputVariable
(
    @Income INT,                 -- Input parameter,  Income of the Employee
    @FirstName VARCHAR (30) OUT, -- Output parameter to collect the Employee name
    @Title VARCHAR (30)OUT       -- Output Parameter to collect the Employee designation
)
AS
BEGIN
SELECT FirstName=@FirstName, Title=@Title 
    FROM Salaries WHERE @Income=Income
END

在此之后,我尝试按如下方式执行 Sp

Declare @FirstName as varchar(30) -- Declaring the variable to collect the Employeename
Declare @Title as varchar(30)     -- Declaring the variable to collect the Designation
Execute GetEmployeessalaryInOutputVariable 500 , @FirstName output, @Title output
select @FirstName,@Title as Designation   

上面的语句一写就报错

Invalid object name GetEmployeessalaryInOutputVariable

为什么程序已经创建并存在,但它的行为是这样的?

另外,如何运行查询以获得正确的结果?

【问题讨论】:

  • 能否提供格式化后的代码?

标签: sql stored-procedures dynamic execute declare


【解决方案1】:
Execute GetEmployeessalaryInOutputVariable 500 , 'FirstName', 'Title'

Declare @FirstName as varchar(30)
set @FirstName = 'FirstName'
Declare @Title as varchar(30)   
set @Title = 'title'
Execute GetEmployeessalaryInOutputVariable 500 , @FirstName, @Title 

【讨论】:

    【解决方案2】:
    Create  PROCEDURE GetEmployeessalaryInOutputVariable
    (
        @Income INT,                 -- Input parameter,  Income of the Employee
        @FirstName VARCHAR (30) OUT, -- Output parameter to collect the Employee name
        @Title VARCHAR (30)OUT       -- Output Parameter to collect the Employee designation
    )
    AS
    BEGIN
    SELECT @FirstName = FirstName, @Title=Title 
        FROM Salaries WHERE @Income=Income
    END
    

    你的SP应该是这样的

    【讨论】:

    • 谢谢你,你给的 Sp 是正确的,但问题是它只显示一行,而有 6 行收入 500 请提出任何解决方案
    • @ashu 您应该在 SP Execute 从表中获取数据后更改您的 SP 以插入到表中 insert into <table_name> SELECT FirstName, Title FROM Salaries WHERE @Income=Income ....
    猜你喜欢
    • 2011-09-30
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多