August 14, 2007 at 6:21 am
I'm trying to create a sproc with the following t-sql:
SET
@sql =
'SELECT dbo.Programs.Program
, LEFT(loc.Work_Location, 1) + '
0' AS DivNum
, CASE WHEN LEFT(loc.Work_Location, 1)+ '
0' = 80 THEN 'Buildings' ELSE d.[Division Name] END AS Division
, JM.Job_Number AS Job
, Cust.Customer_Name AS Customer
, Emp.Employee_First_and_Last_Name AS BDRep
, SUM(BudDet.Budgeted_Cost_Amount) AS [Budgeted GPS]
, dbo.Calendar.FP AS FinPer
, dbo.Calendar.FY AS FinYear
, '
New' AS GPSType
and I get the following error
Msg 102, Level 15, State 1, Procedure srptGrossProfitSalesByLibrary, Line 22
Incorrect syntax near '0'.
What am I doing wrong?
TIA
Dean
August 14, 2007 at 7:35 am
August 14, 2007 at 7:38 am
August 14, 2007 at 7:57 am
thank you
August 15, 2007 at 4:26 am
If you do not want SQL to spend time doing a data type conversion for every row in the query, you should make the 80 into a character string ...= ''80'' ...
Original author: https://github.com/SQL-FineBuild/Common/wiki/ 1-click install and best practice configuration of SQL Server 2019, 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005.
When I give food to the poor they call me a saint. When I ask why they are poor they call me a communist - Archbishop Hélder Câmara
August 15, 2007 at 9:55 pm
Hi chwck this code . I think this help you
declare @sql varchar(600)
SET @sql = 'SELECT dbo.Programs.Program,JM.Job_Number AS Job,Cust.Customer_Name AS Customer,Emp.Employee_First_and_Last_Name AS BDRep,
SUM(BudDet.Budgeted_Cost_Amount) AS [Budgeted GPS], dbo.Calendar.FP AS FinPer, dbo.Calendar.FY AS FinYear ,New AS GPSType,
LEFT(loc.Work_Location, 1) + 0 AS DivNum ,CASE WHEN LEFT(loc.Work_Location, 1)+ 0 = 80 THEN Buildings ELSE d.[Division Name] END AS Division'
print @sql
August 15, 2007 at 10:09 pm
There is nothing in the code that needs dynamic SQL. Rewrite the code to take variables. Normally, the only time you need dynamic SQL is for variable table or column names and I'm not seeing any of that in this code.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply