Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I understand it the column BBH_NEW.rebate2 is of type <code>money</code> when you get the error. In T-SQL you can't have a query that doesn't compile, and that is what you are encountering. Even though the query in the always-false if block won't run, it doesn't compile because the data types don't match.</p> <p>First, a quick solution for you - use CONVERT or CAST to explicitly change the data type.</p> <pre><code>if 'money'!= 'money' begin IF NOT EXISTS (SELECT top 1 * FROM [BBH_NEW] WHERE [rebate2] IS NOT NULL and len(CONVERT(VARCHAR(8000), [rebate2])) &gt; 0 ) BEGIN print ' rebate2 ' end end </code></pre> <p>But, there has to be a better way to do whatever you are doing... When does that SQL get generated? If it is at runtime, can you just not generate the part that won't run? Maybe something like this?</p> <pre><code>SELECT @hold = CASE WHEN @temp = 'money' THEN 'if (select max(['+@col+']) from ['+@tblname+']) = 0 and (select min(['+@col+']) from ['+@tblname+']) = 0 print '' '+@col+' money''' ELSE 'IF NOT EXISTS (SELECT max([' + @col + ']) FROM ['+ @tblname + '] WHERE len( [' + @col + ']) &gt; 0 ) BEGIN print '' ' + @col + ' '' end' END </code></pre> <p>or maybe change the generation to this...</p> <pre><code>SELECT @temp = DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = @tblname AND COLUMN_NAME = @col IF(@temp = 'money') SELECT @hold = 'IF(EXISTS( SELECT 1 FROM ['+@tblname+'] HAVING MAX(['+@col+']) = 0 AND MIN(['+@col+']) = 0)) BEGIN PRINT '' '+@col+' '' END'; ELSE SELECT @hold = 'IF(NOT EXISTS( SELECT * FROM ['+@tblname+'] WHERE ['+@col+'] IS NOT NULL AND LEN(['+@col+']) &gt; 0)) BEGIN PRINT '' '+@col+' '' END'; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload