June 21, 2014 at 10:46 pm
I have multiple parameters for a sp, where I am executing a dynamic sql statement using sp_executesql. Now the parameter definitions can be dynamically set. However, all the parameters are to be passed for the sp_executesql to run. So, I was wondering if it is possible to set these parameters also dynamically? Will such an approach going to reduce the execution time?
June 21, 2014 at 11:54 pm
You can define the parameter to a default value of NULL
@YourParameter1 varchar(100)=NULL,
@YourParameter2 int=NULL
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
June 22, 2014 at 12:52 am
geo123abram (6/21/2014)
I have multiple parameters for a sp, where I am executing a dynamic sql statement using sp_executesql. Now the parameter definitions can be dynamically set. However, all the parameters are to be passed for the sp_executesql to run. So, I was wondering if it is possible to set these parameters also dynamically? Will such an approach going to reduce the execution time?
You build the parameter definition just as you build the dynamic sql, as it is also a dynamic sql statement. This is a better method than concatenating variables into the actual statement. On the other hand, changes in the parameter definition would render the previous execution plan invalid.
June 22, 2014 at 4:45 am
My question is, in the sp_executesql's parameter section (the last one), can I define the parameters dynamically? That is, I dont want to feed all the parameters but a selected few based on my conditions.
June 22, 2014 at 7:16 am
geo123abram (6/22/2014)
My question is, in the sp_executesql's parameter section (the last one), can I define the parameters dynamically? That is, I dont want to feed all the parameters but a selected few based on my conditions.
I am not able to understand how do you want it to be dynamic ? Simply you can set default values to NULL for all the parameters(which I had suggested in my first reply) and then based on those values passed(NULL or NOT NULL) you can do something like this.
use master
go
create proc TestBed
@number int=NULL,
@type varchar(10)=NULL
as
declare @sql nvarchar(max)=''
declare @params nvarchar(max)='@num int,@typ varchar(10)'
select @sql='select * from spt_values where (number=@num or @num is null) and (type=@typ or @typ is null)'
exec sp_executesql @sql,@params,@num=@number,@typ=@type
go
testbed 1,'P'
go
testbed NUll,'P'
go
testbed 1
go
testbed
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
June 22, 2014 at 10:17 am
geo123abram (6/22/2014)
My question is, in the sp_executesql's parameter section (the last one), can I define the parameters dynamically? That is, I dont want to feed all the parameters but a selected few based on my conditions.
You can, but there's little downside to passing all the all the parameters and just setting the ones you aren't using this call to null. Can simplify coding a bit.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 22, 2014 at 9:56 pm
What I wanted to do was instead of putting @num=@number,@typ=@type in
exec sp_executesql @sql,@params,@num=@number,@typ=@type
is it possible to put only those parameters that I want to put. Basically to reduce the WHERE clauses. I guess there is not much of an advantage in doing that as GilaMonster pointed out.
June 22, 2014 at 9:58 pm
GilaMonster, thanks for the response. But I do not know how to do that. Can you show me a small example?
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy