日期:2014-05-18  浏览次数:20489 次

存储过程查询
以一张表每条记录中的三个字段作为条件,查另一张表,

3个变量,该怎么写?

------解决方案--------------------
select m.* , n.*
from m ,n 
where m.关键字 = n.关键字 and
m.c1 = @变量1 and m.c2 = @变量2 and m.c3 = @变量3
------解决方案--------------------
SQL code

create procedure pr_test
(@v1 int,
@v2 int,
@v3 int
)

as
begin
set nocount on
select b.* from a,b where a.id=b.a_id and b.v1=@V1 AND b.v2=@v2 and b.v3=@v3
end
或者
create procedure pr_test
as
begin
set nocount on
declare @v1 int,@v2 int,@v3 int
select @v1=v1,@v2=v2,@v3=v3 from a where 条件;
select b.* from a,b where a.id=b.a_id and b.v1=@V1 AND b.v2=@v2 and b.v3=@v3
end

------解决方案--------------------
贴数据和结果出来。说的不清楚回答也不会清楚
------解决方案--------------------
三个变量,任意组合,哈哈,花了俺几个钟头才搞定。希望能有用。


create proc up_selectemp
@empName nvarchar(20),
@departNO int,
@positionNO int

as

declare @sql varchar(500)
if(@departNO=0)
begin
declare @s1 varchar(100)
set @s1= ''
end
else
begin
set @s1 = 'and DepartNO='+convert (varchar ,@departNO)
end
if(@positionNO=0)
begin
declare @s2 varchar(100)
set @s2= ''
end
else
begin
set @s2 = ' and PositionNO='+convert (varchar,@positionNO)
end
set @sql=
'select empID, empName,dt.InfoName,p.InfoName,Intime,Tel,Email
from Employee 
join BasicInfo as dt on Employee .DepartNO=dt.InfoID
join basicinfo as p on Employee .PositionNO=p.InfoID
where empName like '+'''%' + @empName + '%''' +@s1+@s2
 
exec(@Sql)
go