Question

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

Working Code

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.

enter image description here

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top