Solved

Dynamic SQL statement

Posted on 2011-03-21
9
237 Views
Last Modified: 2012-05-11
How can I build a dynamic SQL statement without inserting the SQL into a variable.

For example the store procedure has a variable called @Sex.

This is my main SQL statement:

Select * FROM MYTABLE
WHERE SSN = @SSN AND AGE=@AGE AND

I would build the rest of the SQL statement using the variable @SEX

@SEX (if male)
TVSHOWS=@TVSHOWS AND RACING=@RACING AND NASCAR=@NASCAR

@SEX (if Female)
SOAPS=@SOAPS AND COOKING=@COOKING AND CHILDREN=@CHILDREN


0
Comment
Question by:Internet_Engineer
[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
9 Comments
 
LVL 5

Expert Comment

by:KGNickl
ID: 35182854
Make separate queries in a stored procedure. Then just pass in the sex of the person when calling the stored procedure and it will run the correct query for you.

IF @SEX = "MALE"
TVSHOWS=@TVSHOWS AND RACING=@RACING AND NASCAR=@NASCAR

IF @SEX = "MALE"
SOAPS=@SOAPS AND COOKING=@COOKING AND CHILDREN=@CHILDREN

0
 
LVL 51

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 250 total points
ID: 35183174
try this syntax:

Select * FROM MYTABLE
WHERE SSN = @SSN AND AGE=@AGE AND
and (
case  
when @sex='M' and TVSHOWS=@TVSHOWS AND RACING=@RACING AND NASCAR=@NASCAR then 1
when @sex='F' and SOAPS=@SOAPS AND COOKING=@COOKING AND CHILDREN=@CHILDREN then 1
else 0
end
) = 1




0
 
LVL 41

Accepted Solution

by:
Sharath earned 250 total points
ID: 35183296
Do you really need a dynamic sql for this? You can try like this.
SELECT * 
  FROM MYTABLE 
 WHERE SSN = @SSN 
       AND AGE = @AGE 
       AND ((@SEX = 'male' 
             AND TVSHOWS = @TVSHOWS 
             AND RACING = @RACING 
             AND NASCAR = @NASCAR) 
             OR (@SEX = 'Female' 
                 AND SOAPS = @SOAPS 
                 AND COOKING = @COOKING 
                 AND CHILDREN = @CHILDREN))

Open in new window

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:Internet_Engineer
ID: 35327419
Here is the solution:

(@SEX = 'male' AND (TVSHOWS=@TVSHOWS AND RACING=@RACING AND NASCAR=@NASCAR))

(@SEX = 'female' AND (TVSHOWS=@TVSHOWS AND RACING=@RACING AND NASCAR=@NASCAR))
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35329030
Solution was provided. Not sure why the asker wants to close this question without accepting any expert's post. In fact the solution posted by asker is wrong when checked with what he/she asked.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35330746
My recommendation is #3 and I suggest the posts http:#a35183296 and http:#a35183174 as answers.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 35390703
Starting auto-close process to implement the recommendations of the participating Expert(s).
 
modus_operandi
EE Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

749 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