将查询条件作为结果输出,求一存储过程
假设:(我说的是假设,如有雷同,那不意思,也有可能是巧合!!!)
-----------------testa-----------------------------------
drop table testa
go
create table testA(id int,nm varchar(20),cityid int ,citynm varchar(50),rearid int ,areanm varchar(50))
insert into testA select 1,'东京A',01,'东京热',1001,'东京热1'
union all
select 2,'东京B',01,'东京热',1001,'东京热1'
union all
select 3,'东京C',01,'东京热',1001,'东京热1'
union all
select 4,'加勒比A',02,'加勒比',1002,'asd'
union all
select 5,'加勒比B',02,'加勒比',1002,'asd'
union all
select 6,'加勒比C',02,'加勒比',1002,'asd'
union all
select 7,'一本道A',03,'一本道',1002,'asd'
union all
select 8,'一本道B',03,'一本道',1002,'asd'
union all
select 9,'一本道C',03,'一本道',1002,'asd'
select * from testa
drop table testB
go
create table testB(id int, nm varchar(50),cityid int,rearid int,nmid int)
insert into testB select 1,'苍井空',1,1001,1
union all
select 2,'武藤兰',1,1001,1
union all
select 3,'饭岛爱',1,1001,1
union all
select 4,'小泽玛莉亚',1,1001,1
union all
select 5,'啊啊啊',2,1002,2
union all
select 6,'嗷嗷啊',4,1002,4
select * from testB
已知条件:city的级别大于area,area大于名称(nm)
(东京) (东京热) (东京A)
这里是查询语句:
select a.nm as '子公司',a.areanm as '子子公司',a.citynm as '公司',b.nm as 'star' from testA a join testB b on a.id = b.nmid
----------------------------------------------
子公司子子公司公司star
东京A东京热1东京热苍井空
东京A东京热1东京热武藤兰
东京A东京热1东京热饭岛爱
东京A东京热1东京热小泽玛莉亚
东京B东京热1东京热啊啊啊
加勒比Aasd加勒比嗷嗷啊
假设我传条件:b.nmid = 1,我希望得到的是:
select a.nm as '子公司',a.citynm as '公司',b.nm as 'star' from testA a join testB b on a.id = b.nmid
where b.nmid = 1
------------得到以nmid的value东京A作为列头-----------------------
子公司公司star
东京A东京热苍井空
东京A东京热武藤兰
东京A东京热饭岛爱
东京A东京热小泽玛莉亚
假设我传条件:b.rearid = 1001,我希望得到的是:
select a.areanm as '子子公司',a.citynm as '公司',b.nm as 'star' from testA a join testB b on a.id = b.nmid
where b.rearid = 1001
------------得到以rearid 的value东京热1作为列头-----------------------
子子公司公司star
东京热1东京热苍井空
东京热1东京热武藤兰
东京热1东京热饭岛爱
东京热1东京热小泽玛莉亚
问题:求一存储过程,能够实现我上面的效果,output 不会用
55555555555555555,求大神的到来!
最后给你添麻烦了:
请执行:
drop table testa
go
drop table testB
go
[最优解释]
create proc pr_name(@str varchar(100))
as
begin
if @str like '%nmid%'
exec('select a.nm as ''子公司'',a.citynm as ''公司'',b.nm as ''star''
from testA a join testB b on a.id = b.nmid where '+@str)
else if @str like '%rearid %'
exec('select a.areanm as ''子子公司'',a.citynm as ''公司'',b.nm as ''star''
from testA a join testB b on a.id = b.nmidwhere '+@str)
end
exec pr_name 'b.rearid = 1001'
[其他解释]
create proc pr_name(@str varchar(100))
as
begin
exec('select a.nm as ''子公司'',a.citynm as ''公司'',b.nm as ''star''
from testA a join testB b on a.id = b.nmid where '+@str)
end
exec pr_name 'b.rearid = 1001'
[其他解释]
谢谢你的回复,这里的“子公司”是不确定的!
假设传来nmid,那么他就是 “子公司”
如果传的是rearid ,对应显示的就是“子子公司”
即:根据条件得到结果!
[其他解释]
其实:我想这样,您帮我看下,怎么实现这个:
alter proc get_test
as
begin
declare @cc varchar(30)
select @cc as '子公司',a.areanm as '子子公司',a.citynm as '公司',b.nm as 'star' from testA a join testB b on a.id = b.nmid
where b.nmid = 1
if 1<>0
set @cc = '123'
end
为社么在if给@cc 赋值失败了???
子公司子子公司公司star
NULL东京热1东京热苍井空
NULL东京热1东京热武藤兰
NULL东京热1东京热饭岛爱
NULL东京热1东京热小泽玛莉亚
[其他解释]
谢谢,搞定了!
我卡在了一个引号的手上了!
这个才是我最重要的效果,但是仍然谢谢我爱罗(ssp2009)
alter proc te(
@nmid varchar(30),
@rearid varchar(30)
)
as
begin
declare @cc varchar(max)
declare @sql varchar(max)
if @nmid <> ''
set @cc = ' a.nm '
if @rearid <> ''
set @cc = ' a.areanm'
set @sql =' select '+ @cc +' as ''子公司或子子公司'',a.citynm as ''公司'',b.nm as ''star'' from testA a join testB b on a.id = b.nmid where 1=1 '
if @nmid <> ''
set @sql += ' and b.nmid =''' + @nmid + ''''
if @rearid <> ''
set @sql += ' and b.rearid =''' + @rearid + ''''
exec (@sql)
end
exec te '','1001'
我的错误想法是这样的:
select nm from testB
然后我生命一个变量:@cc varchar(40)
然后
select @cc from testB
我最初想直接替换,结果在select @cc from testB
一直抱错,所以我就卡住了
现在就是把sql也声明成@sql = ‘select @cc from testB’
就可以了
ps:长时间没有搞,连最基本的都快忘了!
[其他解释]
只有你一个人,那分就都给你吧!