SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Changing from "hard-coded" to dynamic query


Changing from "hard-coded" to dynamic query

Author
Message
momba
momba
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 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.
toddasd
toddasd
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1338 Visits: 3820
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.
momba
momba
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 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.
toddasd
toddasd
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1338 Visits: 3820
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.
momba
momba
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 49
Ohhhhh (duh). That makes sense.

Thank you!!!! :-D
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96955 Visits: 38988
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;




Cool
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)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63995 Visits: 17974
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 Modens 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)
momba
momba
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 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() )
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96955 Visits: 38988
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';




Cool
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)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96955 Visits: 38988
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.

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search