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
 General SQL Server Forums
 New to SQL Server Programming
 IF else in SP with null param

Author  Topic 

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-04-04 : 07:17:54
The IF @no_of_week = null is not working when i dont pass the parameter. kindly help

CREATE PROCEDURE [dbo].[usp_get_data]
(@start_date datetime, @end_date datetime = null , @no_of_week int = null )
AS
BEGIN


IF @no_of_week = null
BEGIN
set @no_of_week = (select datepart(week, @end_date) - datepart(week, @start_date ) + 1 ) --- if user input is with end date and doesnot have @no_of_week
END

SET @week_value=(SELECT TOP 1 CASE WHEN @no_of_week=1 then [1_week]
WHEN @no_of_week=2 then [2_weeks]
WHEN @no_of_week =3 then [3_weeks]
WHEN @no_of_week =4 then [4_weeks]
WHEN @no_of_week between 5 and 24 then [5_24_weeks]
WHEN @no_of_week between 25 and 52 then [25_52_weeks]
end
FROM pricing_matrix)



SELECT * FROM fact_sales
WHERE total_week = @week_value


End

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-04 : 07:25:53
NULL is not stored as a value so operators =,> etc will not work with NULL under default conditions (ANSI NULL settings)


so you should be using IS NULL,IS NOT NULL for comparison like


CREATE PROCEDURE [dbo].[usp_get_data]
(@start_date datetime, @end_date datetime = null , @no_of_week int = null )
AS
BEGIN


IF @no_of_week is null
BEGIN
set @no_of_week = (select datepart(week, @end_date) - datepart(week, @start_date ) + 1 ) --- if user input is with end date and doesnot have @no_of_week
END

SET @week_value=(SELECT TOP 1 CASE WHEN @no_of_week=1 then [1_week]
WHEN @no_of_week=2 then [2_weeks]
WHEN @no_of_week =3 then [3_weeks]
WHEN @no_of_week =4 then [4_weeks]
WHEN @no_of_week between 5 and 24 then [5_24_weeks]
WHEN @no_of_week between 25 and 52 then [25_52_weeks]
end
FROM pricing_matrix)



SELECT * FROM fact_sales
WHERE total_week = @week_value


End


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

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2013-04-04 : 09:13:31
Thanks. It works

THANKS
SHANMUGARAJ
nshanmugaraj@gmail.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-04 : 14:27:47
welcome

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

- Advertisement -