SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Minimize Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ravininave
Yak Posting Veteran

India
94 Posts

Posted - 08/09/2012 :  14:08:49  Show Profile  Reply with Quote
This is My Stores Procedure
CREATE PROCEDURE [dbo].[FetchUserWisePins]
(@FirstDate datetime,@SecondDate datetime,@mTag int)
AS
BEGIN	
 
--Parameters @mTag = 0 -All, 1 - Paid Users, 2 - Unpaid Users
--JoinType = 0 - Unpaid Users, 1 - Paid user, 2 - Paid user

IF @mTag =0  --All Users
BEGIN
  Select mName,Salary from CustMaster Where JoinType in (0,1,2) -- JoinType is Int Field
END

IF @mTag =1  --Paid users
BEGIN
  Select mName,Salary from CustMaster Where JoinType in (1,2) -- JoinType is Int Field
END


IF @mTag =2  --UnPaid users
BEGIN
  Select mName,Salary from CustMaster Where JoinType in (0) -- JoinType is Int Field
END

END



I thought to pass parameters of @mTag something like

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

and 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

USA
1962 Posts

Posted - 08/09/2012 :  14:32:59  Show Profile  Visit chadmat's Homepage  Reply with Quote
You need to parse out the Ints from the string if you want to pass it in as a string.

-Chad
Go to Top of Page

ravininave
Yak Posting Veteran

India
94 Posts

Posted - 08/09/2012 :  14:34:33  Show Profile  Reply with Quote
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
Go to Top of Page

chadmat
The Chadinator

USA
1962 Posts

Posted - 08/09/2012 :  14:38:53  Show Profile  Visit chadmat's Homepage  Reply with Quote
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
Go to Top of Page

ravininave
Yak Posting Veteran

India
94 Posts

Posted - 08/09/2012 :  14:42:41  Show Profile  Reply with Quote
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_executesql
actually many times there are many parameters in sql.



VB6/ASP.NET
------------------------
http://www.nehasoftec.com
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/09/2012 :  14:47:34  Show Profile  Reply with Quote
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))
AS

Select 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.
Go to Top of Page

ravininave
Yak Posting Veteran

India
94 Posts

Posted - 08/09/2012 :  15:10:51  Show Profile  Reply with Quote
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))
AS

Select 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
Go to Top of Page

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1435 Posts

Posted - 08/09/2012 :  18:58:15  Show Profile  Reply with Quote
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)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000