Solved

Building WHERE clause in Stored Procedure

Posted on 2011-02-16
15
249 Views
Last Modified: 2012-08-13
I have 3 parameters for a stored procedure.
Desc1      Year      Desc2
08-100      2008      PERM-1
09-110      2009      PERM-2
10-289      2010      PERM-3

I want to construct a WHERE clause like below
(Desc1 = ’08-100’ AND Year=’2008’ AND ‘Desc2’ = ‘PERM-1’) AND
(Desc1 = ’09-110’ AND Year=’2009’ AND ‘Desc2’ = ‘PERM-2’) AND
(Desc1 = ’10-289’ AND Year=’2010’ AND ‘Desc2’ = ‘PERM-3’)

Would someone please help me on this
0
Comment
Question by:cimscims
  • 9
  • 6
15 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34908178
I am not sure what you are looking for.
This is the way to create a stored procedure using those three parameters
CREATE PROCEDURE usp_YourStoredProcedure
(
    @Desc1 VARCHAR(50),
    @Year   INT,
    @Desc2 VARCHAR(50)
)
AS
BEGIN
    SELECT * FROM YourTable WHERE Desc1 = @Desc1 AND Year =  @Year AND Desc2 = @Desc2
END
GO
0
 

Author Comment

by:cimscims
ID: 34908377
The three parameters for the SP that have values as below
Desc1 --> '08-100,09-110,10-289'
Year    --> '2008','2009','2010'
Desc2 --> 'PERM-1,PERM-2,PERM-3'

I have the stored procedure as below

CREATE PROCEDURE usp_XXXXX(Desc1 varchar(2000),Year varchar(2000),Desc2 varchar(2000)
AS
BEGIN
SELECT col1,col2...
FROM table1 A
               outer join table2 B ON A.Id = B.Id
               outer join table3 C ON A.Id = C.Id
WHERE
             (Desc1 = ’08-100’ AND Year=’2008’ AND ‘Desc2’ = ‘PERM-1’) AND
             (Desc1 = ’09-110’ AND Year=’2009’ AND ‘Desc2’ = ‘PERM-2’) AND
             (Desc1 = ’10-289’ AND Year=’2010’ AND ‘Desc2’ = ‘PERM-3’)                                            
END

As you see in the above procedure, i want to construct a WHERE clause.

Let me know if i am not clear. Thanks
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34908561
I understood. Good Question!

You need to use split function or use that code in your stored procedure.
I will try to help. Or by that time some other experts may post before me

Raj
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34908586
Would the count of values alway three (as in your example) or it could vary ?
eg:-  '08-100,09-110,10-289' - have three data

Raj
0
 

Author Comment

by:cimscims
ID: 34908925
There is no specific count. It could vary from 1 to any number (may be in hundreds).
0
 

Author Comment

by:cimscims
ID: 34909278
I have used Split and tried to customize but i am getting a syntax error at SET @where statement. Would you pls help me out.
SP.txt
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34909325
So you are trying to create a dynamic query

The error is that - in your query acts as substract operator - which is not valid for varchar

Raj
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34909371
This is the WHERE part you are trying to create dynamically, right ?
I.Desc1 = @sliceDesc1
 AND I.Year = @sliceYear 
AND I.Desc2 = @sliceDesc2

Open in new window

Check it with your current WHERE part, it have some issues
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34909403
It should be simply
SET @where = 'I.Desc1 = ' + @sliceDesc1 + ' AND I.Year = ' + @sliceYear + ' AND I.Desc2 = ' + @sliceDesc2

0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 34909417
Your logic is correct.

Create the WHERE condition part of the query inside the loop and finally attach to the query and execute it.

Remember to do this way. Finally WHERE part should like this (OR - not AND)
   (Desc1 = ’08-100’ AND Year=’2008’ AND ‘Desc2’ = ‘PERM-1’) OR
             (Desc1 = ’09-110’ AND Year=’2009’ AND ‘Desc2’ = ‘PERM-2’) OR
             (Desc1 = ’10-289’ AND Year=’2010’ AND ‘Desc2’ = ‘PERM-3’)      
0
 

Author Comment

by:cimscims
ID: 34909421
I am correcting the WHERE clause. But the Where clause is fecthing the first row three times. I will look into this and post back.
0
 

Author Comment

by:cimscims
ID: 34909440
I will replace AND with OR
0
 

Author Closing Comment

by:cimscims
ID: 34909585
Thanks for your time Raj.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34909674
Glad I could assist you.

I am posting another solution that I have. I am not sure this will more better than your current solution.
Anyway posting

1. Create this function
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


create FUNCTION ConvertCSDataToTable
(
	@Desc1 VARCHAR(8000),
    @Year  VARCHAR(8000),
    @Desc2 VARCHAR(8000)

)
RETURNS @Items table 
(
	Desc1 VARCHAR(8000),
    Year  VARCHAR(8000),
    Desc2 VARCHAR(8000))
AS
BEGIN

DECLARE @Desc1Temp As varchar(8000), @YearTemp AS VARCHAR(8000), @Desc2Temp AS VARCHAR(8000)
DECLARE @Pos1 As int, @Pos2 As int, @Pos3 As int

WHILE DATALENGTH(@Desc1)>0
BEGIN
	SET @Pos1=CHARINDEX(',',@Desc1)
	IF @Pos1=0 SET @Pos1=DATALENGTH(@Desc1)+1
	SET @Desc1Temp =  LTRIM(RTRIM(LEFT(@Desc1,@Pos1-1)))
	SET @Desc1=SUBSTRING(@Desc1,@Pos1+DATALENGTH(','),8000)

	SET @Pos2=CHARINDEX(',',@Desc2)
	IF @Pos2=0 SET @Pos2=DATALENGTH(@Desc2)+1
	SET @Desc2Temp =  LTRIM(RTRIM(LEFT(@Desc2,@Pos2-1)))
	SET @Desc2=SUBSTRING(@Desc2,@Pos2+DATALENGTH(','),8000)

	SET @Pos3=CHARINDEX(',',@Year)
	IF @Pos3=0 SET @Pos3=DATALENGTH(@Year)+1
	SET @YearTemp =  LTRIM(RTRIM(LEFT(@Year,@Pos3-1)))
	SET @Year=SUBSTRING(@Year,@Pos3+DATALENGTH(','),8000)

	INSERT INTO @Items VALUES (@Desc1Temp, @YearTemp,  @Desc2Temp)
END
RETURN
END

Open in new window


2. Then use this query inside your stored procedure - Uncomment Collate if any collate error
Instead of '08-100,09-110,10-289' use the parameter  - @Desc1 etc
SELECT a.*
 FROM #table A,
 abaxuser.ConvertCSDataToTable( '08-100,09-110,10-289',  '2008,2009,2010', 'PERM-1,PERM-2,PERM-3') B
WHERE a.Desc1 = b.Desc1 -- COLLATE SQL_Latin1_General_CP1_CI_AS
AND a.Year = b.Year  -- COLLATE SQL_Latin1_General_CP1_CI_AS
AND a.Desc2 = b.Desc2 -- COLLATE SQL_Latin1_General_CP1_CI_AS

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34909683
Instead of 'abaxuser', use 'dbo'
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question