Solved

How to use If Statement in Where Clause in SQL

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
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 shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

27 Experts available now in Live!

Get 1:1 Help Now