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 ««12

Is this Correct Use of Dynamic SQL??? Expand / Collapse
Author
Message
Posted Sunday, September 29, 2013 5:47 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, October 21, 2013 3:10 AM
Points: 81, Visits: 191
Cadavre (9/28/2013)
When you post in multiple threads like this, you fragment answers and make people have to start from the beginning again with helping you. If you have further questions, stick to your original thread please.



Sorry:-(....
Post #1499800
Posted Sunday, September 29, 2013 6:19 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:38 AM
Points: 36,979, Visits: 31,502
pietlinden (9/28/2013)
If you understand the repercussions of code that can't be optimized...


Actually, one of the best reasons for using Dynamic SQL is to optimize the performance of "Catch All" queries. Gail Shaw has a great article on the subject.
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1499805
Posted Sunday, September 29, 2013 7:08 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, October 21, 2013 3:10 AM
Points: 81, Visits: 191
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SearchBiography]

@firstname nvarchar(50),
@middlename nvarchar(50),
@lastname nvarchar(50),
@sexID nchar(5) = NULL,
@statusID nchar(5) = NULL

AS
BEGIN

SET NOCOUNT ON;
DECLARE @SqlQuery nvarchar(max) , @SqlParam nvarchar(max)

SET @SqlQuery = ''

SELECT @SqlQuery = ' SELECT * ' + ' FROM TestMyView WHERE 1 = 1 '

IF @sexID <> '0'

SELECT @SqlQuery = @SqlQuery + ' AND sexID = @xsexID '

IF @statusID <> '0'

SELECT @SqlQuery = @SqlQuery + ' AND statusID = @xstatusID '

IF LEN(@firstname) > 0

SELECT @SqlQuery = @SqlQuery + ' AND firstname LIKE ''%'' + @xfirstname + ''%'' '

IF LEN(@middlename) > 0

SELECT @SqlQuery = @SqlQuery + ' AND middlename LIKE ''%'' + @xmiddlename + ''%'' '

IF LEN(@lastname) > 0

SELECT @SqlQuery = @SqlQuery + ' AND lastname LIKE ''%'' + @xlastname + ''%'' '


SELECT @SqlParam = '@xfirstname nvarchar(50),
@xmiddlename nvarchar(50),
@xlastname nvarchar(50),
@xsexID nchar(1),
@xstatusID nchar(1) '


EXEC sp_executesql @SqlQuery,@SqlParam,
@xfirstname = @firstname, @xmiddlename = @middlename,
@xlastname = @lastname, @xsexID = @sexID,
@xstatusID = @statusID
END




by reference to : http://www.sommarskog.se/dyn-search-2005.html and http://www.sommarskog.se/dynamic_sql.html
Post #1499814
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse