我的存储过程中有两个select
语句:
alter proc multiple
select * from table-one
select * from table-two
现在如何仅通过执行存储过程来获取表1的数据?
答案 0 :(得分:0)
您可以传递输入变量并使用if语句。例如:
ALTER PROCEDURE multiple
@choice INT
AS
BEGIN
IF (@choice = 1)
BEGIN
SELECT * FROM Table1
END
IF (@choice = 2)
BEGIN
SELECT * FROM Table2
END
IF (@choice = 3)
BEGIN
SELECT * FROM Table1
SELECT * FROM Table2
END
END
执行程序:
EXECUTE multiple @choice = 1 -- to use 1st select
EXECUTE multiple @choice = 2 -- to use 2st select
EXECUTE multiple @choice = 3 -- to use both selects
答案 1 :(得分:0)
您可以使用TEMP表填充临时表中的所有结果。
如果您有3个表名tab_1,tab_2,tab_3,则从这些表(tab_1,tab_2,tab_3)创建一个最大列的临时表,并在临时表中添加一个额外的列以标识表中的数据。
tab_1(id bigint,name varchar(50))
tab_2(id bigint,email varchar(50))
tab_3(id bigint,address varchar(50),phone varchar(50))
那么你的临时表应该是这样的
#tmp(col1 bigint(),col2 varchar(50),col3 varchar(50),from_table varchar(50))
e.g
create table tab_1
(
id bigint identity(1,1),
name varchar(50),
email varchar(50)
)
insert into tab_1(name,email) values
('a','a@mail.com'), ('b','c@mail.com'),
('a1','a1@mail.com'), ('a2','a2@mail.com'),
('a3','a3@mail.com'), ('a4','a4@mail.com'),
('b1','b1@mail.com'),('b2','b2@mail.com')
create table tab_2
(
id bigint identity(1,1),
name varchar(50),
email varchar(50),
amount decimal(18,2)
)
insert into tab_2(name,email,amount) values
('a','a@mail.com',12.5), ('b','c@mail.com',11.6),
('a1','a1@mail.com',11.7), ('a2','a2@mail.com',88.9),
('a3','a3@mail.com',90), ('a4','a4@mail.com',45),
('b1','b1@mail.com',78),('b2','b2@mail.com',88)
和Sp应该像
create table #tab(col1 bigint,
col2 varchar(50),
col3 varchar(50),col4 varchar(50),table_from varchar(50))
insert into #tab(col1,col2,col3,table_from)
select id,name,email,'table_1' from tab_1
insert into #tab(col1,col2,col3,col4,table_from)
select id,name,email,amount,'table_2' from tab_2
select * from #tab