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.
| Author |
Topic |
|
stormie
Starting Member
5 Posts |
Posted - 2012-06-09 : 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_executesqlBasically, 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 doALTER procedure [dbo].[countyListings_test] @palace nvarchar(255) = null,@historicCounty nvarchar(255) = null,@confidence nvarchar(255) = nullas select @palace as palaceselect @historiccounty as historicCountyselect @confidence as confidencereturn Then I call it like thisexec 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 countybut if i doexec dbo.countyListings_test @historicCounty='test',@palace='palace'(still in an incorrect order) - it worksforgive 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
52326 Posts |
Posted - 2012-06-09 : 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
stormie
Starting Member
5 Posts |
Posted - 2012-06-09 : 14:10:39
|
Sorry if I was not clearMy issue is the sp is expecting say 4 parameters, but if I only pass parameter 2,3,4 because parameter 1 is not neededparameter 2 is going in the parameter1 slot 3 in the 2 slot and 4 in the three slot.I dont understand why this is happeningThe example if gave was to illustrate how it's going wrong, but you are correct my code does look like wherea.historicCounty = @historicCountyand (a.confidence = @certainor a.confidence = @possibleor a.confidence = @questionableor a.confidence = @rejected)I would be much happier translating Medieval french I think |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-09 : 14:16:41
|
quote: Originally posted by stormie Sorry if I was not clearMy issue is the sp is expecting say 4 parameters, but if I only pass parameter 2,3,4 because parameter 1 is not neededparameter 2 is going in the parameter1 slot 3 in the 2 slot and 4 in the three slot.I dont understand why this is happeningThe example if gave was to illustrate how it's going wrong, but you are correct my code does look like wherea.historicCounty = @historicCountyand (a.confidence = @certainor a.confidence = @possibleor a.confidence = @questionableor 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 valuesOne question though, why you're filtering all passed values against same field?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
stormie
Starting Member
5 Posts |
Posted - 2012-06-09 : 14:26:19
|
| Thanks for your prompt responseIt's getting called from the asp .net web page, but I seem to have the same problem if I run it via sql query analyzerWhat 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 dataIn 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
52326 Posts |
Posted - 2012-06-09 : 15:13:50
|
quote: Originally posted by stormie Thanks for your prompt responseIt's getting called from the asp .net web page, but I seem to have the same problem if I run it via sql query analyzerWhat 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 dataIn 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
stormie
Starting Member
5 Posts |
Posted - 2012-06-09 : 15:48:08
|
ok if you try this in sql analysercreate procedure dbo.myProblem@param1 varchar(255) =null,@param2 varchar(255) = null,@param3 varchar(255) = nullasselect@param1 as P1,@param2 as P2,@param3 as P3 then thisexec 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 p3exec sp_executesql N'exec dbo.myProblem @param1 , @param3',N'@param1 nvarchar(255),@param3 nvarchar(255)',@param1=N'p1',@param3=N'p3' gives p1 p3 NULLwhere it should be p1 null p3but typingexec dbo.myProblem @param1='P1',@param3='P3' gives P1 NULL P3 which is correctAll very odd |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-09 : 15:51:54
|
modify the below statementexec 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 seeexec 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
stormie
Starting Member
5 Posts |
Posted - 2012-06-09 : 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 nowIt's been bothering me all day ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-09 : 20:10:05
|
| welcome...as i told issue was with not explicitly passing parameter name when ignoring parameters in between------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|