Click here to Skip to main content
15,917,652 members
Home / Discussions / Database
   

Database

 
Questiongetting second highest date from a sql table Pin
hkchauhan26-Jul-07 2:16
hkchauhan26-Jul-07 2:16 
AnswerRe: getting second highest date from a sql table Pin
Krish - KP26-Jul-07 2:21
Krish - KP26-Jul-07 2:21 
AnswerRe: getting second highest date from a sql table Pin
hk7792126-Jul-07 2:34
hk7792126-Jul-07 2:34 
Questioninserting values by checking the existing values Pin
dsaikrishna26-Jul-07 2:12
dsaikrishna26-Jul-07 2:12 
AnswerRe: inserting values by checking the existing values Pin
Eliz.k27-Jul-07 0:20
Eliz.k27-Jul-07 0:20 
QuestionHiding Report Parameters. Pin
Karuna Kumar26-Jul-07 0:34
Karuna Kumar26-Jul-07 0:34 
QuestionStored Procedure Issue Pin
Brendan Vogt26-Jul-07 0:00
Brendan Vogt26-Jul-07 0:00 
AnswerRe: Stored Procedure Issue Pin
Mike Dimmick26-Jul-07 0:46
Mike Dimmick26-Jul-07 0:46 
I would strongly recommend constructing a parameterized query in your client code and using that, rather than a stored procedure.

You occasionally find recommendations to use 'OR @paramName IS NULL' to make a parameter optional. This works in the sense that the correct results are returned, but it causes problems for the query optimizer. In SQL Server 2000 SP4, I observed that for a query that looked like
SELECT * FROM table
WHERE 
   ( ColumnA = @columnA OR @columnA IS NULL ) AND
   ( ColumnB = @columnB OR @columnB IS NULL )
that the optimizer actually picked an index on ColumnB when you specified only @columnA, and one on ColumnA when you specified only @columnB! That is, it scanned the wrong index, rather than seeking on the right one.

SQL Server 2005 at least picked the right index and index operation, but you have a new problem: the query plan for a stored procedure is computed the first time it is run, based on the arguments supplied for that run, and the resulting procedure is cached and reused until it ages out of the cache. If, the first time, you supply column A but not B, it picks the index on column A and caches this plan. If you then supply column B but not A, it tries to use the cached plan, eventually realises that the plan isn't workable and reverts to scanning the index and looking up in the base table - just doing a table scan would have been quicker. You can work around this by specifying WITH RECOMPILE on an EXEC statement, which recompiles the procedure just for that run (the previously cached plan is retained), or by specifying WITH RECOMPILE on the CREATE PROCEDURE statement, which means it's recompiled every time.

If you must use a stored procedure, use it as a shell - call sp_executesql with a dynamically-constructed query. The reason I recommend doing it in the client code is that C# and VB's string manipulation support is much better than T-SQL's.

Stability. What an interesting concept. -- Chris Maunder

GeneralRe: Stored Procedure Issue Pin
Colin Angus Mackay26-Jul-07 0:52
Colin Angus Mackay26-Jul-07 0:52 
AnswerRe: Stored Procedure Issue Pin
Colin Angus Mackay26-Jul-07 0:48
Colin Angus Mackay26-Jul-07 0:48 
GeneralRe: Stored Procedure Issue Pin
Mike Dimmick26-Jul-07 3:05
Mike Dimmick26-Jul-07 3:05 
GeneralRe: Stored Procedure Issue Pin
Brendan Vogt26-Jul-07 3:10
Brendan Vogt26-Jul-07 3:10 
AnswerRe: Stored Procedure Issue Pin
Aaron VanWieren26-Jul-07 2:31
Aaron VanWieren26-Jul-07 2:31 
GeneralRe: Stored Procedure Issue Pin
Brendan Vogt26-Jul-07 3:09
Brendan Vogt26-Jul-07 3:09 
Questioncoping data in sql server express Pin
Amjath Rahman25-Jul-07 22:55
Amjath Rahman25-Jul-07 22:55 
AnswerRe: coping data in sql server express Pin
Blue_Boy25-Jul-07 23:12
Blue_Boy25-Jul-07 23:12 
GeneralRe: coping data in sql server express Pin
Amjath Rahman25-Jul-07 23:24
Amjath Rahman25-Jul-07 23:24 
GeneralRe: coping data in sql server express Pin
Blue_Boy25-Jul-07 23:37
Blue_Boy25-Jul-07 23:37 
AnswerRe: coping data in sql server express Pin
Aaron VanWieren26-Jul-07 2:27
Aaron VanWieren26-Jul-07 2:27 
GeneralRe: coping data in sql server express Pin
Blue_Boy26-Jul-07 3:55
Blue_Boy26-Jul-07 3:55 
QuestionHow to reset identity(ID) field Pin
Mubashir Javaid25-Jul-07 22:09
Mubashir Javaid25-Jul-07 22:09 
AnswerRe: How to reset identity(ID) field Pin
Vasudevan Deepak Kumar25-Jul-07 22:15
Vasudevan Deepak Kumar25-Jul-07 22:15 
GeneralRe: How to reset identity(ID) field Pin
Muhammad Qasim Pasta25-Jul-07 22:37
Muhammad Qasim Pasta25-Jul-07 22:37 
AnswerRe: How to reset identity(ID) field Pin
Blue_Boy25-Jul-07 22:16
Blue_Boy25-Jul-07 22:16 
Questionasp horizontal data Pin
calvinjb888825-Jul-07 21:31
calvinjb888825-Jul-07 21:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.