I am getting an error when I run this query because the data type money can't be implicitly converted to a varchar. However, I am using an if statemnt to make sure the data type is not money before I try the conversion. Clearly, the conversion is being executed anyways. Anyone know why?
table: BBH_NEW col: rebate2
datatype: money
if 'money'= 'money'
begin
if (select max([rebate2]) from [BBH_NEW]) = 0
and (select min([rebate2]) from [BBH_NEW]) = 0
print ' rebate2 '
print ' 1 '
end
if 'money'!= 'money'
begin
IF NOT EXISTS (SELECT top 1 * FROM [BBH_NEW] WHERE [rebate2] IS NOT NULL and
len([rebate2]) > 0 )
BEGIN
print ' rebate2 '
end
end
Error:
Msg 257, Level 16, State 3, Line 11
Implicit conversion from data type money to varchar is not allowed. Use the CONVERT function to run this query.
yes this code was generated. If it helps, this is the code which was used to produce it:
select @temp =
data_type FROM information_schema.columns
WHERE table_schema = 'dbo'
AND table_name = @tblname
AND column_name = @col
SELECT @hold =
'if '''+@temp+'''= ''money''
begin
if (select max(['+@col+']) from ['+@tblname+']) = 0
and (select min(['+@col+']) from ['+@tblname+']) = 0
print '' '+@col+' money''
end
if '''+@temp+'''!= ''money''
begin
IF NOT EXISTS (SELECT max([' + @col + ']) FROM ['+ @tblname + ']
WHERE len( [' + @col + ']) > 0 )
BEGIN
print '' ' + @col + ' ''
end
end'