Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Adding column in where condtion dynamically Expand / Collapse
Author
Message
Posted Tuesday, January 15, 2013 3:03 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 3, 2014 5:09 AM
Points: 45, Visits: 111
Hi,

I have following tables

product
customer
agent

we have column by name "state code" in product and customer.

i have sql like

select * from table name(we get table name dynamically like product,customer and agent using variable)

i need to add condition to get records with statecode=0

when i use where condition in the above query it looks like

select * from table name(we get table name dynamically like product,customer and agent)
where statecode=0

but this dynamic query fails for table "agent" as agent table does not contain statecode column.

so how can i wirte query where i get "where state code=0" condtion only for product and customer but not to agent.

mean

for product and customer query should look like this
select * from table name(we get table name dynamically like product,customer and agent)
where statecode=0

for agent :

select * from table name(we get table name dynamically like product,customer and agent)


i have tried

if (variable='product' or variable='customer')
select * from table name(we get table name dynamically like product,customer and agent)
where statecode=0

else
select * from table name(we get table name dynamically like product,customer and agent)

am looking at, is there any other way to implement this?



Post #1407119
Posted Tuesday, January 15, 2013 3:25 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 8:33 AM
Points: 2,616, Visits: 4,720
You can implement this with Dynamic SQL as well

DECLARE	@TableName VARCHAR(100), @SQL VARCHAR(1000)

SET @TableName = 'agent'

SET @SQL = ' SELECT * '
+ ' FROM ' + @TableName
+ ' WHERE ' + CASE WHEN @TableName IN ( 'product', 'customer' ) THEN 'statecode = 0' ELSE '1=1' END


EXECUTE ( @SQL )




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1407129
Posted Tuesday, January 15, 2013 3:36 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 3, 2014 5:09 AM
Points: 45, Visits: 111
Hi Kingston Dhasian,

i implemented as per your inputs it is working as expected.

thank you!!!

Post #1407134
Posted Tuesday, January 15, 2013 11:45 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, August 16, 2013 8:28 AM
Points: 249, Visits: 460
You would be better served with the last line like this:

EXEC sp_executesql @SQL

You should have a higher plan reuse rate instead of the standard EXECUTE (@SQL)
Post #1407408
Posted Tuesday, January 15, 2013 12:34 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 13,110, Visits: 11,940
Or even better create 3 stored procs to retrieve your data. You are using the "one query fits all via dynamic sql" approach. This is full of issues. It is difficult to maintain, once business rules start diverging you have really complicated code (this sounds the case here), you are running the risk of sql injection (maybe not now but in the future).

The code that Kingston shared will work but breaking this into separate pieces is really the best move in the long run.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1407427
Posted Tuesday, January 15, 2013 12:36 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 13,110, Visits: 11,940
DiverKas (1/15/2013)
You would be better served with the last line like this:

EXEC sp_executesql @SQL

You should have a higher plan reuse rate instead of the standard EXECUTE (@SQL)


AND it allows for parameterization which is vital if using dynamic sql.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1407430
Posted Tuesday, January 15, 2013 12:41 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 11:19 AM
Points: 42,466, Visits: 35,529
I strongly recommend against this approach. It's a security risk and a maintenance nightmare. You wouldn't develop a C# method that could handle customers or products depending on a parameter passed, don't do it to SQL procedures.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1407433
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse