<p>I want to ask how other programmers are producing Dynamic SQL strings for execution as the CommandText of a SQLCommand object.</p>

<p>I am producing parameterized queries containing user-generated WHERE clauses and SELECT fields. Sometimes the queries are complex and I need a lot of control over how the different parts are built. </p>

<p>Currently, I am using many loops and switch statements to produce the necessary SQL code fragments and to create the SQL parameters objects needed. This method is difficult to follow and it makes maintenance a real chore. </p>

<p>Is there a cleaner, more stable way of doing this?</p>

<p>Any Suggestions?</p>

<p>EDIT:
To add detail to my previous post:</p>

<ol>
<li>I cannot really template my query due to the requirements. It just changes too much.</li>
<li>I have to allow for aggregate functions, like Count(). This has consequences for the Group By/Having clause.  It also causes nested SELECT statements. This, in turn, effects the column name used by </li>
<li>Some Contact data is stored in an XML column. Users can query this data AS WELL AS and the other relational columns together. Consequences are that xmlcolumns cannot appear in Group By clauses[sql syntax].</li>
<li>I am using an efficient paging technique that uses Row_Number() SQL Function. Consequences are that I have to use a Temp table and then get the @@rowcount, before selecting my subset, to avoid a second query.</li>
</ol>

<p>I will show some code (the horror!) so that you guys have an idea of what I'm dealing with.</p>

<pre><code>sqlCmd.CommandText = "DECLARE @t Table(ContactId int, ROWRANK int" + declare
      + ")INSERT INTO @t(ContactId, ROWRANK" + insertFields + ")"//Insert as few cols a possible
      + "Select ContactID, ROW_NUMBER() OVER (ORDER BY " + sortExpression + " "
      + sortDirection + ") as ROWRANK" // generates a rowrank for each row
      + outerFields
      + " FROM ( SELECT c.id AS ContactID"
      + coreFields
      + from         // sometimes different tables are required 
      + where + ") T " // user input goes here.
      + groupBy+ " "
      + havingClause //can be empty
      + ";"
      + "select @@rowcount as rCount;" // return 2 recordsets, avoids second query
      + " SELECT " + fields + ",field1,field2" // join onto the other cols n the table
      +" FROM @t t INNER JOIN contacts c on t.ContactID = c.id" 
      +" WHERE ROWRANK BETWEEN " + ((pageIndex * pageSize) + 1) + " AND " 
      + ( (pageIndex + 1) * pageSize); // here I select the pages I want
</code></pre>

<p>In this example, I am querying XML data. For purely relational data, the query is much more simple. Each of the section variables are StringBuilders. Where clauses are built like so:</p>

<pre><code>// Add Parameter to SQL Command
AddParamToSQLCmd(sqlCmd, "@p" + z.ToString(), SqlDbType.VarChar, 50, ParameterDirection.Input, qc.FieldValue);
// Create SQL code Fragment
where.AppendFormat(" {0} {1} {2} @p{3}", qc.BooleanOperator, qc.FieldName, qc.ComparisonOperator, z);
</code></pre>

[{'answer_id': 51839, 'author': 'Ryan Lanciaux', 'author_id': 1385358, 'author_profile': 'https://Stackoverflow.com/users/1385358', 'pm_score': 0, 'selected': False, 'text': "<p>Out of curiousity, have you considered using an ORM for managing your data access. A lot of the functionality you're trying to implement could already be there. It may be something to look at because its best not to re-invent the wheel.</p>\n"}, {'answer_id': 51841, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 1, 'selected': False, 'text': '<p>You could try the approach used by code generation tools like CodeSmith. Create a SQL template with placeholders. At runtime, read the template into a string and substitute the placeholders with actual values. This is only useful if all the SQL code follow a pattern.</p>\n'}, {'answer_id': 51848, 'author': 'Yaakov Ellis', 'author_id': 51, 'author_profile': 'https://Stackoverflow.com/users/51', 'pm_score': 3, 'selected': True, 'text': "<p>I had the need to do this on one of my recent projects. Here is the scheme that I am using for generating the SQL: </p>\n\n<ul>\n<li>Each component of the query is represented by an Object (which in my case is a Linq-to-Sql entity that maps to a table in the DB). So I have the following classes: Query, SelectColumn, Join, WhereCondition, Sort, GroupBy. Each of these classes contains all details relating to that component of the query.</li>\n<li>The last five classes are all related to a Query object. So the Query object itself has collections of each class.</li>\n<li>Each class has a method that can generate the SQL for the part of the query that it represents. So creating the overall query ends up calling Query.GenerateQuery() which in turn enumerates through all of the sub-collections and calls their respective GenerateQuery() methods</li>\n</ul>\n\n<p>It is still a bit complicated, but in the end you know where the SQL generation for each individual part of the query originates (and I don't think that there are any big switch statements). And don't forget to use StringBuilder.</p>\n"}, {'answer_id': 51935, 'author': 'Scott Lawrence', 'author_id': 3475, 'author_profile': 'https://Stackoverflow.com/users/3475', 'pm_score': 1, 'selected': False, 'text': "<p>Gulzar and Ryan Lanciaux make good points in mentioning CodeSmith and ORM.  Either of those might reduce or eliminate your current burden when it comes to generating dynamic SQL.  Your current approach of using parameterized SQL is wise, simply because it protects well against SQL injection attacks.</p>\n\n<p>Without an actual code sample to comment on, it's difficult to provide an informed alternative to the loops and switch statements you're currently using.  But since you mention that you're setting a CommandText property, I would recommend the use of string.Format in your implementation (if you aren't already using it).  I think it may make your code easier to restructure, and therefore improve readability and understanding.</p>\n"}, {'answer_id': 51938, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': '<p>Usually it\'s something like this:</p>\n\n<pre><code>string query= "SELECT {0} FROM .... WHERE {1}"\nStringBuilder selectclause = new StringBuilder();\nStringBuilder wherecaluse = new StringBuilder();\n\n// .... the logic here will vary greatly depending on what your system looks like\n\nMySqlcommand.CommandText = String.Format(query, selectclause.ToString(), whereclause.ToString());\n</code></pre>\n\n<p>I\'m also just getting started out with ORMs.  You might want to take a look at one of those.  ActiveRecord / Hibernate are some good keywords to google.</p>\n'}, {'answer_id': 51970, 'author': 'Anthony Mastrean', 'author_id': 3619, 'author_profile': 'https://Stackoverflow.com/users/3619', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://en.wikipedia.org/wiki/Object-relational_mapping" rel="nofollow noreferrer">ORM</a>s have already solved the problem of dynamic SQL generation (I prefer <a href="http://www.hibernate.org/343.html" rel="nofollow noreferrer">NHibernate</a>/<a href="http://www.castleproject.org/activerecord/index.html" rel="nofollow noreferrer">ActiveRecord</a>). Using these tools you can create a query with an unknown number of conditions by looping across user input and generating an array of Expression objects. Then execute the built-in query methods with that custom expression set.</p>\n\n<pre><code>List&lt;Expression&gt; expressions = new List&lt;Expression&gt;(userConditions.Count);\nforeach(Condition c in userConditions)\n{\n    expressions.Add(Expression.Eq(c.Field, c.Value));\n}\nSomeTable[] records = SomeTable.Find(expressions);\n</code></pre>\n\n<p>There are more \'Expression\' options: non-equality, greater/less than, null/not-null, etc. The \'Condition\' type I just made up, you can probably stuff your user input into a useful class.</p>\n'}, {'answer_id': 52100, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 1, 'selected': False, 'text': "<p>If you really need to do this from code, then an ORM is probably the way to go to try to keep it clean.</p>\n\n<p>But I'd like to offer an alternative that works well and could avoid the performance problems that accompany dynamic queries, due to changing SQL that requires new query plans to be created, with different demands on indexes.</p>\n\n<p>Create a stored procedure that accepts all possible parameters, and then use something like this in the where clause:</p>\n\n<pre><code>where...\nand (@MyParam5 is null or @MyParam5 = Col5)\n</code></pre>\n\n<p>then, from code, it's much simpler to set the parameter value to DBNull.Value when it is not applicable, rather than changing the SQL string you generate.</p>\n\n<p>Your DBAs will be much happier with you, because they will have one place to go for query tuning, the SQL will be easy to read, and they won't have to dig through profiler traces to find the many different queries being generated by your code.</p>\n"}, {'answer_id': 52119, 'author': 'mattruma', 'author_id': 1768, 'author_profile': 'https://Stackoverflow.com/users/1768', 'pm_score': 2, 'selected': False, 'text': '<p>We created our own FilterCriteria object that is kind of a <em>black-box</em> dynamic query builder.  It has collection properties for SelectClause, WhereClause, GroupByClause and OrderByClause.  It also contains a properties for CommandText, CommandType, and MaximumRecords.</p>\n\n<p>We then jut pass our FilterCriteria object to our data logic and it executes it against the database server and passes parameter values to a stored procedure that executes the dynamic code. </p>\n\n<p>Works well for us ... and keeps the SQL generation nicely contained in an object.</p>\n'}]
['https://Stackoverflow.com/questions/51827', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5197/']