Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use If Statement in Where Clause in SQL

Posted on 2014-02-19
3
Medium Priority
?
1,388 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
[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
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.

610 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