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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error Query SQL Dynamic

Author  Topic 

dinhson_vn
Starting Member

15 Posts

Posted - 2015-05-02 : 06:39:32
Dear Everybody,

I first wrote script dynamic but error. Please see the following error in script.

Thanks very much.


DECLARE @FromDate AS DATETIME,
@ToDate AS DATETIME,
@CompanyCode VARCHAR(30)

DECLARE @SQL NVARCHAR(MAX)
SET @FromDate = '05-05-13'
SET @ToDate = '05-31-13'
SET @CompanyCode = 'US'
set @sql = 'SELECT *,
CASE
WHEN [Gen_ Prod_ Posting Group] ="RETAIL" THEN "RETAIL"
WHEN [Gen_ Prod_ Posting Group] ="DEPOSIT" THEN "DEPOSIT"
WHEN [Gen_ Prod_ Posting Group] ="EMPTY" THEN "EMPTY"
WHEN [Gen_ Prod_ Posting Group] ="EXC" THEN "EXC"
ELSE "DISCOUNT"
END AS GPPG'
set @sql =@sql +' INTO #Temp2'
set @sql =@sql +'FROM ['+@CompanyCode+'$Value Entry]'
set @sql =@sql + 'WHERE [Posting Date] BETWEEN @FromDate AND @ToDate'
set @sql =@sql + 'AND ([Document Type] = 2 OR [Document Type] = 4)'
set @sql =@sql +'AND [Source Code] = "SALES"
AND (
[Gen_ Prod_ Posting Group] <> "DEPOSIT"
AND [Gen_ Prod_ Posting Group] <> "EMPTY"
)'
set @sql =@sql +'AND [Item No_] LIKE "10%"'
set @sql =@sql +'SELECT [Item No_],
[Description],
[Item Ledger Entry Type],
[Gen_ Prod_ Posting Group],
[Location Code],
[Document Type],'
set @sql =@sql +'CASE WHEN GPPG="RETAIL" THEN SUM([Valued Quantity]) ELSE 0 END AS "Quantity",'
set @sql =@sql +'CASE WHEN GPPG="RETAIL" THEN SUM([Sales Amount (Actual)]) ELSE 0 END AS "Amount",'
set @sql =@sql +' SUM([Sales Tax Amount (Actual)]) AS "Excise Tax Amount",'
set @sql =@sql +' CASE WHEN GPPG="DISCOUNT" THEN SUM([Valued Quantity]) ELSE 0 END AS "Discount Quantity",'
set @sql =@sql +' CASE WHEN GPPG="DISCOUNT" THEN SUM([Discount Amount]) ELSE 0 END AS "Discount Amount",'
set @sql =@sql +'SUM([Valued Quantity in HL]) AS "Quantity HL"'
set @sql =@sql +'FROM #Temp2'
set @sql =@sql +'GROUP BY
[Item No_],
[Description],
[Item Ledger Entry Type],
[Location Code],
[Gen_ Prod_ Posting Group],
[Document Type], GPPG

DROP TABLE #Temp2'

exec sp_executesql @sql


Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'US$Value Entry'.
Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'BY'.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-05-02 : 09:18:12
If you change exec sp_executesql @sql to print @sql, you'll be able to see the errors

Gerald Britton
Toronto PASS Chapter
Go to Top of Page

dinhson_vn
Starting Member

15 Posts

Posted - 2015-05-02 : 09:41:20
Thanks gbritton

If I change exec sp_executesql @sql to print @sql not see error. But it's print all code :)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2015-05-02 : 14:35:36
quote:
Originally posted by dinhson_vn

it's print all code



SO hopefully you can see the error in the printed code?

If not, execute that code and you may well get a more meaningfull error message

My guess is that you are missing a space before GROUP BY ... but that should be obvious in the PRINTED code, but very hard to see in the original string manipulation code.
Go to Top of Page
   

- Advertisement -