Solved

# Dynamic SQL statement

Posted on 2011-03-21
Medium Priority
248 Views
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
Question by:Internet_Engineer

LVL 5

Expert Comment

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 61

Assisted Solution

HainKurt earned 1000 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

Sharath earned 1000 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))
``````
0

Author Comment

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

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

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

LVL 1

Expert Comment

ID: 35390703
Starting auto-close process to implement the recommendations of the participating Expert(s).

modus_operandi
0

## Featured Post

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
###### Suggested Courses
Course of the Month12 days, 21 hours left to enroll

#### 578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.