Hi experts,
I have this query which should be run on the Query Manager but it just gives me this error: "1). [Microsoft][SQL Serve Native CLient 10.0]Invalid cursor state" The weird thing is, it perfectly runs in the SQL Server and in other SAP B1 server. Do any one knows how this can be fixed.
Here's the query:
DECLARE @columns NVARCHAR(MAX), @sql NVARCHAR(MAX);
SET @columns = N'';
SELECT @columns += N', p.' + QUOTENAME(PRCCODE)
FROM (SELECT O.PrcCode FROM dbo.OOCR AS p
INNER JOIN dbo.OCR1 AS o
ON p.ocrcode = o.ocrCode
WHERE P.DIMCODE = 1 and p.Direct = 'Y'
GROUP BY O.PRCcode) AS x;
SET @sql = N'
SELECT ocrcode as [Cost Center Code], ' + STUFF(@columns, 1, 2, '') + '
FROM
(
SELECT o.prccode, o.prcamount, p.ocrcode
FROM dbo.OOCR AS p
INNER JOIN dbo.OCR1 AS o
ON p.ocrcode = o.ocrcode
where o.validto is null
) AS j
PIVOT
(
max(PRCAmount) FOR prccode IN ('
+ STUFF(REPLACE(@columns, ', p.[', ',['), 1, 1, '')
+ ')
) AS p;';
PRINT @sql;
EXEC sp_executesql @sql;
Thanks in advance,
Malu