SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Syntax help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eljapo4
Yak Posting Veteran

United Kingdom
94 Posts

Posted - 08/02/2012 :  09:08:45  Show Profile  Reply with Quote
hi, I've the following piece of SQL in a SP

SET @SQLStmt = 'SELECT TOP 100
M.AuthNumber, 
M.CallDate, 
M.ModelDescription, 						   
C.CustomerForename + '' '' + C.CustomerSurname As CustomerName, 
CASE WHEN C.CustomerStreet = '' THEN
C.CustomerVisitingStreet, C.CustomerVisitingPostcode + '' '' + C.CustomerVisitingTown
ELSE
C.CustomerStreet, C.CustomerPostcode + '' '' + C.CustomerTown 
END As CustomerPostcode,						   
CASE WHEN Status = 0 THEN ''Open'' ELSE ''Closed'' END As Status, 
ISNULL(E.Name,''-'') As Engineer, 
CASE WHEN RouteStatus = 0 THEN ''Not Assigned''
WHEN RouteStatus = 1 THEN ''Awaiting Part'' 
ELSE ''Assigned'' 
END As RouteStatus  
FROM dbo.tbl_Call_Record_Main M 
INNER JOIN dbo.tbl_Call_Record_Customers C ON C.AuthNumber = M.AuthNumber 
LEFT JOIN dbo.tbl_Engineers E ON M.EngineerID = E.EngineerID
WHERE ' + @SearchTerms + ' 
ORDER BY M.CallDate DESC '


I've added the section bold and now when I run my program I am getting an incorrect syntax message ( Incorrect syntax near ','.) I just want to check if C.CustomerStreet is blank.

Any help would be appreciated.

eljapo4
Yak Posting Veteran

United Kingdom
94 Posts

Posted - 08/02/2012 :  09:33:47  Show Profile  Reply with Quote
realised I was trying the 1 case expression for 2 different fields. Updated code:

SET @SQLStmt = 'SELECT TOP 100 
   M.AuthNumber, 
   M.CallDate, 
   M.ModelDescription, 						   
   C.CustomerForename + '' '' + C.CustomerSurname As CustomerName, 
			   
   CASE WHEN C.CustomerStreet = '' THEN C.CustomerVisitingStreet ELSE C.CustomerStreet END AS CustomerStreet, 
   CASE WHEN C.CustomerPostcode = '' THEN C.CustomerVisitingPostcode + '' '' + C.CustomerVisitingTown
   ELSE C.CustomerPostcode + '' '' + C.CustomerTown END As CustomerPostCode,						   
						   
   CASE WHEN Status = 0 THEN ''Open'' ELSE ''Closed'' END As Status, 
   ISNULL(E.Name,''-'') As Engineer, 
   CASE WHEN RouteStatus = 0 THEN ''Not Assigned''
   WHEN RouteStatus = 1 THEN ''Awaiting Part'' 
   ELSE ''Assigned'' 
   END As RouteStatus  
   FROM dbo.tbl_Call_Record_Main M 
   INNER JOIN dbo.tbl_Call_Record_Customers C ON C.AuthNumber = M.AuthNumber 
   LEFT JOIN dbo.tbl_Engineers E ON M.EngineerID = E.EngineerID
   WHERE ' + @SearchTerms + ' 
   ORDER BY M.CallDate DESC '

EXEC(@SQLStmt)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 08/02/2012 :  09:57:25  Show Profile  Reply with Quote
I hope serachterms will always have some value else above statement will fail

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000