Click here to monitor SSC
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16568 Visits: 17016
Lynn Pettis (1/25/2013)
Sean Lange (1/25/2013)
Lynn Pettis (1/25/2013)

Sean,

@BatchID should have been @BranchID in the sp_executesql statement. That was a type.

Regarding @params = @Params, quite legal. From MSDN (emphasis is mine, and yes the names can be the same):

[quote]

sp_executesql [ @stmt = ] statement
[
{ , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' }
{ , [ @param1 = ] 'value1' [ ,...n ] }
]



Lynn,

I figured the Branch/Batch was a typo. After I posted I realized it was pretty much a direct copy from what you posted (typo included). The @params = @Params looked so strange to me I just assumed it wouldn't work.

The worst part of this is that not catching the typo is a clear indication that the OP does not have a good grasp of what is going on here. w00t

OP - you need to truly understand the code here before you use it. You are the one who has to support it when you get that call at 3am. I don't think your boss will be too impressed if you tell them you don't understand the code because you used what some person on the internet suggested.


Actually the OP did catch the typo regarding @BatchID/@BranchID.


Good grief!!! I wish it was morning so I could blame lack of coffee...I will blame it on the anticipation of getting a few beers after work today (being Friday).

/me creeps backwards from this one before I make even more of a fool out of myself. :-D

_______________________________________________________________

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)
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5832 Visits: 11405
This modification will make sure you include only actually existing tables and do not miss any of them:

declare @SQLCmd nvarchar(max), @Params nvarchar(max), @DiscDate datetime ;
SET @DiscDate = '20090630';
set @Params = N'@BranchID varchar(10), @DiscDate date';

select
@SQLCmd = ISNULL(@SQLCmd, 'select * from TableCurrent where branchid = @BranchID and (disc_dte is null or disc_dte > @DiscDate)')
+ char(13) + char(10) + 'UNION ALL ' + char(13) + char(10)
+ 'select * from ' + QUOTENAME(O.NAME ) + ' where branchid = @BranchID and (disc_dte is null or disc_dte > @DiscDate)'
-- SELECT *
FROM sys.objects AS O
WHERE O.type = 'U' AND O.name LIKE 'Table[_]20[0-1][0-9]%[_]20[01][0-9]'

print @SQLCmd;

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



And if you want to avoid unnecessary scanning too far in the history you may add an extra condition into the WHERE clause.
It will include only those tables which can actually contain records from the date period you're interersted in:

declare @SQLCmd nvarchar(max), @Params nvarchar(max), @DiscDate datetime ;
SET @DiscDate = '20090630';
set @Params = N'@BranchID varchar(10), @DiscDate date';

select
@SQLCmd = ISNULL(@SQLCmd, 'select * from TableCurrent where branchid = @BranchID and (disc_dte is null or disc_dte > @DiscDate)')
+ char(13) + char(10) + 'UNION ALL ' + char(13) + char(10)
+ 'select * from ' + QUOTENAME(O.NAME ) + ' where branchid = @BranchID and (disc_dte is null or disc_dte > @DiscDate)'
-- SELECT *
FROM sys.objects AS O
WHERE O.type = 'U' AND O.name LIKE 'Table[_]20[0-1][0-9]%[_]20[01][0-9]'
AND O.name >= 'Table_' + CONVERT(nvarchar(128), YEAR(@DiscDate)-1)

print @SQLCmd;

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


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