<p>I have a query that is dynamically built after looking up a field list and table name.  I execute this dynamic query inside a stored proc.  The query is built without a where clause when the two proc parameters are zero, and built with a where clause when not.</p>

<p>When I execute the proc with 
    SET FMTONLY ON
    exec [cpExportRecordType_ListByExportAgentID] null, null
It returns no column information.  I have just now replaced building the query without a where clause to just executing the same query directly, and now I get column information.  I would love to know what causes this, anyone?</p>

[{'answer_id': 73268, 'author': 'kristof', 'author_id': 3241, 'author_profile': 'https://Stackoverflow.com/users/3241', 'pm_score': 1, 'selected': False, 'text': '<p>Perhaps it is related to the fact that the passed parameters are NULL, \ncheck how your query is build perhaps it behaves in different way then expected when you pass NULL.\nDoes you proc returns expected results when you call:\n    SET FMTONLY OFF exec [cpExportRecordType_ListByExportAgentID] null, null\n?</p>\n\n<p>Other possibility: \nI understand that you build your query dynamically by getting results from calling another queries to get the column names.\nPerhaps the query that would normally give you the column names returns no data but only column information (SET FMTONLY ON) so you do not have data to build you dynamic query.</p>\n'}, {'answer_id': 81367, 'author': 'ProfK', 'author_id': 8741, 'author_profile': 'https://Stackoverflow.com/users/8741', 'pm_score': 1, 'selected': True, 'text': '<blockquote>\n  <p>kristof:</p>\n  \n  <blockquote>\n    <p>so you do not have data to build you dynamic query.</p>\n  </blockquote>\n</blockquote>\n\n<p>With null parameters my dynamic query was a pure string literal, independent of data.  Changing it to a static query solved the problem.</p>\n'}]
['https://Stackoverflow.com/questions/72185', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8741/']