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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Minimize Query

Author  Topic 

ravininave
Posting Yak Master

111 Posts

Posted - 2012-08-09 : 14:08:49
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

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

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

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

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_executesql
actually many times there are many parameters in sql.



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

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

- Advertisement -