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 1:25 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 13,272, Visits: 12,103
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.

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.


_______________________________________________________________

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 #1411920
Posted Tuesday, February 12, 2013 7:22 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:16 PM
Points: 4,576, Visits: 8,347
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;

Post #1419267
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse