Reputation: 197
need a bit of help in accomplishing something in tsql. I am collecting sql profiler trace data to get a particular stored procedure execution and basically I am trying to strip out the parameter values from the textdata column Example for the below set of data, i need to stripe out parameter value out
exec test
exec test @aa=10
exec test @aa=10,@bb=10
exec test @aa=10,@bb=10,@cc=100
exec test @aa=10,@bb=1000,@cc=1
so the output table might look like
aa bb cc
10 Null NUll
10 10 NULL
10 10 100
10 1000 1
I am just trying to find out what are the common parameters that are being passed to the sp, so if there is other easy ways of doing it please let me know.
Upvotes: 1
Views: 97
Reputation: 5094
what i understand is that you want to overcome parameter sniffing problem .
you can make your proc like this,
create proc usp_test
(
@aa int,
@bb int ,
@cc int
)
as
begin
DECLARE @aa1 INT
SET @aa1 = @aa
DECLARE @bb1 INT
SET @bb1 = @bb
DECLARE @cc1 INT
SET @cc1 = @cc
select col1,col2 from testtable
where col1=@aa1 and col2=@bb1
end
https://www.simple-talk.com/sql/t-sql-programming/parameter-sniffing/
https://blogs.technet.microsoft.com/mdegre/2011/11/06/what-is-parameter-sniffing/
Upvotes: 0
Reputation: 28895
Few Ways i could think off..
1.using plan cache
select query_plan from sys.dm_exec_cached_plans cp
cross apply
sys.dm_exec_text_query_plan(cp.plan_handle,default,default)
where objtype='proc' and object_name(objectid)='usp_test'
Now the above query plan is an xml which will contain all the values used at compile time .This procedure is very cuber some and you will get only compiled values.But with time due to plan cache invalidation which can occur due to many reasons,you will get new values over time
2.Modify your stored proc to insert into some other table all the param values some thing like below
create proc usp_test
(
@a int=1,
@b int =2
)
as
begin
insert into sometable
select @a,@b,getdate()
end
Other than the above said ways,there is no way i could think off by which you can obtain passed parameter values(Excluding trace you are running)
If you are looking to troubleshoot parameter sniffing,by gathering all parameter values,this might not be the accurate way
Upvotes: 2