Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Changing from "hard-coded" to dynamic query Expand / Collapse
Author
Message
Posted Friday, January 25, 2013 7:36 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:52 AM
Points: 19, Visits: 49
Hi. My name is Momba and I'm a T-SQL noob....

So I've got the following hard-coded query:
select * TableCurrent 
WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)
union
select * from Table_2007_2008
WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)
union
select * from Table_2008_2009
WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)
union
select * from Table_2009_2010
WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)
union
select * from Table_2010_2011
WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)
union
select * from Table_2011_2012
WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)

At the end of each fiscal year (June 30th) a new archive is made of the current table and I end up having to dig through everything to get what I need. And this is my best attempt to making it dynamic:

Use TableCurrent

Declare @loopYrbeg int
Declare @loopYrend int
Declare @tablename sysname
Declare @SQL varchar(MAX)

Set @loopYrbeg = 2007

begin

While @loopYrbeg < year( getdate() )

begin

Set @loopYrend = @loopYrbeg + 1
Set @tablename = 'Table_' + CONVERT(varchar(4), @loopYrbeg) + '_' + CONVERT(varchar(4), @loopYrend)
Set @sql = 'select *
from ' + @tablename +char(10)+
'WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)'+char(10)+
'union'+char(10)

Set @loopYrbeg = @loopYrbeg + 1

end

exec (@sql)

select * TableCurrent
WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)

end



...Unfortunately my best attempt doesn't work as it should. Please advise. Thank you.
Post #1411721
Posted Friday, January 25, 2013 8:04 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 2:16 PM
Points: 477, Visits: 3,673
This will fix what you have:

Declare @loopYrbeg int
Declare @loopYrend int
Declare @tablename sysname
Declare @SQL varchar(MAX)

Set @loopYrbeg = 2007
Set @sql = ''

begin
While @loopYrbeg < year( getdate() )
begin
Set @loopYrend = @loopYrbeg + 1
Set @tablename = 'Table_' + CONVERT(varchar(4), @loopYrbeg) + '_' + CONVERT(varchar(4), @loopYrend)
Set @sql = @sql + 'select * from ' + @tablename +char(10)+
'WHERE BRANCHID=''950'' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)' + char(10) +
'union' + char(10)
Set @loopYrbeg = @loopYrbeg + 1
end
set @SQL = left(@sql, LEN(@sql) - 6)
print (@sql)

end



______________________________________________________________________________
How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
Post #1411744
Posted Friday, January 25, 2013 8:34 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:52 AM
Points: 19, Visits: 49
Yes, this is getting really close (thank you)... but how do I union the current table with all the archives?

i.e.

select * TableCurrent
WHERE BRANCHID='950' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630


...which doesn't have a fiscal year in its name.
Post #1411763
Posted Friday, January 25, 2013 8:52 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 2:16 PM
Points: 477, Visits: 3,673
Just initialize the string with that query, like so:

Declare @loopYrbeg int
Declare @loopYrend int
Declare @tablename sysname
Declare @SQL varchar(MAX)

Set @loopYrbeg = 2007
Set @sql = 'select * from tableCurrent ' + char(10) +
'WHERE BRANCHID=''950'' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)' + char(10) +
'union' + char(10);

While @loopYrbeg < year( getdate() )
begin
Set @loopYrend = @loopYrbeg + 1;
Set @tablename = 'Table_' + CONVERT(varchar(4), @loopYrbeg) + '_' + CONVERT(varchar(4), @loopYrend);
Set @sql = @sql + 'select * from ' + @tablename +char(10)+
'WHERE BRANCHID=''950'' AND (DISC_DTE IS NULL OR DISC_DTE > 20070630)' + char(10) +
'union' + char(10);
Set @loopYrbeg = @loopYrbeg + 1;
end
set @SQL = left(@sql, LEN(@sql) - 6);
print (@sql);



______________________________________________________________________________
How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
Post #1411772
Posted Friday, January 25, 2013 9:02 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:52 AM
Points: 19, Visits: 49
Ohhhhh (duh). That makes sense.

Thank you!!!!
Post #1411783
Posted Friday, January 25, 2013 9:16 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:34 PM
Points: 20,676, Visits: 32,269
This:


declare @StartYear int = 2007,
@SQLCmd nvarchar(max);

with quickTally(n) as (select top(year(dateadd(mm,6,getdate())) - @StartYear - 1) n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))dt(n))
select
@SQLCmd = stuff((select char(13) + char(10) +
'select * from Table_' +
cast(@StartYear + n as varchar) + '_' + cast(@StartYear + n + 1 as varchar) +
' where branchid = ''950'' and (disc_dte is null or disc_dte > ''20070630'') union all'
from
quickTally
for xml path(''),TYPE).value('.','nvarchar(max)'),1,2,'') +
char(13) + char(10) + 'select * from TableCurrent where branchid = ''950'' and (disc_dte is null or disc_dte > ''20070630'');'
;
print @SQLCmd;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1411792
Posted Friday, January 25, 2013 9:20 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:34 PM
Points: 12,923, Visits: 12,342
Depending on how often you will be running this query you might consider using sp_executesql instead of the more generic exec. It will cache your execution plan and more importantly it allows for parameters.

http://www.sqlskills.com/blogs/kimberly/exec-and-sp_executesql-how-are-they-different/


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1411794
Posted Friday, January 25, 2013 9:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:52 AM
Points: 19, Visits: 49
... I came across one last issue. How can I make the " While @loopYrbeg < year ( getdate() )" statement conditional?

More specifically IF month ( getdate() ) < 7
THEN @loopYrbeg < year ( getdate() ) - 1
ELSE @loopYrbeg < year ( getdate() )

Post #1411798
Posted Friday, January 25, 2013 9:31 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:34 PM
Points: 20,676, Visits: 32,269
Sean Lange (1/25/2013)
Depending on how often you will be running this query you might consider using sp_executesql instead of the more generic exec. It will cache your execution plan and more importantly it allows for parameters.

http://www.sqlskills.com/blogs/kimberly/exec-and-sp_executesql-how-are-they-different/


This:


declare @StartYear int = 2007,
@SQLCmd nvarchar(max),
@Params nvarchar(max);
set @Params = N'@BranchID varchar(10), @DiscDate date');

with quickTally(n) as (select top(year(dateadd(mm,6,getdate())) - @StartYear - 1) n from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))dt(n))
select
@SQLCmd = stuff((select char(13) + char(10) +
'select * from Table_' +
cast(@StartYear + n as varchar) + '_' + cast(@StartYear + n + 1 as varchar) +
' where branchid = @BranchID and (disc_dte is null or disc_dte > @DiscDate) union all'
from
quickTally
for xml path(''),TYPE).value('.','nvarchar(max)'),1,2,'') +
char(13) + char(10) + 'select * from TableCurrent where branchid = @BranchID and (disc_dte is null or disc_dte > @DiscDate);'
;
print @SQLCmd;

exec sp_executesql @SQLCmd, @params = @Params, @BatchID = '950', @DiscDate = '20070630';





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1411800
Posted Friday, January 25, 2013 9:31 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:34 PM
Points: 20,676, Visits: 32,269
momba (1/25/2013)
... I came across one last issue. How can I make the " While @loopYrbeg < year ( getdate() )" statement conditional?

More specifically IF month ( getdate() ) < 7
THEN @loopYrbeg < year ( getdate() ) - 1
ELSE @loopYrbeg < year ( getdate() )



No loops required.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1411801
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse