Trouble with Dynamic T-SQL IIF Statement

跟風遠走 提交于 2019-12-12 20:23:38

问题


This code works fine and does exactly what I want, which is to sum the Qty * Price for each instance of the dynamic query.

But when I add an IIF statement it breaks. What I am trying to do is the same thing as above but when the transaction type is 'CO' set the sum to a negative amount.


回答1:


The problem turned out to be the NVARCHAR(4000) type of @sql, limiting its length to 4000 characters: the query got truncated at some random place after adding another long chunk to it.

DECLARE @sql NVARCHAR(MAX) solves the problem, allowing a dynamic query of any size below 2GB.



来源:https://stackoverflow.com/questions/14862086/trouble-with-dynamic-t-sql-iif-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!