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)
 Passing nulls to stored proc, but is placing zeros

Author  Topic 

reddymade
Posting Yak Master

165 Posts

Posted - 2008-07-25 : 14:49:37
Even though i pass null value for the first two parameters, but in the query(Print) under where condition it is showing b.progid=0 and c.projid=0
Can you please correct my where condition, if i pass a "" value or 0 it should skip conditions: b.progid and c.projid

Select a.TaskID, a.TaskName, a.TaskCode, a.ProgID,  a.ProjID, a.ContractID, a.Deleted, a.UpdatedBy, rtrim(b.progno) AS progno,  rtrim(c.projno) AS projno,rtrim(d.contractno) AS contractno  from Tab_ccsnetTasks as a Join Tab_ccsNetPrograms as b  ON a.progid = b.progid  JOIN TAB_ccsNetProjects AS c ON a.ProjID = c.ProjID  JOIN TAB_ccsNetContracts AS d ON a.ContractID = d.ContractID   Where 1=1  and b.progid = 0 and c.ProjID = 0 and d.ContractID = 17   




ALTER PROCEDURE [dbo].[USP_GetTasks1]
(
@ProgID int = null,
@ProjID int = null,
@ContractID int = null

)

AS

declare @sqlwhere varchar(1000)
declare @SQL varchar(4000)

select @SqlWhere = 'Where 1=1 '

if @ProgID is not null

select @SqlWhere = @SqlWhere + ' and b.progid = ' + cast(@ProgID as nvarchar)



if @ProjID is not null

select @SqlWhere = @SqlWhere + ' and c.ProjID = ' + cast(@ProjID as nvarchar)


if @ContractID is not null

select @SqlWhere = @SqlWhere + ' and d.ContractID = ' + cast(@ContractID as nvarchar)


Select @SQL = 'Select a.TaskID, a.TaskName, a.TaskCode, a.ProgID,'

Select @SQL = @SQL + ' a.ProjID, a.ContractID, a.Deleted, a.UpdatedBy, rtrim(b.progno) AS progno,'

Select @SQL = @SQL + ' rtrim(c.projno) AS projno,rtrim(d.contractno) AS contractno'

Select @SQL = @SQL + ' from Tab_ccsnetTasks as a Join Tab_ccsNetPrograms as b ON a.progid = b.progid'

Select @SQL = @SQL + ' JOIN TAB_ccsNetProjects AS c ON a.ProjID = c.ProjID'

Select @SQL = @SQL + ' JOIN TAB_ccsNetContracts AS d ON a.ContractID = d.ContractID'

select @SQl = @SQL + ' ' + @SqlWhere + ' '

EXEC (@SQL)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-25 : 14:55:33
you havent specified length while casting to nvarchar
Go to Top of Page

reddymade
Posting Yak Master

165 Posts

Posted - 2008-07-25 : 14:56:06
I changed to this: if isnull(@ProgID,'0') <> 0

Now it is working.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-25 : 14:59:32
quote:
Originally posted by reddymade

I changed to this: if isnull(@ProgID,'0') <> 0

Now it is working.


ok. but please specify a length for casted value also ,like:-

select @SqlWhere = @SqlWhere + ' and b.progid = ' +  cast(@ProgID   as nvarchar(length))
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-07-25 : 16:21:57
quote:
Originally posted by reddymade

I changed to this: if isnull(@ProgID,'0') <> 0

Now it is working.

Not a good solution. It means you could never intentionally pass a zero value to your sproc.
I have seen this issue many times before, and on each occasion my developers THOUGHT they were passing a null value, when in reality their code was passing a zero.
Try testing this by executing the sproc through the query tool.
What language are you using for application development.

e4 d5 xd5 Nf6
Go to Top of Page
   

- Advertisement -