Solved

How to use If Statement in Where Clause in SQL

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

695 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