15. juli 2003 - 09:07Der er
18 kommentarer og 2 løsninger
Help with Stored Procedure
The following Stored procedure can be called with no parameter where I want it to return ALL records, or with a single paramater where it should only return those where TC.PostCodeID = @postcode.
The SP works when using a parameter, the problem is that when no paramaters are given only those where TC.PostCodeID Is Not Null are returned.
So the question is I want it to return ALL records including those where TC.PostCodeID = NULL when no paramaters are used.
NOTE: This is only an example, the WHERE clause contains further paramaters. I have also tried setting ANSI_NULLS OFF before creating the SP!
CREATE PROC spTest ( @Postcode int=Null )
AS SELECT * FROM tblContact TC WHERE TC.PostCodeID = IsNull(@PostCode, TC.PostCodeID)
Hi Arne, yes and thats my problem :o) SP's isnt my hot spot if I made a dynamic SQL I could do it in no time but I would like to use an SP in this case.
15/07-2003 09:13:42 Yes I know thats a solution but I would like if possible to get away from this as there are a number of paramaters and I can imagine how many SELECTS I would need :o)
Prøv med: CREATE PROC spTest ( @Postcode int=Null ) AS if @PostCode is null SELECT * FROM tblContact TC else SELECT * FROM tblContact TC WHERE TC.PostCodeID = @PostCode
Thanks to you both for your input, I think I'll stick to the easy way and build the SQL dynamically in my application (Access :o) ). Now the next problem is! How do I share out the points? I have rejected your answer Arne, didnt mean to, so if you and Steen can place an answer I'll see if I can share the points between you both.
Hope the points are acceptable, once again thanks :o)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.