Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

default values not working for SP? 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,
I have the following SP
Code:
PROCEDURE [dbo].[spADM_RiskData] 

	@MemNo varchar(12) = '%', 
	@StartDate char(10) = NULL,
	@EndDate char(10) = NULL
	
AS
BEGIN

	SET NOCOUNT ON;
	
	-- Set default dates
	Set @StartDate = coalesce(@StartDate, '2008-01-01') 
	Set @EndDate = coalesce(@EndDate, Convert(char(10), GetDate(), 121))

	SELECT Case_ID,Adv_MemNo,Reason
	FROM Case_Checking INNER JOIN Business_Register
	ON Case_Checking.Case_ID = Business_Register.Rec_ID
	WHERE (Flag_Date BETWEEN @StartDate AND @EndDate) AND Adv_MemNo LIKE @MemNo + '%'
	
END

I am calling it with the following...

Code:
spADM_RiskData '','',''

Which should trigger the default values (shouldn't it?)

But I seem to be getting zero records returned instead of all of them?

If I use

Code:
spADM_RiskData '','2008-01-01','2013-03-06'

I get the expected returned records.

So what's wrong with the empty strings for the date parameters and why isn't it triggering the default values?

Thanks,

1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
if you call the proc with;
Code:
spADM_RiskData '','',''

You are not passing NULL values to the second and third parameters but empty string values which are not the same. Try;
Code:
spADM_RiskData ''


Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
NB: by '...passing NULL values to the second and third parameters...' I mean that you are in fact passing values when you need to pass in no value at all to utilise the defined defaults. As an aside, any reason you're using COALESCE instead of ISNULL?...

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
but how do you then pass in an end date but no start date?

I've tried
Code:
spADM_RiskData '',,'2009-01-01'

But that just errors with incorrect syntax?

As an aside, any reason you're using COALESCE instead of ISNULL?...
Yes, from my other thread
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Easiest way is to name the parameters when you call it. See here:



----------------------------------------

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch

Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
 
Or if people want the MS version:



----------------------------------------

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch

Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
 
Thanks SQLScholar,

Never realised you could pass in "@varname = 'value'", that makes it a whole lot easier, I always thought you had to comma separate and they had to be in the order you delcared them in the SP!

I resolved the issue in my Catalyst perl app by using
Code:
    # Build SP string
    my $sp = "spADM_RiskData '$memno',";
    $sp .= (defined $start_date)? "'$start_date'," : "'2008-01-01'";
    $sp .= (defined $end_date)? ",'$end_date'" : '';

But I think I will refactor to correcly use name=value!

Regards,
1DMF



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
I refactored to this
Code:
    # Build SP string
    my $sp = '@MemNo = ' . "'$memno'";
    $sp .= (defined $start_date)? ',@StartDate = ' . "'$start_date'" : '';
    $sp .= (defined $end_date)? ',@EndDate = ' . "'$end_date'" : '';

It's working grand!

Much obliged.

1DMF.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
You could also use the keyword [bold]DEFAULT[/bold] as placeholders

Code:
spADM_RiskData DEFAULT, DEFAULT,'2009-01-01'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top