Solved

How to use If Statement in Where Clause in SQL

Posted on 2014-02-19
3
1,086 Views
Last Modified: 2014-02-19
Hi all
I am looking for a way to use one single storeprocedure for multiple queries.

Hence I need to switch the where condition based on the table I need to access.
How can I achive this..

FROM INFORMATION_SCHEMA.COLUMNS
			WHERE table_name = @TransactionTabelle			
			IF (@TransactionTabelle = New_Transaction)
			AND COLUMN_NAME IN (
			'Text1',
			'Text2',
			'Text3'
			)
			ELSE
			AND COLUMN_NAME IN 
			(
			'TextA1',
			'TextA2',
			'TextA3'
			)

Open in new window


thanks in Advnace
0
Comment
Question by:ZURINET
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39869645
instead of a if, you have to start using case in the where clause.... you cannot use a if in the where clause....

for the below case you can use a simple or clause to get the stuff done as shown below
FROM INFORMATION_SCHEMA.COLUMNS
			WHERE table_name = @TransactionTabelle			
			AND (
                                 ( (@TransactionTabelle = New_Transaction) 
			           AND COLUMN_NAME IN ('Text1','Text2','Text3')
                                 )
                                 OR
                                 (
                                     COLUMN_NAME IN ('TextA1','TextA2','TextA3')
                                 )
                                )

Open in new window

0
 

Author Comment

by:ZURINET
ID: 39869662
Do you think that the or clause will work..
I thinking of using the case clause
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39872037
in this case the or will work as well....
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now