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] Query - where statement

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2011-01-25 : 16:18:51
Have following:

myCommand.Parameters.Add(New Data.SqlClient.SqlParameter("@Job", Data.SqlDbType.Char, 20)).Value = TextBox3.Text



ALTER PROCEDURE [dbo].[sp_goLabor_get_batch_details] 

@Owner char(50),

select distinct(Event.BatchGuid), Batch.Name as BatchName, Batch.ReportDate as ReportDate, Job.CompanyJobId as JobNumber, EventStatusType.Name as ReportStatus
from [VGIWPSQL2].goLabor30.dbo.event
inner join [VGIWPSQL2].goLabor30.dbo.Batch on Batch.BatchGuid = Event.BatchGuid
inner join [VGIWPSQL2].goLabor30.dbo.Job on Job.JobGuid = Event.JobGuid
inner join [VGIWPSQL2].goLabor30.dbo.EventStatusType on EventStatusType.EventStatusTypeGuid = Event.EventStatus
inner join [VGIWPSQL2].goLabor30.dbo.Account on Account.AccountGuid = Event.AccountGuid
where (@Owner IS Null or Account.Username = @Owner) and
(@DateFrom IS Null OR Batch.Reportdate >= @DateFrom)and
(@DateTo IS Null OR Batch.ReportDate <= @DateTo) and
(@Job IS Null or Job.CompanyJobId = @Job) and
(EventStatusType.Name = @Status)
order by Batch.Name


If TextBox3 is not filled in then my sql query does not work on @Job. If I fill in a job number in Textbox3 it works fine.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-01-25 : 16:37:13
ALTER PROCEDURE ...
...
AS

SET NOCOUNT ON

SET @Job = NULLIF(@Job, '')

...
...

An empty string is not the same as NULL, so convert empty string to NULL before using the variable in the query.


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2011-01-26 : 14:43:35
Thank you, worked great !!
Go to Top of Page
   

- Advertisement -