| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2008-10-07 : 11:20:12
|
| Hi,Currently I have a query written (thx to peso! ) as seen below.I know that dynamic WHERE clauses are a no-no usually in SQL, but I think this case is different.As you can see in the query below, the WHERE clause essentially depends on what information is populated into the temp table 'friends'I would like to have a few different ways of populating this table (different select statements, that query different tables)Is it ok to pass a parameter to this SPROC, that will determine how to fill the tables? There will be about 5 different options.I'm fully preparded to make it just 5 different queries, if it will mean better execution plan and performance..... That's what Im used to doing, so thought I would double check here first...any thoughts appreciated !thanks again!Mike123CREATE PROCEDURE dbo.uspGetUserActivityList( @userID INT)ASSET NOCOUNT ONDECLARE @Friends TABLE ( friendID INT PRIMARY KEY CLUSTERED, nameOnline VARCHAR(15) )INSERT @Friends ( friendID, nameOnline )SELECT f.friendID, u.nameOnlineFROM Friends AS fINNER JOIN Users AS u ON u.userID = f.friendIDWHERE f.userID = @UserID-- Present the resultsetSELECT f.nameOnline + ' added a new photo ' + CONVERT(CHAR(19), ep.maxDate, 120) AS msg, ep.MaxDate AS theDate, 1 AS theTypeFROM @Friends AS fINNER JOIN ( SELECT userID, MAX(photoDate) AS maxDate FROM ExtraPhotos GROUP BY userID ) AS ep ON ep.userID = f.friendIDUNION ALLSELECT f.nameOnline + ' added a new friend ' + CONVERT(CHAR(19), u.maxDate, 120) AS msg, u.MaxDate AS theDate, 2 AS theTypeFROM @Friends AS fINNER JOIN ( SELECT userID, MAX(dateAdded) AS maxDate FROM Friends GROUP BY userID ) AS u ON u.userID = f.friendIDORDER BY theType, theDate DESC |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-07 : 11:56:58
|
| which WHERE clause you're talking about? i can see only single WHERE clause but didnt understand what you mean by dynamicity in it. can you explain? |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2008-10-07 : 12:07:12
|
| Hi Visakh15,I guess my topic title is misguiding. It's not really a dynamic where clause, as its not just the WHERE clause thats changing, its the whole select statement as seen above. ( i just bolded it) I would like to have a few options for this select statement ( selecting from diff tables etc) This effectively controls my where clause as its later used in an INNER JOIN.Thanks for your help!Mike123 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-07 : 12:20:01
|
| seems like what you want is set of if ...else statements and based on condition you need INSERT..SELECT inside each with tables varying based on situation. |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2008-10-07 : 12:22:25
|
quote: Originally posted by visakh16 seems like what you want is set of if ...else statements and based on condition you need INSERT..SELECT inside each with tables varying based on situation.
yes thats what I want, but my question is whether this is ok for performance reasons ? I am worried about the execution plan.Thanks again!,mike123 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-07 : 12:26:22
|
| whats the basic difference between queries for various conditions/ is it just one of table that chnages? |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-10-07 : 12:32:49
|
Well, I might not be 100% on the money here but.....You could either have a IF / THEN / ELSE section with different SELECTS / INSERTS etc. depending on your inputs. If you did this then I think it would be advisable to create the procedure with the hint (WITH RECOMPILE) as your second run may have no use for the execution plan delivered by the first.ORUse an if then else section and inside each logical block call another sp to do the work. That way that child sp would have a consistent execution plan. Something likeIF @condition1 = 1 EXEC myStoredProcAIF @condition1 = 2 EXEC myStoredProcbELSE EXEC myStoredProcc That way I think you could still benefit from a properly tuned plan. I might be being naive and if I am I'm sure someone will be able to point it out! :)-------------Charlie |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2008-10-07 : 12:36:59
|
quote: Originally posted by visakh16 whats the basic difference between queries for various conditions/ is it just one of table that chnages?
yes, just populating the table from different sources.. |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2008-10-07 : 12:37:50
|
| perhaps I'm just better writing a few different SPROCS ? It's not the end of the world if I do have to ... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-07 : 13:04:38
|
quote: Originally posted by mike123 perhaps I'm just better writing a few different SPROCS ? It's not the end of the world if I do have to ...
that might prove to be the best solution considering the maintainability aspect. |
 |
|
|
|