﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / Adding column in where condtion dynamically / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 05:44:58 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Adding column in where condtion dynamically</title><link>http://www.sqlservercentral.com/Forums/Topic1407119-391-1.aspx</link><description>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.</description><pubDate>Tue, 15 Jan 2013 12:41:59 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Adding column in where condtion dynamically</title><link>http://www.sqlservercentral.com/Forums/Topic1407119-391-1.aspx</link><description>[quote][b]DiverKas (1/15/2013)[/b][hr]You would be better served with the last line like this:EXEC sp_executesql @SQLYou should have a higher plan reuse rate instead of the standard EXECUTE (@SQL)[/quote]AND it allows for parameterization which is vital if using dynamic sql.</description><pubDate>Tue, 15 Jan 2013 12:36:42 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Adding column in where condtion dynamically</title><link>http://www.sqlservercentral.com/Forums/Topic1407119-391-1.aspx</link><description>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.</description><pubDate>Tue, 15 Jan 2013 12:34:31 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Adding column in where condtion dynamically</title><link>http://www.sqlservercentral.com/Forums/Topic1407119-391-1.aspx</link><description>You would be better served with the last line like this:EXEC sp_executesql @SQLYou should have a higher plan reuse rate instead of the standard EXECUTE (@SQL)</description><pubDate>Tue, 15 Jan 2013 11:45:14 GMT</pubDate><dc:creator>DiverKas</dc:creator></item><item><title>RE: Adding column in where condtion dynamically</title><link>http://www.sqlservercentral.com/Forums/Topic1407119-391-1.aspx</link><description>Hi Kingston Dhasian,i implemented as per your inputs it is working as expected.thank you!!!</description><pubDate>Tue, 15 Jan 2013 03:36:25 GMT</pubDate><dc:creator>subramani.rudrappa 78855</dc:creator></item><item><title>RE: Adding column in where condtion dynamically</title><link>http://www.sqlservercentral.com/Forums/Topic1407119-391-1.aspx</link><description>You can implement this with Dynamic SQL as well[code="sql"]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' ENDEXECUTE ( @SQL )[/code]</description><pubDate>Tue, 15 Jan 2013 03:25:16 GMT</pubDate><dc:creator>Kingston Dhasian</dc:creator></item><item><title>Adding column in where condtion dynamically</title><link>http://www.sqlservercentral.com/Forums/Topic1407119-391-1.aspx</link><description>Hi,I have following tablesproductcustomeragentwe have column by name "state code" in product and customer.i have sql likeselect * 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=0when i use where condition in the above query it looks likeselect * from table name(we get table name dynamically like product,customer and agent)where statecode=0but 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.meanfor product and customer query should look like this select * from table name(we get table name dynamically like product,customer and agent)where statecode=0for 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=0elseselect * from table name(we get table name dynamically like product,customer and agent)am looking at, is there any other way to implement this?</description><pubDate>Tue, 15 Jan 2013 03:03:27 GMT</pubDate><dc:creator>subramani.rudrappa 78855</dc:creator></item></channel></rss>