| Author |
Topic  |
|
|
stormie
Starting Member
United Kingdom
5 Posts |
Posted - 06/09/2012 : 08:07:45
|
Hi , I wonder if anyone can help me. I am working on redisgning my website , which is a database of around 5000 castles and I am really struggling with sp_executesql
Basically, I am calling stored procedure that has a number of paramters, that may or may not be present, so for example a list of all the castles in a county, this could then be refined by it's type - so masonry castle, timber castle etc.
There seems to be some issue with the order of the parameters, becoming mismatched.
If I give a cut down example of what I am trying to do
ALTER procedure [dbo].[countyListings_test]
@palace nvarchar(255) = null,
@historicCounty nvarchar(255) = null,
@confidence nvarchar(255) = null
as
select @palace as palace
select @historiccounty as historicCounty
select @confidence as confidence
return
Then I call it like this
exec sp_executesql
N'exec dbo.countyListings_test @historicCounty,@palace',
N'@historicCounty nvarchar(12),
@palace nvarchar(255)',
@historicCounty=N'Lincolnshire',
@palace=N'Palace'
Note they was being passed in a different order and it returns incorrect, so historicCounty = palace and palace = historic county
but if i do
exec dbo.countyListings_test @historicCounty='test',@palace='palace'
(still in an incorrect order) - it works
forgive me if I am being silly, but I am a building historian and not a programmer
I would appreciate any help
|
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 06/09/2012 : 13:37:38
|
sorry your issue is not clear. the order should not affect so far as you're specifying parameter name correctly in exec statement. Also inside the proc i dont understand why you're just printing parameter values. I hope its just for illustration purpose and in real scenario you'll use it for filtering data from table. In such case the logic should be like
....
where (palacefield = @palace or @palace is null)
and (historicCountyfield = @historicCounty or @historicCounty is null)
and (confidencefield = @confidence or @confidence is null)
...
this can cause a performance bottleneck in case of large dataset and in such cases its better to use dynamic sql with sp_executesql
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
stormie
Starting Member
United Kingdom
5 Posts |
Posted - 06/09/2012 : 14:10:39
|
Sorry if I was not clear
My issue is the sp is expecting say 4 parameters, but if I only pass parameter 2,3,4 because parameter 1 is not needed
parameter 2 is going in the parameter1 slot 3 in the 2 slot and 4 in the three slot.
I dont understand why this is happening
The example if gave was to illustrate how it's going wrong, but you are correct my code does look like where a.historicCounty = @historicCounty and (a.confidence = @certain or a.confidence = @possible or a.confidence = @questionable or a.confidence = @rejected )
I would be much happier translating Medieval french I think  |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 06/09/2012 : 14:16:41
|
quote: Originally posted by stormie
Sorry if I was not clear
My issue is the sp is expecting say 4 parameters, but if I only pass parameter 2,3,4 because parameter 1 is not needed
parameter 2 is going in the parameter1 slot 3 in the 2 slot and 4 in the three slot.
I dont understand why this is happening
The example if gave was to illustrate how it's going wrong, but you are correct my code does look like where a.historicCounty = @historicCounty and (a.confidence = @certain or a.confidence = @possible or a.confidence = @questionable or a.confidence = @rejected )
I would be much happier translating Medieval french I think 
are you passing values from application or directly calling proc. As far as I can see you shouldnt have any issues in ordering as you're explicitly specifying parameter names while passing values
One question though, why you're filtering all passed values against same field?
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
stormie
Starting Member
United Kingdom
5 Posts |
Posted - 06/09/2012 : 14:26:19
|
Thanks for your prompt response
It's getting called from the asp .net web page, but I seem to have the same problem if I run it via sql query analyzer
What I am trying to achieve, is I have checkBox with four items, which can be true or false and I'm using it to filter out the data
In the confidence example, if I only want to see "certain" and "possible" sites, then I just tick those two.
I can work round the issue, by adding in all 4 parameters, but pass a null for the ones that are unchecked.
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 06/09/2012 : 15:13:50
|
quote: Originally posted by stormie
Thanks for your prompt response
It's getting called from the asp .net web page, but I seem to have the same problem if I run it via sql query analyzer
What I am trying to achieve, is I have checkBox with four items, which can be true or false and I'm using it to filter out the data
In the confidence example, if I only want to see "certain" and "possible" sites, then I just tick those two.
I can work round the issue, by adding in all 4 parameters, but pass a null for the ones that are unchecked.
what issue are you facing while calling through sql anaylser?
i assume you're explicitly passing parameter names as well as posted above
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
stormie
Starting Member
United Kingdom
5 Posts |
Posted - 06/09/2012 : 15:48:08
|
ok if you try this in sql analyser
create procedure dbo.myProblem
@param1 varchar(255) =null,
@param2 varchar(255) = null,
@param3 varchar(255) = null
as
select
@param1 as P1,@param2 as P2,@param3 as P3
then this
exec sp_executesql
N'exec dbo.myProblem @param1 ,@param2, @param3',
N'@param1 nvarchar(255),@param2 nvarchar(255),@param3 nvarchar(255)',
@param1=N'p1',
@param2=N'p2',
@param3=N'p3' gives p1 p2 p3
exec sp_executesql
N'exec dbo.myProblem @param1 , @param3',
N'@param1 nvarchar(255),@param3 nvarchar(255)',
@param1=N'p1',
@param3=N'p3'
gives p1 p3 NULL where it should be p1 null p3
but typing
exec dbo.myProblem @param1='P1',@param3='P3'
gives P1 NULL P3 which is correct
All very odd |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 06/09/2012 : 15:51:54
|
modify the below statement
exec sp_executesql
N'exec dbo.myProblem @param1 ,@param2, @param3',
N'@param1 nvarchar(255),@param2 nvarchar(255),@param3 nvarchar(255)',
@param1=N'p1',
@param2=N'p2',
@param3=N'p3'
like this and see
exec sp_executesql
N'exec dbo.myProblem @param1 =@param1,@param2 = @param2, @param3 = @param3',
N'@param1 nvarchar(255),@param2 nvarchar(255),@param3 nvarchar(255)',
@param1=N'p1',
@param2=N'p2',
@param3=N'p3'
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
stormie
Starting Member
United Kingdom
5 Posts |
Posted - 06/09/2012 : 16:00:20
|
Thank you , thats sorted it. I've just got to work out how to get it to do that on the asp c# side of things now
It's been bothering me all day ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 06/09/2012 : 20:10:05
|
welcome...as i told issue was with not explicitly passing parameter name when ignoring parameters in between
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|
|
|