Click here to Skip to main content
15,890,336 members
Home / Discussions / Database
   

Database

 
GeneralRe: Problem with trigger on tables and views Pin
blbecek.pitomej25-Apr-05 10:09
blbecek.pitomej25-Apr-05 10:09 
GeneralUpdate Question Pin
rudemusik22-Apr-05 11:43
rudemusik22-Apr-05 11:43 
GeneralRe: Update Question Pin
Yulianto.22-Apr-05 19:25
Yulianto.22-Apr-05 19:25 
Generalequivalent to OFFSET Pin
Drew Stainton22-Apr-05 11:08
Drew Stainton22-Apr-05 11:08 
GeneralRe: equivalent to OFFSET Pin
Colin Angus Mackay22-Apr-05 11:55
Colin Angus Mackay22-Apr-05 11:55 
GeneralRe: equivalent to OFFSET Pin
Drew Stainton22-Apr-05 13:22
Drew Stainton22-Apr-05 13:22 
GeneralAlternative to dynamic sp's Pin
totig22-Apr-05 2:16
totig22-Apr-05 2:16 
GeneralRe: Alternative to dynamic sp's Pin
Mike Dimmick22-Apr-05 3:51
Mike Dimmick22-Apr-05 3:51 
A quick bit of examining the execution plan reveals that for this procedure, when called with a product name but not the other fields, the query optimiser chooses to use a clustered index scan - basically just reading the table from start to end. This is typically the least efficient operation in SQL Server's armoury, although it may get used in preference to an index if the index isn't very selective (if a lot of results are found in the index, the cost of the bookmark lookups, which may well be out of order with respect to the table, may outweigh the cost of just reading the table).

When performing a SELECT with only ProductName in the WHERE clause, it performs an index seek on the ProductName index then a bookmark lookup to return the whole row.

I rewrote your query to a pattern I've used before:
CREATE PROCEDURE SearchProducts2 (
  @ProductName NVARCHAR(40) = null,
  @UnitPrice MONEY = null,
  @UnitsInStock SMALLINT = null
)
AS
SELECT *
FROM Products
WHERE 
  (ProductName = @ProductName OR @ProductName IS NULL) AND
  (UnitPrice = @UnitPrice OR @UnitPrice IS NULL) AND
  (UnitsInStock = @UnitsInStock OR @UnitsInStock IS NULL)
GO
This had the same effect as yours did, which did surprise me a little.

SQL Server will compile a query plan the first time using the arguments supplied at that time, and will normally cache the plan and reuse it next time. If the arguments that are used will be wildly divergent - lots of values identical in the table but with a few exceptions - you might find that the cached plan isn't good for some values but is for others. Using the WITH RECOMPILE option when creating the procedure forces SQL Server not to cache the plan. Alternatively you can specify WITH RECOMPILE with the EXEC statement to force a recompile at that time.

So I think dynamic SQL is preferable. You can still use parameters with a query string - and you should, to simplify your code (no need to escape special characters) and to avoid the possibility of SQL injection. You simply use a variable name (e.g. @ProductName) in the query text and add a parameter to the command object, the same as you would if you were calling a stored procedure.

Stability. What an interesting concept. -- Chris Maunder
GeneralTreeView--Treenode with a Checkbox bound to database Pin
billoo21-Apr-05 19:14
billoo21-Apr-05 19:14 
GeneralSQL Server Express Install Pin
RChin21-Apr-05 6:41
RChin21-Apr-05 6:41 
GeneralRe: SQL Server Express Install Pin
Jerry Hammond22-Apr-05 2:50
Jerry Hammond22-Apr-05 2:50 
GeneralRe: SQL Server Express Install Pin
RChin22-Apr-05 6:25
RChin22-Apr-05 6:25 
GeneralUsing mysql database Pin
Sasuko21-Apr-05 6:21
Sasuko21-Apr-05 6:21 
GeneralTable design for storing email address Pin
Raghunandan S20-Apr-05 17:30
Raghunandan S20-Apr-05 17:30 
GeneralRe: Table design for storing email address Pin
Anonymous21-Apr-05 10:39
Anonymous21-Apr-05 10:39 
GeneralExtract Create Index Script Pin
-Dr_X-20-Apr-05 11:40
-Dr_X-20-Apr-05 11:40 
GeneralSQL execution path question Pin
Judah Gabriel Himango20-Apr-05 9:12
sponsorJudah Gabriel Himango20-Apr-05 9:12 
GeneralRe: SQL execution path question Pin
Andy Brummer20-Apr-05 9:53
sitebuilderAndy Brummer20-Apr-05 9:53 
GeneralRe: SQL execution path question Pin
Colin Angus Mackay20-Apr-05 9:55
Colin Angus Mackay20-Apr-05 9:55 
GeneralRe: SQL execution path question Pin
-Dr_X-21-Apr-05 5:59
-Dr_X-21-Apr-05 5:59 
GeneralInserting at a specific row Pin
Esmo200020-Apr-05 8:29
Esmo200020-Apr-05 8:29 
GeneralRe: Inserting at a specific row Pin
Colin Angus Mackay20-Apr-05 9:06
Colin Angus Mackay20-Apr-05 9:06 
GeneralA sytax question Pin
Esmo200020-Apr-05 8:05
Esmo200020-Apr-05 8:05 
GeneralRe: A sytax question Pin
Colin Angus Mackay20-Apr-05 9:08
Colin Angus Mackay20-Apr-05 9:08 
GeneralExecuting an UPDATE for each record returned by a SELECT Pin
Luis Alonso Ramos20-Apr-05 6:09
Luis Alonso Ramos20-Apr-05 6:09 

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.