I wonder if I could write a dynamic where statement in a store procedure.
I have a web page that will pass following information to the store procedur
e.
Lets say I have a parameter @.typeid that can be 1 for rent, 2 for sale, 1 or
2 for either. My store procedure looks like this
create select_apartment_details
(
@.typeid nvarchar(50) -- @.typeid ='1' or @.typeid ='2' or @.typeid='1, 2'
)
AS
SELECT * FROM tbl_apartments WHERE typeid IN (@.typeid)
GO
By the way typeid field in the tbl_apartments is integer.
However, this doesnt work. Can anyone help me pls?If the values in the TypeID Column in the tbl_apartments table are indeed
limited to
1 or 2, then I suggest the following
Pass in @.TypeID = 1 When you want only the 1s,
@.TypeID = 2 When you want only the 2s,
and @.TypeID = 3, when you want 1s and 2s
Then in your where clause, you can do this
Where TypeID ^ @.Type <> 0
This will do a bitwise And comparison such that
Database TypeID
@.TypeID | 1 2
-- | -- --
1 | 1 0
2 | 0 2
3 | 1 2
"regaliel" wrote:
> I wonder if I could write a dynamic where statement in a store procedure.
> I have a web page that will pass following information to the store proced
ure.
> Lets say I have a parameter @.typeid that can be 1 for rent, 2 for sale, 1
or
> 2 for either. My store procedure looks like this
> create select_apartment_details
> (
> @.typeid nvarchar(50) -- @.typeid ='1' or @.typeid ='2' or @.typeid='1, 2'
> )
> AS
> SELECT * FROM tbl_apartments WHERE typeid IN (@.typeid)
> GO
> By the way typeid field in the tbl_apartments is integer.
> However, this doesnt work. Can anyone help me pls?
>|||Sorry Memory tricked me... The bitwise operator yu want is bitwise AND, and
the operator symbol is an Ampersand as in
Where TypeID & @.TypeID <> 0
"regaliel" wrote:
> I wonder if I could write a dynamic where statement in a store procedure.
> I have a web page that will pass following information to the store proced
ure.
> Lets say I have a parameter @.typeid that can be 1 for rent, 2 for sale, 1
or
> 2 for either. My store procedure looks like this
> create select_apartment_details
> (
> @.typeid nvarchar(50) -- @.typeid ='1' or @.typeid ='2' or @.typeid='1, 2'
> )
> AS
> SELECT * FROM tbl_apartments WHERE typeid IN (@.typeid)
> GO
> By the way typeid field in the tbl_apartments is integer.
> However, this doesnt work. Can anyone help me pls?
>|||Try this:
SELECT * FROM tbl_apartments WHERE CHARINDEX(CAST(typeid AS CHAR(1)), @.test)
> 0
"regaliel" <regaliel@.discussions.microsoft.com> wrote in message
news:9B49F533-89B9-492D-B556-96B2199585C9@.microsoft.com...
>I wonder if I could write a dynamic where statement in a store procedure.
> I have a web page that will pass following information to the store
> procedure.
> Lets say I have a parameter @.typeid that can be 1 for rent, 2 for sale, 1
> or
> 2 for either. My store procedure looks like this
> create select_apartment_details
> (
> @.typeid nvarchar(50) -- @.typeid ='1' or @.typeid ='2' or @.typeid='1, 2'
> )
> AS
> SELECT * FROM tbl_apartments WHERE typeid IN (@.typeid)
> GO
> By the way typeid field in the tbl_apartments is integer.
> However, this doesnt work. Can anyone help me pls?
>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment