I have two select
statements in my stored procedure:
alter proc multiple
select * from table-one
select * from table-two
Now how to get the data of table-one only by executing the stored procedure?
You can pass input variable and use if statment. For example:
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
And execution of procedure:
EXECUTE multiple @choice = 1 -- to use 1st select
EXECUTE multiple @choice = 2 -- to use 2st select
EXECUTE multiple @choice = 3 -- to use both selects
You can use TEMP table to fill all result in the temp table.
if you have 3 table name tab_1,tab_2,tab_3 then create a temp table with column maximum from these table(tab_1,tab_2,tab_3) and add a extra column to temp table to identify data from tables.
tab_1(id bigint,name varchar(50))
tab_2(id bigint,email varchar(50))
tab_3(id bigint,address varchar(50),phone varchar(50))
then your temp table should be like this
#tmp(col1 bigint(),col2 varchar(50),col3 varchar(50),from_table varchar(50))
eg
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)
and the Sp should be like
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.