Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Are dynamic sql in stored procedure affect performance ??

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-01-25 : 09:17:17
anil writes "hi,

I write a stored procedure like this

create procedure test
@age numeric(10)
@order varchar(1)

as

if @Order = "+"
begin
select *
from tab1
where age = @age
order by age acc
end
else if @order = "-"
begin
select *
from tab1
where age = @age
order by age desc
end

and other using the dynamic SQL

create procedure test1
@age numeric(10)
@order varchar(10)

as
declare
@sql varchar(250)

set @sql = "select * from tab1 where age = @age order by age"

if @Order = "+"
begin
set @orderby = "acc"
end
else if @order = "-"
begin
set @orderby = "desc"
end

set @sql = @sql + @orderby
EXECUTE sp_executesql @sql


will test have better performance than test1 ??

Stored procedure creates the query plan for the query so in test1 as we are creating the query dynamically will the database create the query plan for that ???

TIA
Anil "

Nazim
A custom title

1408 Posts

Posted - 2002-01-25 : 23:46:37
Follow this link
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=10964

--------------------------------------------------------------
Dont Tell God how big your Problem is , Tell the Problem how Big your God is
Go to Top of Page
   

- Advertisement -