?
Solved

Building WHERE clause in Stored Procedure

Posted on 2011-02-16
15
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

771 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