Hi Everyone,
I have been tasked with creating a report which allows for the searching of Quotes. One of the reports output requirements is that I show any Sales Orders (where they exist) which relate to a given Quote number.
Here is the code that I have written so far -
DECLARE @custCode nvarchar(10), @itemFrom nvarchar(30), @itemTo nvarchar(30), @dateFrom DATE, @dateTo DATE SET @custCode = '' IF @custCode = '' SET @custCode = '%' SET @itemFrom = '' IF @itemFrom = '' SET @itemFrom = '00%' SET @itemTo = '' + 'Z' IF @itemTo = 'Z' SET @itemTo = 'ZZZZZ%' SET @dateFrom = '' IF @dateFrom = '' SET @dateFrom = '1999-01-01' SET @dateTo = '2015-01-27' IF @dateTo = '1753-01-01' SET @dateTo = GETDATE() SELECT T0.DocNum AS 'Quote #' ,(SELECT Ta.DocNum FROM ORDR Ta INNER JOIN RDR1 Tb on Tb.DocEntry = Ta.DocEntry AND Tb.LineNum = T1.BaseLine WHERE Tb.BaseType = '23' AND Tb.BaseEntry = T1.DocEntry) AS 'Sales Order #' , CASE WHEN T0.DocStatus = 'C' THEN 'Closed' ELSE 'Open' END AS Status , T0.DocDate AS 'Doc Date' , T0.CardCode AS 'Cust Code' , T0.CardName AS 'Cust Name' , T1.ItemCode AS 'Item Code' , T1.Dscription AS 'Item Description' , T1.Quantity AS 'Quantity' , T1.Price , T1.LineTotal , T2.SlpName AS 'Sales Person' FROM OQUT T0 LEFT JOIN QUT1 T1 ON T1.DocEntry = T0.DocEntry LEFT JOIN OSLP T2 ON T2.SlpCode = T0.SlpCode WHERE T0.CardCode LIKE (@custCode) AND (T1.ItemCode >= @itemFrom AND T1.ItemCode <= @itemTo) AND (T0.DocDate >= @dateFrom AND T0.DocDate <= @dateTo) AND T0.CANCELED = 'N' ORDER BY [Quote #]
My challenge specifically relates to the INNER SELECT, as it is causing the following error in SSMS.
Msg 512, Level 16, State 1, Line 36
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Any help with resolving this error will be greatly appreciated.
Kind Regards,
David