Solved

How to use If Statement in Where Clause in SQL

Posted on 2014-02-19
3
1,142 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

839 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