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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [Resolved] Problems modifying stored procedure

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-05-29 : 13:17:55
I have a sp that I need to modify and having some syntax issues:
Here is my script:

SELECT	  			dbo.Batch.ReportDate AS job_date, dbo.Job.CompanyJobId AS job_number, dbo.Item.CompanyItemId AS cost_code, 

SUM(CASE dbo.SourceType.CompanySourceTypeId WHEN 'MA' then dbo.ProductionEvent.AlternateQuantity ELSE 0 END) AS qty_received,
SUM(CASE dbo.SourceType.CompanySourceTypeId WHEN 'PR' THEN dbo.ProductionEvent.Quantity ELSE 0 END) AS qty_used,
SUM(CASE dbo.SourceType.CompanySourceTypeId WHEN 'MA' then dbo.ProductionEvent.AlternateQuantity ELSE 0 END)
- SUM(CASE dbo.SourceType.CompanySourceTypeId WHEN 'PR' THEN dbo.ProductionEvent.Quantity ELSE 0 END) AS qty_wasted,
SUBSTRING(dbo.Job.CompanyJobId, 1 ,3) AS plant_id

FROM dbo.Batch INNER JOIN
dbo.Event ON dbo.Batch.BatchGuid = dbo.Event.BatchGuid INNER JOIN
dbo.Job ON dbo.Event.JobGuid = dbo.Job.JobGuid INNER JOIN
dbo.ProductionEvent ON dbo.Event.EventGuid = dbo.ProductionEvent.EventGuid INNER JOIN
dbo.Item ON dbo.Event.ItemGuid = dbo.Item.ItemGuid INNER JOIN
dbo.Source ON dbo.ProductionEvent.SourceGuid = dbo.Source.SourceGuid INNER JOIN
dbo.SourceType ON dbo.Source.SourceTypeGuid = dbo.SourceType.SourceTypeGuid left outer JOIN
dbo.Product ON dbo.ProductionEvent.ProductGuid = dbo.Product.ProductGuid

WHERE dbo.Item.UnitOfMeasure = 'TN' and dbo.Batch.ReportDate >= @DateFrom and dbo.Batch.ReportDate <= @DateTo

GROUP BY dbo.Batch.ReportDate, dbo.Job.CompanyJobId,dbo.Item.CompanyItemId



Now I need to modify this line:

SUM(CASE dbo.SourceType.CompanySourceTypeId WHEN 'MA' then dbo.ProductionEvent.AlternateQuantity ELSE 0 END) AS qty_received


to include:

and dbo.Product.CompanyProductId LIKE 'ASPH%' 


New line should be:

SUM(CASE dbo.SourceType.CompanySourceTypeId WHEN 'MA' and dbo.Product.CompanyProductId when LIKE 'ASPH%'then dbo.ProductionEvent.AlternateQuantity ELSE 0 END) AS qty_received,


I get error saying: Incorrect syntax near the keyword 'and'.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-29 : 13:20:42
You are missing the THEN part of the first WHEN.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-29 : 13:23:29
change like this and try:-
SUM(CASE WHEN dbo.SourceType.CompanySourceTypeId ='MA' and dbo.Product.CompanyProductId LIKE 'ASPH%' THEN dbo.ProductionEvent.AlternateQuantity ELSE 0 END) AS qty_received,
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-05-29 : 16:52:03
Worked perfectly..... Thank you guys.....
Go to Top of Page
   

- Advertisement -