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 2008 Forums
 Transact-SQL (2008)
 Help with if statement in stored procedure

Author  Topic 

samwood
Starting Member

2 Posts

Posted - 2013-07-01 : 05:45:25
Hi im trying to do an if statement in SP but i cannot seem to get it working. if the orderdate is null then it needs to run the first one
else the other




USE [Melville]
GO
/****** Object: StoredProcedure [dbo].[SP_EnabledCategories] Script Date: 06/28/2013 14:17:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_EnabledCategories]
-- Add the parameters for the stored procedure here
@ShowId int = 0,
@showdate datetime = 0,
@orderdate datetime = null,



if (@orderdate = null)
BEGIN
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
select @showdate = builddate from show where showid=@showid
SELECT TOP 9999999 [CategoryId]
,[ParentCategoryId]
,[Description]
,[DisplayOrder]
,[FilePath]
,[FileName]
,[IsDeletedOnOnlineOrdering]
,[IsDeletedOnClick]
,[StandInfoRequired]
,[DaysBeforeBuildDateToDisable]
,[CanHaveSeperateDeliveryAddress]
,datediff(d,getdate(), case when @showdate)
FROM [Melville].[dbo].[Category]
where
((CategoryId not in (select CategoryId from ShowDisabledCategory where ShowId=@Showid))
)
and (datediff(d,getdate(),@showdate) >DaysBeforeBuildDateToDisable)

order by parentcategoryid, DisplayOrder

END
ELSE
begin
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
select @orderdate = CustomDate from show where showid=@showid
SELECT TOP 9999999 [CategoryId]
,[ParentCategoryId]
,[Description]
,[DisplayOrder]
,[FilePath]
,[FileName]
,[IsDeletedOnOnlineOrdering]
,[IsDeletedOnClick]
,[StandInfoRequired]
,[DaysBeforeBuildDateToDisable]
,[CanHaveSeperateDeliveryAddress]
,datediff(d,getdate(), case when @orderdate)
FROM [Melville].[dbo].[Category]
where
((CategoryId not in (select CategoryId from ShowDisabledCategory where ShowId=@Showid))
)
and (datediff(d,getdate(),@orderdate) >DaysBeforeBuildDateToDisable)

order by parentcategoryid, DisplayOrder

END

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-01 : 06:03:39
your issue was using = with NULL. You cant use comparison operators like =,<> etc with NULL under default conditions as NULL is not stored as a value but it simply represents an unknown value. So you should be using IS NULL and IS NOT NULL for comparisons

however, for the above case you dont need the IF condition at all. just use like


USE [Melville]
GO
/****** Object: StoredProcedure [dbo].[SP_EnabledCategories] Script Date: 06/28/2013 14:17:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_EnabledCategories]
-- Add the parameters for the stored procedure here
@ShowId int = 0,
@showdate datetime = 0,
@orderdate datetime = null
AS
select @showdate = case when @orderdate is null then builddate else CustomDate end from show where showid=@showid

SELECT TOP 9999999 [CategoryId]
,[ParentCategoryId]
,[Description]
,[DisplayOrder]
,[FilePath]
,[FileName]
,[IsDeletedOnOnlineOrdering]
,[IsDeletedOnClick]
,[StandInfoRequired]
,[DaysBeforeBuildDateToDisable]
,[CanHaveSeperateDeliveryAddress]
,datediff(d,getdate(), case when @showdate)
FROM [Melville].[dbo].[Category]
where
((CategoryId not in (select CategoryId from ShowDisabledCategory where ShowId=@Showid))
)
and (datediff(d,getdate(),@showdate) >DaysBeforeBuildDateToDisable)

order by parentcategoryid, DisplayOrder

GO


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

samwood
Starting Member

2 Posts

Posted - 2013-07-01 : 06:07:29
cheers :) spot on that worked thanks for explaining it
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-01 : 06:08:04
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -