How To Build A Procedure That Returns Different Numbers Of Columns As A Result Based On A Parameter
Nov 23, 2006
/*
Subject: How to build a procedure that returns different
numbers of columns as a result based on a parameter.
You can copy/paste this whole post in SQL Query Analyzer
or Management Studio and run it once you've made sure
there is no harmful code.
Currently we have several stored procedures which final
result is a select with several joins that returns many
columns (150 in one case, maybe around 50 the average).
We have analyzed our application and found out that most
of the time not all the columns are used. We have
identified 3 different sets of columns needed in
different parts of the application.
Let's identify and name these sets as:
1- simple set, return the employee list for example
2- common set, return the employee information (which
include the simple set)
3- extended set, return the employee information (which
inlude the common set which itself includes the simple
set) + additional information from other tables, maybe
even some SUM aggregates and so on (I don't know for
example, how much sales the employee did so far).
So the bigger sets contain the smaller ones. Please keep
reading all the way to the bottom to better understand
technically what we are trying.
Here is a code sample of how our current procedures
work. Please note that the passing parameter we can either
pass a Unique Identifier (PK) to retrieve a single record,
or if we pass for example -1 or NULL we retrieve all the
employee records.
*/
create table a ( apk int primary key, af1 int, af2 int, af3 int, af4
int, af5 int, af6 int)
create table b ( bpk int primary key, bf1 int, bf2 int, bf3 int, bf4
int, bf5 int, bf6 int)
create table c ( cpk int primary key, cf1 int, cf2 int, cf3 int, cf4
int, cf5 int, cf6 int)
create table d ( dpk int primary key, df1 int, df2 int, df3 int, df4
int, df5 int, df6 int)
insert a values (1,1111,1112,1113,1114,1115,1116)
insert a values (2,1211,1212,1213,1214,1215,1216)
insert a values (3,1311,1312,1313,1314,1315,1316)
insert a values (4,1411,1412,1413,1431,1415,1416)
insert a values (5,1511,1512,1513,1514,1515,1516)
insert a values (6,1611,1612,1613,1614,1615,1616)
insert b values (1,2111,2112,2113,2114,2115,2116)
insert b values (2,2211,2212,2213,2214,2215,2216)
insert b values (3,2311,2312,2313,2314,2315,2316)
insert b values (4,2411,2412,2413,2431,2415,2416)
insert b values (5,2511,2512,2513,2514,2515,2516)
insert b values (6,2611,2612,2613,2614,2615,2616)
insert c values (1,3111,3112,3113,3114,3115,3116)
insert c values (2,3211,3212,3213,3214,3215,3216)
insert c values (3,3311,3312,3313,3314,3315,3316)
insert c values (4,3411,3412,3413,3431,3415,3416)
insert c values (5,3511,3512,3513,3514,3515,3516)
insert c values (6,3611,3612,3613,3614,3615,3616)
insert d values (1,4111,4112,4113,4114,4115,4116)
insert d values (2,4211,4212,4213,4214,4215,4216)
insert d values (3,4311,4312,4313,4314,4315,4316)
insert d values (4,4411,4412,4413,4431,4415,4416)
insert d values (5,4511,4512,4513,4514,4515,4516)
insert d values (6,4611,4612,4613,4614,4615,4616)
go
create procedure original_proc @pk int as
if @pk = -1
set @pk = null
select
a.af1, a.af2, a.af3, a.af4, b.bf1, b.bf2, b.bf3, b.bf4, c.cf1, c.cf2,
c.cf3, c.cf4, d.df1, d.df2, d.df3, d.df4
from
a
join b on a.apk = b.bpk
join c on b.bpk = c.cpk
join d on c.cpk = d.dpk
where
a.apk = ISNULL(@pk, a.apk)
go
exec original_proc 1
go
/*
Currently the above SP is a single SP that is basically
returning ALL possible needed data. However most of the
time we might need to call and retrieve a simple employee
list.
So we thought about modifying the stored procedure by
adding an extra parameter that will indicate which set
of columns to return.
For modifying the stored procedure in order to get a
variable name of columns returned and avoiding
repeating code, we built 4 objects: the stored
procedure being called, one table function and 2 views.
One table function so that we are able to pass a parameter.
The views since they do not accept parameters they are
always joined at least with the inline table function.
The stored procedure generates in its body a dynamic
SQL statement, where it queries the table function and
the views, depending which set is required. Here is a
code sample of our current design (you need to run the
previous code in order for this to work).
*/
create function _1_set(@pk int)
returns table
as return
(
select a.apk, a.af1, a.af2, a.af3, a.af4, b.bf1, b.bf2
from a
join b on a.apk = b.bpk
where a.apk = ISNULL(@pk, a.apk)
)
go
create view _2_set as
select b.bpk, b.bf3, b.bf4, c.cf1, c.cf2
from b
join c on b.bpk = c.cpk
go
create view _3_set as
select c.cpk, c.cf3, c.cf4, d.df1, d.df2, d.df3, d.df4
from c
join d on c.cpk = d.dpk
go
create procedure new_proc @pk int, @set int as
declare @sql nvarchar(4000)
if @pk = -1
set @pk = null
set @sql = 'select * from _1_set(@pk) fs '
if @set 1
set @sql = @sql + 'join _2_set ss on fs.apk = ss.bpk '
if @set 2
set @sql = @sql + 'join _3_set ts on ss.bpk = ts.cpk '
exec sp_executesql @sql, N'@pk int', @pk
go
exec new_proc 1, 3
go
/*
For executing the new procedure, we pass parameter 1
for the smaller set, 2 for the medium size set or 3
for the complete set.
For example when we want to retrieve the common set
we pass the Unique Identifier of the employee to the
SP and then we pass the type of set we want to use
as the second parameter (1 for simple set, 2 for
common set and 3 for extended set).
The SP has the IF and dynamic SQL to add more JOINs.
We would like to know what you think of this approach
and if you know a simpler way of doing it.
For cleaning up the test objects run the following code.
*/
drop procedure original_proc
drop procedure new_proc
drop function _1_set
drop view _2_set
drop view _3_set
drop table a
drop table b
drop table c
drop table d
As always I would appreciate any feedback, opinion,
comments, ideas and suggestions.
Thank you
View 9 Replies
Dec 20, 2007
I have a query that will return one record as its results if you provide two variables: @login and @record_date. This works great if you only want one result. However, now what I want to do is not provide those variables and get the result set back for each login and record_date combination. The hitch is that there are several other variables that are built off of the two that are supplied. Here is the query:
DECLARE @login char(20), /*This sets the rep for the query.*/
@record_date datetime, /*This is the date that we want to run this for.*/
@RWPY decimal(18,2), /*This is the required wins per year.*/
@OCPW decimal(18,2), /*This is the opportunities closed per week.*/
@OACW decimal(18,2), /*This is opportunities advanced to close per week.*/
@TOC decimal(18,2), /*This is the total number of opportunities in close.*/
@OANW decimal(18,2), /*This is opportunities advanced to negotiate per week.*/
@TON decimal(18,2), /*This is the total number of opportunities in negotiate.*/
@OADW decimal(18,2), /*This is the opportunities advanced to demonstrate per week*/
@TOD decimal(18,2), /*This is the total number of opportunities in demonstrate.*/
@OAIW decimal(18,2), /*This is the opportunities advanced to interview per week.*/
@TOI decimal(18,2), /*This is the total number of opportunities in interview.*/
@OCW decimal(18,2), /*This is the opportunities created per week.*/
@TOA decimal(18,2) /*This is the total number of opportunities in approach.*/
SET @login = 'GREP'
SET @record_date = '12/18/2007'
SET @RWPY = (SELECT ((SELECT annual_quota FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)/(SELECT target_deal FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)))
SET @OCPW = (SELECT @RWPY/weeks FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @OACW = (SELECT @OCPW/cls_perc_adv FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @TOC = (SELECT @OACW*(cls_time/7) FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @OANW = (SELECT @OACW/neg_perc_adv FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @TON = (SELECT @OANW*(neg_time/7) FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @OADW = (SELECT @OANW/dem_perc_adv FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @TOD = (SELECT @OADW*(dem_time/7) FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @OAIW = (SELECT @OADW/int_perc_adv FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @TOI = (SELECT @OAIW*(int_time/7) FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @OCW = (SELECT @OAIW/app_perc_adv FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SET @TOA = (SELECT @OCW*(app_time/7) FROM #pipelinehist WHERE loginname = @login AND record_date = @record_date)
SELECT loginname,
CAST(@TOA AS decimal(18,1)) AS [Opps in Approach],
app_time AS [Approach Average Time],
app_perc_adv AS [Approach Perc Adv],
CAST(@TOI AS decimal(18,1)) AS [Opps in Interview],
int_time AS [Interview Average Time],
int_perc_adv AS [Interview Perc Adv],
CAST(@TOD AS decimal(18,1)) AS [Opps in Demonstrate],
dem_time AS [Demonstrate Average Time],
dem_perc_adv AS [Demonstrate Perc Adv],
CAST(@TON AS decimal(18,1)) AS [Opps in Negotiate],
neg_time AS [Negotiate Average Time],
neg_perc_adv AS [Negotiate Perc Adv],
CAST(@TOC AS decimal(18,1)) AS [Opps In Close],
cls_time AS [Close Average Time],
cls_perc_adv AS [Close Perc Adv]
FROM #pipelinehist
WHERE loginname = @login AND record_date = @record_date
Here is some sample data to use with this. With this sample data what I want to get back is a total of 30 records in the result set each with its data specific to the login and record_date of that returned record.
CREATE TABLE #pipelinehist (
glusftboid int IDENTITY(1,1) NOT NULL,
record_date datetime NOT NULL,
loginname char(20) NOT NULL,
app_new float NOT NULL,
app_time float NOT NULL,
app_perc_adv float NOT NULL,
int_time float NOT NULL,
int_perc_adv float NOT NULL,
dem_time float NOT NULL,
dem_perc_adv float NOT NULL,
neg_time float NOT NULL,
neg_perc_adv float NOT NULL,
cls_time float NOT NULL,
cls_perc_adv float NOT NULL,
target_deal money NOT NULL,
annual_quota money NOT NULL,
weeks int NOT NULL
) ON [PRIMARY]
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'AREP', 56.8, 26.9, 0.57, 29.5, 0.47, 20, 0.67, 80.7, 0.53, 2.1, 0.97, 2194.93, 575000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'BREP', 33.2, 0.5, 0.9, 7.7, 0.77, 8, 0.77, 9.2, 0.6, 7.7, 0.64, 971.1, 330000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'CREP', 210.2, 0.3, 0.87, 6.6, 0.5, 13.7, 0.4, 16.3, 0.43, 1.5, 0.91, 461.25, 330000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'DREP', 47.6, 5, 0.53, 33.3, 0.6, 57.5, 0.53, 50, 0.7, 1.5, 1, 2045.7, 575000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'EREP', 75.3, 110.9, 0.47, 36, 0.5, 17.4, 0.87, 20.3, 0.6, 7.2, 0.83, 2021.74, 775000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'FREP', 17.2, 23.3, 0.73, 6.8, 0.8, 6.3, 0.93, 29.7, 0.67, 15.5, 0.83, 2218.95, 575000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'GREP', 105.4, 67, 0.2, 32.9, 0.43, 18.5, 0.67, 8.9, 0.77, 3.5, 0.93, 1838.91, 400000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'HREP', 116.4, 118.5, 0.33, 30.9, 0.77, 46.3, 0.77, 46.3, 0.6, 0.9, 0.97, 1735.13, 1150000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'IREP', 143.3, 9, 0.77, 96, 0.17, 21.6, 0.77, 39.9, 0.43, 0.9, 0.93, 1385.43, 400000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'JREP', 179.4, 66.7, 0.7, 67.6, 0.1, 41.4, 0.6, 20.2, 0.8, 14, 0.7, 1563.76, 330000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'KREP', 107.6, 38.2, 0.23, 47.5, 0.47, 21.3, 0.77, 9.6, 0.73, 2.1, 0.83, 2120, 575000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'LREP', 18.6, 8.3, 0.87, 23.2, 0.57, 2.6, 0.87, 12.2, 0.67, 1, 1, 1229.02, 330000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'MREP', 4, 46.2, 0.6, 26.7, 0.57, 8.1, 0.87, 1.7, 0.9, 1.4, 1, 1091.22, 350000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'NREP', 54, 21.6, 0.57, 1.7, 0.77, 11, 0.8, 7.4, 0.9, 49, 0.47, 3240.68, 1300000, 50)
INSERT into #pipelinehist VALUES ('12/17/2007 0:00', 'OREP', 37.6, 24.4, 0.57, 50.1, 0.43, 6.7, 0.87, 15.6, 0.73, 0.9, 0.97, 1163.48, 330000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'AREP', 57.2, 32.5, 0.6, 29.5, 0.47, 20, 0.67, 85.6, 0.5, 2.1, 0.97, 2194.93, 575000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'BREP', 33.9, 0.5, 0.93, 7.8, 0.73, 8.3, 0.77, 9.2, 0.6, 7.7, 0.64, 971.1, 330000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'CREP', 152.1, 0, 0.87, 4.3, 0.67, 9.7, 0.47, 15.7, 0.47, 1.8, 0.85, 396.43, 330000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'DREP', 80.5, 9.8, 0.5, 40.7, 0.57, 68.3, 0.43, 64.2, 0.57, 1.5, 1, 2045.7, 575000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'EREP', 61, 92.1, 0.5, 31, 0.53, 16.9, 0.83, 17.7, 0.6, 7.3, 0.83, 2318.04, 775000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'FREP', 19.4, 21.1, 0.7, 5.3, 0.77, 2.2, 0.93, 33.3, 0.7, 9.7, 0.87, 1937.17, 575000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'GREP', 81.7, 40.5, 0.3, 33, 0.37, 18.5, 0.67, 8.9, 0.77, 3.5, 0.93, 1838.91, 400000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'HREP', 128.6, 115.7, 0.3, 30.9, 0.77, 46.3, 0.77, 48.8, 0.6, 0.9, 0.97, 1728.29, 1150000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'IREP', 100.9, 3.4, 0.77, 86.2, 0.27, 18, 0.8, 54.7, 0.37, 0.9, 0.93, 1385.43, 400000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'JREP', 179.4, 66.7, 0.7, 63.5, 0.1, 41.4, 0.6, 20.2, 0.8, 14, 0.7, 1563.76, 330000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'KREP', 285.2, 36.5, 0.1, 46, 0.43, 24.2, 0.73, 9.6, 0.73, 2.1, 0.83, 2120, 575000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'LREP', 17.6, 7.3, 0.9, 21.5, 0.57, 1.7, 0.87, 12.2, 0.67, 1, 1, 1250.54, 330000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'MREP', 26.7, 46.2, 0.6, 26.7, 0.57, 8.1, 0.87, 1.7, 0.9, 1.3, 1, 979.7, 350000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'NREP', 61.6, 20.8, 0.5, 1.7, 0.77, 11, 0.8, 7.4, 0.9, 49, 0.47, 3240.68, 1300000, 50)
INSERT into #pipelinehist VALUES ('12/18/2007 0:00', 'OREP', 31.6, 16.9, 0.63, 50.1, 0.43, 7.2, 0.87, 19.5, 0.7, 0.9, 0.97, 1303.48, 330000, 50)
View 3 Replies
View Related