如何在mysql存储过程中连接两个表

时间:2012-11-22 10:58:56

标签: mysql stored-procedures

我编写了一个存储过程来进行搜索。在下面的存储过程中,我有参数hpw1, aty1, ind1, rol1, tz1和参数id, hpw, aty, ind, rol, tz,我的代码如下所示。

当我调用这个存储过程时,我得到的结果包含多行,我不知道这段代码出错了,所以任何人都可以为这个问题提出解决方案。

存储过程:

DELIMITER $$

CREATE DEFINER=`Compuser`@`%` PROCEDURE `Equity_Search`(IN hpw1 INT,IN aty1 INT, IN ind1 INT, IN rol1 INT, IN tz1 INT,
OUT id INT, OUT hpw INT ,  OUT aty INT , OUT ind INT, OUT rol INT, OUT tz INT
)
BEGIN
select  e.usr_workfor_equity_usr_id,
        e.usr_workfor_equity_hours_per_week,
        e.usr_workfor_equity_assignment_type_id,
        i.my_usr_workfor_industry_id,
        r.my_usr_workfor_role_id,
        e.usr_workfor_equity_timezone_id into id,hpw,aty,ind,rol,tz
from my_usr_workfor_equity e
inner join my_usr_workfor_roles r on e.usr_workfor_equity_usr_id = r. my_usr_workfor_usr_id
inner join my_usr_workfor_industries i on e.usr_workfor_equity_usr_id = i. my_usr_workfor_usr_id 
where  e.usr_workfor_equity_hours_per_week = hpw1hpw1 or 
e.usr_workfor_equity_assignment_type_id = aty1 or 
i.my_usr_workfor_industry_id = ind1 or 
r.my_usr_workfor_role_id = rol1 or 
e.usr_workfor_equity_timezone_id = tz1 ;

END

调用存储过程:

call Equity_Search(6,0,0,0,0,@id,@hpw,@aty,@ind,@rol,@tz);

错误:

  

ErrorCode:1172结果由多行组成。

有些人请建议我解决这个错误,我在代码中哪里出错?

由于

1 个答案:

答案 0 :(得分:1)

试试这个

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("ProcedureName", conn) { 
                       CommandType = CommandType.StoredProcedure }) 
{
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}

首先创建storedprocedure并执行它。然后在存储过程中更改“create to alter”

create ProcedureName 
as
begin
select column1,column2.. from table1 
join table2 on condition
end