SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 IF else in SP with null param
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shanmugaraj
Posting Yak Master

212 Posts

Posted - 04/04/2013 :  07:17:54  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
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

India
52309 Posts

Posted - 04/04/2013 :  07:25:53  Show Profile  Reply with Quote
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

212 Posts

Posted - 04/04/2013 :  09:13:31  Show Profile  Send shanmugaraj a Yahoo! Message  Reply with Quote
Thanks. It works

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

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 04/04/2013 :  14:27:47  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000