Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-12 : 23:30:49
|
Hi everyone,I had the following stored procedure in my database. But when I execute it from my VB.NET website,it occured error.CREATE PROC sp_rpt_POByBranches ( @SellerID varchar (50), @BuyerID varchar (50), @FromDate datetime, @ToDate datetime, @OrderFlag int -- 0: Top Sales; 1: Top Quantity; 2: Item Desc)ASSET NOCOUNT ONDECLARE @SQL VARCHAR(8000), @BranchName VARCHAR (20), @BuyerGLN VARCHAR (13), @SQL1 VARCHAR(8000)SET @SQL = ''SET @SQL = @SQL + 'SELECT SellerItemNo, BuyerStyleNo, ItemDesc, '-- LOOP BRANCHDECLARE curBranch CURSOR LOCAL FORSELECT Distinct [Name], GLN FROM Tb_AddressWHERE CompanyID = @BuyerIDORDER BY [Name]OPEN curBranchFETCH FROM curBranch INTO @BranchName, @BuyerGLN WHILE @@fetch_status = 0 BEGIN SET @SQL = @SQL + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',' SET @SQL = @SQL + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_' + @BuyerGLN + ',' FETCH FROM curBranch INTO @BranchName, @BuyerGLN ENDCLOSE curBranchDEALLOCATE curBranchSET @SQL = @SQL + 'SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, 'SET @SQL = @SQL + 'SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL 'SET @SQL = @SQL + 'FROM Trn_PO_Trailers PT WITH (NOLOCK) 'SET @SQL = @SQL + 'LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] 'SET @SQL = @SQL + 'LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID 'SET @SQL = @SQL + 'WHERE CONVERT(VARCHAR,P.PODt,112) >= ''' + CONVERT(VARCHAR, @FromDate ,112) + ''' 'SET @SQL = @SQL + 'AND CONVERT(VARCHAR,P.PODt,112) <= ''' + CONVERT(VARCHAR, @ToDate ,112) + ''' 'SET @SQL = @SQL + 'AND P.SellerID = ''' + @SellerID + ''' 'SET @SQL = @SQL + 'AND P.BuyerID = ''' + @BuyerID + ''' 'SET @SQL = @SQL + 'GROUP BY SellerItemNo, BuyerStyleNo, ItemDesc '------------------------------------------------ SUM COLUMNSSET @SQL1 = ''SET @SQL1 = @SQL1 + 'UNION 'SET @SQL1 = @SQL1 + 'SELECT NULL, NULL, NULL, '-- LOOP BRANCHDECLARE curBranch1 CURSOR LOCAL FOR-- ATTN: THIS SELECT STATEMENT MUST BE SAME AS VB CODE - GetFieldList() SELECT Distinct [Name], GLN FROM Tb_AddressWHERE CompanyID = @BuyerIDORDER BY [Name]OPEN curBranch1FETCH FROM curBranch1 INTO @BranchName, @BuyerGLN WHILE @@fetch_status = 0 BEGIN SET @SQL1 = @SQL1 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',' SET @SQL1 = @SQL1 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_' + @BuyerGLN + ',' FETCH FROM curBranch1 INTO @BranchName, @BuyerGLN ENDCLOSE curBranch1DEALLOCATE curBranch1SET @SQL1 = @SQL1 + 'SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, 'SET @SQL1 = @SQL1 + 'SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL 'SET @SQL1 = @SQL1 + 'FROM Trn_PO_Trailers PT WITH (NOLOCK) 'SET @SQL1 = @SQL1 + 'LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] 'SET @SQL1 = @SQL1 + 'LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID 'SET @SQL1 = @SQL1 + 'WHERE CONVERT(VARCHAR,P.PODt,112) >= ''' + CONVERT(VARCHAR, @FromDate ,112) + ''' 'SET @SQL1 = @SQL1 + 'AND CONVERT(VARCHAR,P.PODt,112) <= ''' + CONVERT(VARCHAR, @ToDate ,112) + ''' 'SET @SQL1 = @SQL1 + 'AND P.SellerID = ''' + @SellerID + ''' 'SET @SQL1 = @SQL1 + 'AND P.BuyerID = ''' + @BuyerID + ''' '---------------------------------------------------SORTING IF @OrderFlag = 0 -- Top SalesBEGIN SET @SQL1 = @SQL1 + 'ORDER BY SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) DESC, ItemDesc, SellerItemNo, BuyerStyleNo 'END ELSE IF @OrderFlag = 1 -- Top QtyBEGIN SET @SQL1 = @SQL1 + 'ORDER BY SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) DESC, ItemDesc, SellerItemNo, BuyerStyleNo 'ENDELSE IF @OrderFlag = 2 -- Item DescBEGIN SET @SQL1 = @SQL1 + 'ORDER BY ItemDesc, SellerItemNo, BuyerStyleNo 'ENDEXECUTE(@SQL + @SQL1)GO The following is the error that I get:Incorrect syntax near the keyword 'SELECT'.Line 1: Incorrect syntax near 'PlaceDeli'.I am not able to figure out what cause the error. Can anyone help me to check?? |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 00:44:30
|
Change this line:EXECUTE(@SQL + @SQL1) to select (@SQL + @SQL1) so we can see what you're executing. Post the return here.Nathan Skerl |
 |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-13 : 00:51:01
|
| CREATE PROC sp_rpt_POByBranches ( @SellerID varchar (50), @BuyerID varchar (50), @FromDate datetime, @ToDate datetime, @OrderFlag int -- 0: Top Sales; 1: Top Quantity; 2: Item Desc)ASSET NOCOUNT ONDECLARE @SQL VARCHAR(8000), @BranchName VARCHAR (20), @BuyerGLN VARCHAR (13), @SQL1 VARCHAR(8000)SET @SQL = ''SET @SQL = @SQL + 'SELECT SellerItemNo, BuyerStyleNo, ItemDesc, '-- LOOP BRANCHDECLARE curBranch CURSOR LOCAL FORSELECT Distinct [Name], GLN FROM Tb_AddressWHERE CompanyID = @BuyerIDORDER BY [Name]OPEN curBranchFETCH FROM curBranch INTO @BranchName, @BuyerGLN WHILE @@fetch_status = 0 BEGIN SET @SQL = @SQL + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',' SET @SQL = @SQL + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_' + @BuyerGLN + ',' FETCH FROM curBranch INTO @BranchName, @BuyerGLN ENDCLOSE curBranchDEALLOCATE curBranchSET @SQL = @SQL + 'SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, 'SET @SQL = @SQL + 'SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL 'SET @SQL = @SQL + 'FROM Trn_PO_Trailers PT WITH (NOLOCK) 'SET @SQL = @SQL + 'LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] 'SET @SQL = @SQL + 'LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID 'SET @SQL = @SQL + 'WHERE CONVERT(VARCHAR,P.PODt,112) >= ''' + CONVERT(VARCHAR, @FromDate ,112) + ''' 'SET @SQL = @SQL + 'AND CONVERT(VARCHAR,P.PODt,112) <= ''' + CONVERT(VARCHAR, @ToDate ,112) + ''' 'SET @SQL = @SQL + 'AND P.SellerID = ''' + @SellerID + ''' 'SET @SQL = @SQL + 'AND P.BuyerID = ''' + @BuyerID + ''' 'SET @SQL = @SQL + 'GROUP BY SellerItemNo, BuyerStyleNo, ItemDesc '------------------------------------------------ SUM COLUMNSSET @SQL1 = ''SET @SQL1 = @SQL1 + 'UNION 'SET @SQL1 = @SQL1 + 'SELECT NULL, NULL, NULL, '-- LOOP BRANCHDECLARE curBranch1 CURSOR LOCAL FOR-- ATTN: THIS SELECT STATEMENT MUST BE SAME AS VB CODE - GetFieldList() SELECT Distinct [Name], GLN FROM Tb_AddressWHERE CompanyID = @BuyerIDORDER BY [Name]OPEN curBranch1FETCH FROM curBranch1 INTO @BranchName, @BuyerGLN WHILE @@fetch_status = 0 BEGIN SET @SQL1 = @SQL1 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',' SET @SQL1 = @SQL1 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_' + @BuyerGLN + ',' FETCH FROM curBranch1 INTO @BranchName, @BuyerGLN ENDCLOSE curBranch1DEALLOCATE curBranch1SET @SQL1 = @SQL1 + 'SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, 'SET @SQL1 = @SQL1 + 'SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL 'SET @SQL1 = @SQL1 + 'FROM Trn_PO_Trailers PT WITH (NOLOCK) 'SET @SQL1 = @SQL1 + 'LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] 'SET @SQL1 = @SQL1 + 'LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID 'SET @SQL1 = @SQL1 + 'WHERE CONVERT(VARCHAR,P.PODt,112) >= ''' + CONVERT(VARCHAR, @FromDate ,112) + ''' 'SET @SQL1 = @SQL1 + 'AND CONVERT(VARCHAR,P.PODt,112) <= ''' + CONVERT(VARCHAR, @ToDate ,112) + ''' 'SET @SQL1 = @SQL1 + 'AND P.SellerID = ''' + @SellerID + ''' 'SET @SQL1 = @SQL1 + 'AND P.BuyerID = ''' + @BuyerID + ''' '---------------------------------------------------SORTING IF @OrderFlag = 0 -- Top SalesBEGIN SET @SQL1 = @SQL1 + 'ORDER BY SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) DESC, ItemDesc, SellerItemNo, BuyerStyleNo 'END ELSE IF @OrderFlag = 1 -- Top QtyBEGIN SET @SQL1 = @SQL1 + 'ORDER BY SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) DESC, ItemDesc, SellerItemNo, BuyerStyleNo 'ENDELSE IF @OrderFlag = 2 -- Item DescBEGIN SET @SQL1 = @SQL1 + 'ORDER BY ItemDesc, SellerItemNo, BuyerStyleNo 'ENDSELECT(@SQL + @SQL1)GO |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 00:58:12
|
| No, we need you to alter the proc on your server, then execute it (using inputs that you know have caused the error), and show us the result from your proc.Does this make sense? |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 01:00:14
|
| The line I asked you to change will return the statement back to the calling application instead of executing it (and throwing the error). In this way we can see what is actually getting executed without triggering the error. |
 |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-13 : 01:22:35
|
the following is the select statement:SELECT SellerItemNo, BuyerStyleNo, ItemDesc, SUM(CASE WHEN PT.PlaceDelivery = '03011' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03011,SUM(CASE WHEN PT.PlaceDelivery = '03011' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) The following will be error if I using "Execute" instead of "Select":Server: Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'SELECT'.Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near 'PlaceDeli'.The following is the stored procedure that I edited to get the parameter in.CREATE PROC sp_rpt_POByBranches ASSET NOCOUNT ON DECLARE @SellerID varchar (50), DECLARE @BuyerID varchar (50), DECLARE @FromDate datetime, DECLARE @ToDate datetime, DECLARE @OrderFlag int -- 0: Top Sales; 1: Top Quantity; 2: Item DescSet SellerID='F9AC0427-5520-420F-841A-FD2EB8A97421'Set BuyerID='12BBC6AC-B1B7-44AB-A777-5D1B38531BDE'Set FromDate='2009-10-01'Set TODate='2009-10-13'Set OrderFlag='1'DECLARE @SQL VARCHAR(8000), @BranchName VARCHAR (20), @BuyerGLN VARCHAR (13), @SQL1 VARCHAR(8000)SET @SQL = ''SET @SQL = @SQL + 'SELECT SellerItemNo, BuyerStyleNo, ItemDesc, '-- LOOP BRANCHDECLARE curBranch CURSOR LOCAL FORSELECT Distinct [Name], GLN FROM Tb_AddressWHERE CompanyID = @BuyerIDORDER BY [Name]OPEN curBranchFETCH FROM curBranch INTO @BranchName, @BuyerGLN WHILE @@fetch_status = 0 BEGIN SET @SQL = @SQL + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',' SET @SQL = @SQL + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_' + @BuyerGLN + ',' FETCH FROM curBranch INTO @BranchName, @BuyerGLN ENDCLOSE curBranchDEALLOCATE curBranchSET @SQL = @SQL + 'SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, 'SET @SQL = @SQL + 'SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL 'SET @SQL = @SQL + 'FROM Trn_PO_Trailers PT WITH (NOLOCK) 'SET @SQL = @SQL + 'LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] 'SET @SQL = @SQL + 'LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID 'SET @SQL = @SQL + 'WHERE CONVERT(VARCHAR,P.PODt,112) >= ''' + CONVERT(VARCHAR, @FromDate ,112) + ''' 'SET @SQL = @SQL + 'AND CONVERT(VARCHAR,P.PODt,112) <= ''' + CONVERT(VARCHAR, @ToDate ,112) + ''' 'SET @SQL = @SQL + 'AND P.SellerID = ''' + @SellerID + ''' 'SET @SQL = @SQL + 'AND P.BuyerID = ''' + @BuyerID + ''' 'SET @SQL = @SQL + 'GROUP BY SellerItemNo, BuyerStyleNo, ItemDesc '------------------------------------------------ SUM COLUMNSSET @SQL1 = ''SET @SQL1 = @SQL1 + 'UNION 'SET @SQL1 = @SQL1 + 'SELECT NULL, NULL, NULL, '-- LOOP BRANCHDECLARE curBranch1 CURSOR LOCAL FOR-- ATTN: THIS SELECT STATEMENT MUST BE SAME AS VB CODE - GetFieldList() SELECT Distinct [Name], GLN FROM Tb_AddressWHERE CompanyID = @BuyerIDORDER BY [Name]OPEN curBranch1FETCH FROM curBranch1 INTO @BranchName, @BuyerGLN WHILE @@fetch_status = 0 BEGIN SET @SQL1 = @SQL1 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',' SET @SQL1 = @SQL1 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_' + @BuyerGLN + ',' FETCH FROM curBranch1 INTO @BranchName, @BuyerGLN ENDCLOSE curBranch1DEALLOCATE curBranch1SET @SQL1 = @SQL1 + 'SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, 'SET @SQL1 = @SQL1 + 'SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL 'SET @SQL1 = @SQL1 + 'FROM Trn_PO_Trailers PT WITH (NOLOCK) 'SET @SQL1 = @SQL1 + 'LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] 'SET @SQL1 = @SQL1 + 'LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID 'SET @SQL1 = @SQL1 + 'WHERE CONVERT(VARCHAR,P.PODt,112) >= ''' + CONVERT(VARCHAR, @FromDate ,112) + ''' 'SET @SQL1 = @SQL1 + 'AND CONVERT(VARCHAR,P.PODt,112) <= ''' + CONVERT(VARCHAR, @ToDate ,112) + ''' 'SET @SQL1 = @SQL1 + 'AND P.SellerID = ''' + @SellerID + ''' 'SET @SQL1 = @SQL1 + 'AND P.BuyerID = ''' + @BuyerID + ''' '---------------------------------------------------SORTING IF @OrderFlag = 0 -- Top SalesBEGIN SET @SQL1 = @SQL1 + 'ORDER BY SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) DESC, ItemDesc, SellerItemNo, BuyerStyleNo 'END ELSE IF @OrderFlag = 1 -- Top QtyBEGIN SET @SQL1 = @SQL1 + 'ORDER BY SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) DESC, ItemDesc, SellerItemNo, BuyerStyleNo 'ENDELSE IF @OrderFlag = 2 -- Item DescBEGIN SET @SQL1 = @SQL1 + 'ORDER BY ItemDesc, SellerItemNo, BuyerStyleNo 'ENDSELECT(@SQL + @SQL1)GO |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 01:41:42
|
You sure you pasted the entire statement in there? Seems like it got cutoff:SELECT SellerItemNo, BuyerStyleNo, ItemDesc, SUM( CASE WHEN PT.PlaceDelivery = '03011' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END ) AS TotalUnit_03011, SUM( CASE WHEN PT.PlaceDelivery = '03011' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 01:52:46
|
| Are you executing this in Management Studio? Make sure you have the options set to return more than the default 256 chars. You can find these options in Tools > Options > Query Results > SQL Server > Results to Grid -OR- Results to Text.If those are set too low the GUI will truncate the result. |
 |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-13 : 01:54:16
|
| @SQL:SELECT SellerItemNo, BuyerStyleNo, ItemDesc, SUM(CASE WHEN PT.PlaceDelivery = '03011' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03011,SUM(CASE WHEN PT.PlaceDelivery = '03011' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) @SQL1:UNION SELECT NULL, NULL, NULL, SUM(CASE WHEN PT.PlaceDelivery = '03011' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03011,SUM(CASE WHEN PT.PlaceDelivery = '03011' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) ASI am using sql server 2000,how can I set the option that you stated? |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 02:05:54
|
| I assume you are using Query Analyzer? Look in Query menu > current connection options > Advanced for a similar option. |
 |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-13 : 02:33:47
|
Hi,Yes,you are correct,there are some statement left out.Hereby attached posted the latest statement that I get:@SQLSELECT SellerItemNo, BuyerStyleNo, ItemDesc, SUM(CASE WHEN PT.PlaceDelivery = '03999' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03999,SUM(CASE WHEN PT.PlaceDelivery = '03999' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_03999,SUM(CASE WHEN PT.PlaceDelivery = '03002' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03002,SUM(CASE WHEN PT.PlaceDelivery = '03002' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_03002,SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL FROM Trn_PO_Trailers PT WITH (NOLOCK) LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID WHERE CONVERT(VARCHAR,P.PODt,112) >= '20091001' AND CONVERT(VARCHAR,P.PODt,112) <= '20091013' AND P.SellerID = 'F9AC0427-5520-420F-841A-FD2EB8A97421' AND P.BuyerID = '12BBC6AC-B1B7-44AB-A777-5D1B38531BDE' GROUP BY SellerItemNo, BuyerStyleNo, ItemDesc @SQL1:UNION SELECT NULL, NULL, NULL, SUM(CASE WHEN PT.PlaceDelivery = '03999' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03999,SUM(CASE WHEN PT.PlaceDelivery = '03999' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_03999,SUM(CASE WHEN PT.PlaceDelivery = '03002' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03002,SUM(CASE WHEN PT.PlaceDelivery = '03002' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_03002,SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL FROM Trn_PO_Trailers PT WITH (NOLOCK) LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID WHERE CONVERT(VARCHAR,P.PODt,112) >= '20091001' AND CONVERT(VARCHAR,P.PODt,112) <= '20091013' AND P.SellerID = 'F9AC0427-5520-420F-841A-FD2EB8A97421' AND P.BuyerID = '12BBC6AC-B1B7-44AB-A777-5D1B38531BDE' ORDER BY SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) DESC, ItemDesc, SellerItemNo, BuyerStyleNo |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 03:35:34
|
| The syntax looks fine. Do those statements run ok if you just exec them manually one by one (without the UNION) ?Nathan Skerl |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 03:44:35
|
| Only thing I can think is if the inputs are different when testing from your app. For example, if curBranch returns many rows then you will quickly exceed the 8000 limit of @SQL param, leading to truncation of one of the case statements... this is what is probably leading to the peculiar error "...near 'PlaceDeli' "It is being truncated at that point. |
 |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-13 : 03:55:53
|
| Ok,so is there anyway or any solution to solved this issues?Or will it be possible to separate the statement because I have no idea on it. |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 04:12:29
|
Does this query complete without error?SELECT SellerItemNo, BuyerStyleNo, ItemDesc, SUM(CASE WHEN PT.PlaceDelivery = '03999' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03999,SUM(CASE WHEN PT.PlaceDelivery = '03999' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_03999,SUM(CASE WHEN PT.PlaceDelivery = '03002' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03002,SUM(CASE WHEN PT.PlaceDelivery = '03002' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_03002,SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL FROM Trn_PO_Trailers PT WITH (NOLOCK) LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID WHERE CONVERT(VARCHAR,P.PODt,112) >= '20091001' AND CONVERT(VARCHAR,P.PODt,112) <= '20091013' AND P.SellerID = 'F9AC0427-5520-420F-841A-FD2EB8A97421' AND P.BuyerID = '12BBC6AC-B1B7-44AB-A777-5D1B38531BDE' GROUP BY SellerItemNo, BuyerStyleNo, ItemDesc UNION SELECT NULL, NULL, NULL, SUM(CASE WHEN PT.PlaceDelivery = '03999' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03999,SUM(CASE WHEN PT.PlaceDelivery = '03999' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_03999,SUM(CASE WHEN PT.PlaceDelivery = '03002' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_03002,SUM(CASE WHEN PT.PlaceDelivery = '03002' THEN (ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS NetAmt_03002,SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) AS TotalUnit_ALL, SUM((ISNULL(PD.CaseCost,0) * ISNULL(PT.Qty,0))) AS NetAmt_ALL FROM Trn_PO_Trailers PT WITH (NOLOCK) LEFT JOIN Trn_PO_Details PD WITH (NOLOCK) ON PD.POID = PT.POID AND PD.[LineNo] = PT.[LineNo] LEFT JOIN Trn_PO P WITH (NOLOCK) ON P.POID = PT.POID WHERE CONVERT(VARCHAR,P.PODt,112) >= '20091001' AND CONVERT(VARCHAR,P.PODt,112) <= '20091013' AND P.SellerID = 'F9AC0427-5520-420F-841A-FD2EB8A97421' AND P.BuyerID = '12BBC6AC-B1B7-44AB-A777-5D1B38531BDE' ORDER BY SUM((ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0))) DESC, ItemDesc, SellerItemNo, BuyerStyleNo Nathan Skerl |
 |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-13 : 04:23:01
|
| Yes, both of them completed without error. I am agree with you on the statement that if Curbranch returned many rows, the problem will occured. But if I deleted the looping for branch, the results will not be correct anymore. |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-13 : 10:07:31
|
| Thanks...I had viewed the post,but have no idea on it...I will appreciate if you could help me..Thanks,Nathan... |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-13 : 17:46:23
|
Sorry for delay... here is example of how you can gain another 8000 chars for your case statement loop. As is mentioned in the above link, this method is not optimal, but I suspect you did not write this proc and are simply stuck with fixing the issue at hand.Keep in mind if that cursor is very large you may need to extend this example script to accommodate more chars.declare @cmd1 varchar(8000), @cmd2 varchar(8000)set @cmd1 = ''set @cmd2 = ''declare curBranch cursor local forselect distinct [Name], GLN from Tb_Addresswhere CompanyID = @BuyerIDorder by [Name]open curBranchfetch from curBranch into @BranchName, @BuyerGLN while @@fetch_status = 0 begin -- check the length if len(@cmd1 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',') > 8000 begin set @cmd2 = @cmd2 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',' end else begin set @cmd1 = @cmd1 + 'SUM(CASE WHEN PT.PlaceDelivery = ''' + @BuyerGLN + ''' THEN (ISNULL(PD.PackSize,0) * ISNULL(PT.Qty,0)) ELSE 0 END) AS TotalUnit_' + @BuyerGLN + ',' end fetch from curBranch into @BranchName, @BuyerGLN endclose curBranchdeallocate curBranch |
 |
|
|
daniel50096230
Yak Posting Veteran
99 Posts |
Posted - 2009-10-22 : 04:38:35
|
| Hi, after I changing this stored procedure, how should I combine it together with others SQL such as @SQL and @SQL1? |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2009-10-22 : 12:32:51
|
You will need to combine within the exec statement in the same way youre currently doing it:EXECUTE(@SQL + @SQL1) Nathan Skerl |
 |
|
|
Next Page
|
|
|
|
|