Hola saludos a todos, quisiera que me ayudaran con un problema de un query que no me funciona en el el query manager, me sale Acuerdo Globa OOTA o Validaciones por usuario CSHS, el quey funciona bien pero cuando lo monto en query manager me sale ese error. Les dejo el query:
DECLARE @CardCode AS VARCHAR(20)
DECLARE @Fecha_Inicial AS DateTime
DECLARE @Fecha_Final AS DateTime
SET @CardCode = '[%0]'
SET DateFormat DMY
SET @Fecha_Inicial = '[%1]'
SET @Fecha_Final = '[%2]'
IF(@CardCode <> '')
SELECT
T7.Numero_Pago,
T7.Fecha_Documento,
T7.Nombre_Proveedor,
T7.No_Ref_Acreedor,
T7.Fecha_Factura,
T7.SubTotal,
T7.IVA,
T7.Retenciones,
T7.[1] AS Retefuente,
T7.[2] AS Reteica,
T7.Total_Factura,
T7.Importe_Vencido_Total
FROM (
SELECT *
FROM (
SELECT
T0.DocNum AS Numero_Pago
--, T0.CardCode AS Codigo_Proveedor
, T0.CardName AS Nombre_Proveedor
--, CONVERT(VARCHAR(8), T0.DocDate, 3) AS Fecha_Contabilizacion
--, CONVERT(VARCHAR(8), T0.DocDueDate, 3) AS Fecha_Vencimiento
, CONVERT(VARCHAR(8), T0.TaxDate, 3) AS Fecha_Documento
--, T2.DocNum AS Factura_Proveedor
--, T2.NumAtCard AS No_Ref_Acreedor
, CASE
WHEN T2.NumAtCard IS NULL THEN 'No tiene No. de referencia'
WHEN T2.NumAtCard = '' THEN 'No tiene No. de referencia'
ELSE T2.NumAtCard
END AS No_Ref_Acreedor
, CONVERT(VARCHAR(8), T2.DocDate, 3) AS Fecha_Factura
, SUM(T2.DocTotal + T2.WTSum - T2.VatSum) AS SubTotal
, T2.VatSum AS IVA
, T2.WTSum AS Retenciones
, ROW_NUMBER() OVER (PARTITION BY T2.NumAtCard ORDER BY T2.NumAtCard) AS Row_Number
--, T4.WTCode AS Detalle_Retencion
, T3.U_Ret_ML
--, CONVERT(VARCHAR(8), T2.DocDueDate, 3) AS Fecha_Vencimiento_Factura
, T2.DocTotal AS Total_Factura
, T0.DocTotal AS Importe_Vencido_Total
--, T2.Comments AS Comentario_1
--, T2.JrnlMemo AS Comentario_2
FROM OVPM T0
LEFT JOIN VPM2 T1 ON T0.DocNum = T1.DocNum
LEFT JOIN OPCH T2 ON T1.DocEntry = T2.DocEntry
LEFT JOIN PCH5 T3 ON T3.AbsEntry = T2.DocEntry
--INNER JOIN INV5 T4 ON T4.AbsEntry = T2.DocEntry
WHERE T0.CardCode = @CardCode
AND T0.TaxDate Between @Fecha_Inicial And @Fecha_Final
GROUP BY T0.DocNum, T0.CardName, CONVERT(VARCHAR(8), T0.TaxDate, 3), CASE
WHEN T2.NumAtCard IS NULL THEN 'No tiene No. de referencia'
WHEN T2.NumAtCard = '' THEN 'No tiene No. de referencia'
ELSE T2.NumAtCard
END, CONVERT(VARCHAR(8), T2.DocDate, 3), T2.VatSum, T2.WTSum, T3.U_Ret_ML, T2.DocTotal, T0.DocTotal, T2.NumAtCard) T5
PIVOT (
SUM(T5.U_Ret_ML)
FOR T5.Row_Number IN ([1], [2])
) AS P) T7
ELSE
SELECT
T8.Numero_Pago,
T8.Fecha_Documento,
T8.Nombre_Proveedor,
T8.No_Ref_Acreedor,
T8.Fecha_Factura,
T8.SubTotal,
T8.IVA,
T8.Retenciones,
T8.[1] AS Retefuente,
T8.[2] AS Reteica,
T8.Total_Factura,
T8.Importe_Vencido_Total
FROM (
SELECT *
FROM (
SELECT
T0.DocNum AS Numero_Pago
--, T0.CardCode AS Codigo_Proveedor
, T0.CardName AS Nombre_Proveedor
--, CONVERT(VARCHAR(8), T0.DocDate, 3) AS Fecha_Contabilizacion
--, CONVERT(VARCHAR(8), T0.DocDueDate, 3) AS Fecha_Vencimiento
, CONVERT(VARCHAR(8), T0.TaxDate, 3) AS Fecha_Documento
--, T2.DocNum AS Factura_Proveedor
--, T2.NumAtCard AS No_Ref_Acreedor
, CASE
WHEN T2.NumAtCard IS NULL THEN 'No tiene No. de referencia'
WHEN T2.NumAtCard = '' THEN 'No tiene No. de referencia'
ELSE T2.NumAtCard
END AS No_Ref_Acreedor
, CONVERT(VARCHAR(8), T2.DocDate, 3) AS Fecha_Factura
, SUM(T2.DocTotal + T2.WTSum - T2.VatSum) AS SubTotal
, T2.VatSum AS IVA
, T2.WTSum AS Retenciones
, ROW_NUMBER() OVER (PARTITION BY T2.NumAtCard ORDER BY T2.NumAtCard) AS Row_Number
--, T4.WTCode AS Detalle_Retencion
, T3.U_Ret_ML
--, CONVERT(VARCHAR(8), T2.DocDueDate, 3) AS Fecha_Vencimiento_Factura
, T2.DocTotal AS Total_Factura
, T0.DocTotal AS Importe_Vencido_Total
--, T2.Comments AS Comentario_1
--, T2.JrnlMemo AS Comentario_2
FROM OVPM T0
LEFT JOIN VPM2 T1 ON T0.DocNum = T1.DocNum
LEFT JOIN OPCH T2 ON T1.DocEntry = T2.DocEntry
LEFT JOIN PCH5 T3 ON T3.AbsEntry = T2.DocEntry
--INNER JOIN INV5 T4 ON T4.AbsEntry = T2.DocEntry
WHERE T0.TaxDate Between @Fecha_Inicial And @Fecha_Final
GROUP BY T0.DocNum, T0.CardName, CONVERT(VARCHAR(8), T0.TaxDate, 3), CASE
WHEN T2.NumAtCard IS NULL THEN 'No tiene No. de referencia'
WHEN T2.NumAtCard = '' THEN 'No tiene No. de referencia'
ELSE T2.NumAtCard
END, CONVERT(VARCHAR(8), T2.DocDate, 3), T2.VatSum, T2.WTSum, T3.U_Ret_ML, T2.DocTotal, T0.DocTotal, T2.NumAtCard) T6
PIVOT (
SUM(T6.U_Ret_ML)
FOR T6.Row_Number IN ([1], [2])
) AS P ) T8
Les agradeceria ayuda.