Quantcast
Channel: SQLServerCentral » Reporting Services » Reporting Services » Latest topics
Viewing all articles
Browse latest Browse all 2791

Removing cursor rom report procedure

$
0
0
Hi,IS here any way hat I can get rid of the cursor from the procedure given below ? When I run this procedure in SSMS it retuns result in less tan 2 secs . The report locally takes 10 secs . However when I deploy the report into the report sever I get he error"Query execution failed for dataset 'dsTestReport'. (rsErrorExecutingCommand)"Code given belowALTER PROCEDURE [dbo].[CheckReport] @pStartDate varchar(20) , -- Change this @pEndDate varchar(20)-- , -- Change only if you want less than today.-- @pPostingCompany varchar(50) -- Change this to required posting companyWITH RECOMPILE AS BEGIN Declare @StartDate varchar(20) = @pStartDate -- Change this Declare @EndDate varchar(20) = @pEndDate -- Change only if you want less than today. declare @PostingCompany varchar(50) --= 'TGS-UK' -- Change this to required posting company --------- declare @sql varchar(max) declare @LocalDatabase varchar(30) declare @LocalCompany varchar(50) declare @WSAddress varchar(500) CREATE TABLE #TEst ( ID INT Identity(1,1),PostingCompany VARCHAR(100),G_LAccountNo VARCHAR(40),GLAccountName VARCHAR(500), Docno VARCHAR(200), Amount FLOAT , LC_Amount FLOAT )DECLARE cursor1 CURSOR FOR select Code,[Trans_ Dist_ WS Address] from [dbo].[Master Company$IC Partner]SET NOCOUNT ON OPEN cursor1 FETCH NEXT FROM cursor1 INTO @PostingCompany, @WSAddressWHILE @@FETCH_STATUS = 0BEGIN--WS Address values looks like below. Parse this to get database and company name --http://localhost:7049/TGS_NO/WS/TGS%20Nopec%20ASA/Codeunit/Integration_Receive_Trans_Distribset @WSAddress = substring(@WSAddress, charindex('/TGS',@WSAddress)+1, 1000)set @LocalDatabase = substring(@WSAddress, 1, charindex('/',@WSAddress)-1)set @WSAddress = substring(@WSAddress, charindex('/',@WSAddress)+1, 1000)set @WSAddress = substring(@WSAddress, charindex('/',@WSAddress)+1, 1000)set @LocalCompany = replace(substring(@WSAddress, 1, charindex('/',@WSAddress)-1), '%20', ' ')-- contract query and execute --where [Posting Company] = ''' + @pPostingCompany + '''' + set @sql = ' INSERT INTO #TEst ( PostingCompany ,G_LAccountNo ,GLAccountName , Docno , Amount , LC_Amount )select MasterCompany.*, LocalCompany.Amount AS [LC Amount]from ( select GLE.[Posting Company],GLE.[G_L Account No_], GLA.Name AS [GL Account Name], [Document No_], sum(Amount) Amount from [dbo].[Master Company$G_L Entry] GLE LEFT JOIN [dbo].[Master Company$G_L Account] GLA ON GLE.[G_L Account No_] = GLA.[No_] where [Posting Company] = ''' + @PostingCompany + '''' + ' AND [Posting Date] between ''' + @pStartDate + ''' and ''' + @pEndDate + '''' + ' group by [Posting Company],[G_L Account No_], GLA.Name, [Document No_]) MasterCompanyLEFT JOIN ( select [G_L Account No_], [Document No_], sum(Amount) Amount from ' + @LocalDatabase + '.dbo.[' + @LocalCompany + '$G_L Entry] where [Posting Date] between ''' + @pStartDate + ''' and ''' + @pEndDate + '''' + ' group by [G_L Account No_], [Document No_]) LocalCompany ON MasterCompany.[G_L Account No_] = LocalCompany.[G_L Account No_] AND MasterCompany.[Document No_] = LocalCompany.[Document No_]where (LocalCompany.[G_L Account No_] is null OR MasterCompany.Amount <> LocalCompany.Amount) '--print @sqlexec (@sql)FETCH NEXT FROM cursor1 INTO @PostingCompany, @WSAddressENDCLOSE cursor1DEALLOCATE cursor1SET NOCOUNT OFFSELECT PostingCompany ,G_LAccountNo ,GLAccountName , Docno , ISNULL(Amount,0) As Amount, ISNULL(LC_Amount,0) AS LC_Amount FROM #TEst Order By PostingCompanyDROP TABLE #TEstEND

Viewing all articles
Browse latest Browse all 2791

Trending Articles