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 |
ravininave
Posting Yak Master
111 Posts |
Posted - 2012-08-09 : 14:08:49
|
This is My Stores ProcedureCREATE PROCEDURE [dbo].[FetchUserWisePins](@FirstDate datetime,@SecondDate datetime,@mTag int)ASBEGIN --Parameters @mTag = 0 -All, 1 - Paid Users, 2 - Unpaid Users--JoinType = 0 - Unpaid Users, 1 - Paid user, 2 - Paid userIF @mTag =0 --All UsersBEGIN Select mName,Salary from CustMaster Where JoinType in (0,1,2) -- JoinType is Int FieldENDIF @mTag =1 --Paid usersBEGIN Select mName,Salary from CustMaster Where JoinType in (1,2) -- JoinType is Int FieldENDIF @mTag =2 --UnPaid usersBEGIN Select mName,Salary from CustMaster Where JoinType in (0) -- JoinType is Int FieldENDEND I thought to pass parameters of @mTag something likeDeclare @MyType nvarchar (10)Set @MyType = Case When @mTag = 0 then (0,1,2)When @mTag = 1 then (1,2)When @mTag = 2 then (0) ELSE (0) ENDand simply use my sql statement as Select mName,Salary from CustMaster Where JoinType in (@MyType)But as the JoinType is Numeric fields it gives error.Can anyone help to simplify this.VB6/ASP.NET------------------------http://www.nehasoftec.com |
|
chadmat
The Chadinator
1974 Posts |
Posted - 2012-08-09 : 14:32:59
|
You need to parse out the Ints from the string if you want to pass it in as a string.-Chad |
 |
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2012-08-09 : 14:34:33
|
quote: Originally posted by chadmat You need to parse out the Ints from the string if you want to pass it in as a string.-Chad
I don't wish to pass it in as a string. I simply wish to fulfill my requirement.VB6/ASP.NET------------------------http://www.nehasoftec.com |
 |
|
chadmat
The Chadinator
1974 Posts |
Posted - 2012-08-09 : 14:38:53
|
What is your requirement? I see a stored proc, and something you were trying that didn't work.You could build the query string and do an sp_executesql or just an exec.-Chad |
 |
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2012-08-09 : 14:42:41
|
quote: Originally posted by chadmat What is your requirement? I see a stored proc, and something you were trying that didn't work.You could build the query string and do an sp_executesql or just an exec.-Chad
Yes exec could be the solution. But what I wish to try like :Select mName,Salary from CustMaster Where JoinType in (@MyType)is this possible without exec or sp_executesqlactually many times there are many parameters in sql.VB6/ASP.NET------------------------http://www.nehasoftec.com |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-08-09 : 14:47:34
|
If you have only these three cases, I would stick with the query you already have. An alternative - which is simple to do, but probably not great from a performance perspective is as follows:CREATE PROCEDURE [dbo].[FetchUserWisePins](@FirstDate datetime,@SecondDate datetime,@mTag VARCHAR(32))ASSelect mName,Salary from CustMaster Where ','+@mTag+',' LIKE '%,'+CAST(JoinType AS VARCHAR(32))+',%'GO You would then set the parameter like this:Declare @MyType nvarchar (10)Set @MyType = Case When @mTag = 0 then '0,1,2'When @mTag = 1 then '1,2'When @mTag = 2 then '0' ELSE '0' END Alternatively, you can use dynamic SQL (sp_executesql) but will need to be careful to reduce the risk of SQL injection attacks. |
 |
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2012-08-09 : 15:10:51
|
quote: Originally posted by sunitabeck If you have only these three cases, I would stick with the query you already have. An alternative - which is simple to do, but probably not great from a performance perspective is as follows:CREATE PROCEDURE [dbo].[FetchUserWisePins](@FirstDate datetime,@SecondDate datetime,@mTag VARCHAR(32))ASSelect mName,Salary from CustMaster Where ','+@mTag+',' LIKE '%,'+CAST(JoinType AS VARCHAR(32))+',%'GO You would then set the parameter like this:Declare @MyType nvarchar (10)Set @MyType = Case When @mTag = 0 then '0,1,2'When @mTag = 1 then '1,2'When @mTag = 2 then '0' ELSE '0' END Alternatively, you can use dynamic SQL (sp_executesql) but will need to be careful to reduce the risk of SQL injection attacks.
Thanx, will check and let u know.VB6/ASP.NET------------------------http://www.nehasoftec.com |
 |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-08-09 : 18:58:15
|
quote: But what I wish to try like :Select mName,Salary from CustMaster Where JoinType in (@MyType)is this possible without exec or sp_executesql
No, the IN operator does not allow a string variable. Also, if the "All" option truly returns every record, why have a where clause at all for that query?=================================================Show me a sane man and I will cure him for you. -Carl Jung, psychiatrist (1875-1961) |
 |
|
|
|
|
|
|