求一个存储过程,大家给帮帮忙啊,多谢了
现在需要一个存储过程,要求是这样的,有一个输入参数input,返回一个2维数组。有两个查询语句,把这两个结果集组成2维数组返回。
例如select * from table1 where cond1=input 得出一个结果集{a,b,c},select * from table2 where cond2=input 得到另一个结果集{d,e,f},然后把这两个结果集组成一个二维数组{(a,b,c),(d,e,f)}返回,不知道说清楚没有,请大家帮忙了,多谢多谢。
------解决方案--------------------create proc sp_test
@input varchar(100)
as
begin
select * from table1 where cond1 = @input
union all
select * from table2 where cond2 = @input
end
------解决方案--------------------SQL Server 没有二维数组概念,你可以转换方式返回结果.
------解决方案--------------------如果列数相同返回一个表不可以么?
------解决方案--------------------create proc sp_test @input varchar(100)
as
begin
if exists (select * from dbo.sysobjects where id = object_id(N '[dbo].[result_table] ') and OBJECTPROPERTY(id, N 'IsUserTable ') = 1)
drop table [dbo].[result_table]
GO
select * into result_table ---如果两个表的列数相同,可以把想要的数据插入到一个表中
from (
select * from table1 where cond1 = @input
union all
select * from table2 where cond2 = @input
) t
end
------解决方案--------------------create table tb1(id int,col varchar(10))
insert tb1 select 1, 'a '
union all select 1, 'b '
union all select 1, 'c '
create table tb2(id int,col varchar(10))
insert tb2 select 1, 'd '
union all select 1, 'e '
union all select 1, 'f '
go
create proc prc1(@input int)
as
begin
declare @s1 varchar(100)
set @s1= ' '
select @s1=@s1++ ', '+col from tb1 where id=@input
select @s1=stuff(@s1,1,1, ' ')
declare @s2 varchar(100)
set @s2= ' '
select @s2=@s2+ ', '+col from tb2 where id=@input
select @s2=stuff(@s2,1,1, ' ')
select '( '+@s1+ '),( '+@s2+ ') '
end
go
create proc prc2(@input int)
as
begin
select * from
(
select col from tb1 where id=@input
union all
select col from tb2 where id=@input
)a
end
go
exec prc1 1
exec prc2 1
drop table tb1,tb2
drop proc prc1,prc2
结果1
----------------
(a,b,c),(d,e,f)
(所影响的行数为 1 行)
结果2
col
----------
a
b
c
d
e
f
(所影响的行数为 6 行)