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] Select - syntax problem

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-07-23 : 10:55:20
I have problems in the "where" clause where I need to select on @DateFrom and @DateTo (if filled in as parm).

CREATE PROCEDURE sp_EquipmentDetailInquiry  

@JobNumber int,
@ItemNumber char(20),
@DateFrom datetime = Null,
@DateTo datetime = Null

as


SELECT dbo.Batch.ReportDate as date,
dbo.Item.CompanyItemId as item,
--dbo.EquipmentLaborEvent.RateType,
dbo.Equipment.CompanyEquipmentId as equipment,
dbo.Equipment.Name as name,
dbo.EquipmentLaborEvent.Hours as hours

FROM dbo.EquipmentLaborEvent
inner join dbo.Event on dbo.Event.EventGuid = dbo.EquipmentLaborEvent.EventGuid
inner join dbo.Batch on dbo.Batch.BatchGuid = dbo.Event.BatchGuid
inner join dbo.Job on dbo.Job.JobGuid = dbo.Event.JobGuid
inner join dbo.Item on dbo.Item.JobGuid = dbo.Job.JobGuid
inner join dbo.Equipment on dbo.Equipment.EquipmentGuid = dbo.EquipmentLaborEvent.EquipmentGuid
WHERE dbo.Job.CompanyJobId = @JobNumber and dbo.Item.CompanyItemId = @ItemNumber and (if @DateFrom <> Null then dbo.Batch.Reportdate >= @DateFrom and dbo.Batch.ReportDate <= @DateTo)
ORDER BY dbo.Batch.Reportdate, dbo.Job.CompanyJobId, dbo.Item.CompanyItemId,dbo.Equipment.CompanyEquipmentId
GO

VGuyz
Posting Yak Master

121 Posts

Posted - 2008-07-23 : 10:58:28
Instead of IF condition in where clause use CASE
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-23 : 10:58:53
[code]CREATE PROCEDURE sp_EquipmentDetailInquiry

@JobNumber int,
@ItemNumber char(20),
@DateFrom datetime = Null,
@DateTo datetime = Null

as


SELECT dbo.Batch.ReportDate as date,
dbo.Item.CompanyItemId as item,
--dbo.EquipmentLaborEvent.RateType,
dbo.Equipment.CompanyEquipmentId as equipment,
dbo.Equipment.Name as name,
dbo.EquipmentLaborEvent.Hours as hours

FROM dbo.EquipmentLaborEvent
inner join dbo.Event on dbo.Event.EventGuid = dbo.EquipmentLaborEvent.EventGuid
inner join dbo.Batch on dbo.Batch.BatchGuid = dbo.Event.BatchGuid
inner join dbo.Job on dbo.Job.JobGuid = dbo.Event.JobGuid
inner join dbo.Item on dbo.Item.JobGuid = dbo.Job.JobGuid
inner join dbo.Equipment on dbo.Equipment.EquipmentGuid = dbo.EquipmentLaborEvent.EquipmentGuid
WHERE dbo.Job.CompanyJobId = @JobNumber
and dbo.Item.CompanyItemId = @ItemNumber
and ( @DateFrom IS Null OR dbo.Batch.Reportdate >= @DateFrom)
and (@DateTo IS Null OR dbo.Batch.ReportDate <= @DateTo
)
ORDER BY dbo.Batch.Reportdate, dbo.Job.CompanyJobId, dbo.Item.CompanyItemId,dbo.Equipment.CompanyEquipmentId
GO[/code]
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-23 : 11:29:59
Snufse, didn't we go through this scenario here?
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=106898

See posts dated 07/21/2008 : 18:36:07 and later.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-07-23 : 11:48:49
Visakh16, great, as always when you help out, it works super....
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-23 : 11:51:13
I can't see that there is a difference between this suggestion and the one I posted two days ago?


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -