SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Is this Correct Use of Dynamic SQL???


Is this Correct Use of Dynamic SQL???

Author
Message
enriquezreyjoseph
enriquezreyjoseph
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 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:-(....
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114559 Visits: 41388
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
enriquezreyjoseph
enriquezreyjoseph
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search