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 2000 Forums
 SQL Server Development (2000)
 Trying to understand the default keyword

Author  Topic 

Munchausen
Starting Member

25 Posts

Posted - 2005-07-25 : 12:21:49
I'm working on a database and I'm not too familiar with using the default keyword.

Here's a partial example of a stored procedure that resides in the database.


CREATE PROCEDURE UpdateRecord

@MessageID varchar(254) = DEFAULT,
@FilerCode varchar(3) = DEFAULT,
@EntryNo varchar(9) = DEFAULT

AS

UPDATE table SET
MessageID = @MessageID,
EntryNo = @EntryNo,
FilerCode = @FilerCode

WHERE FilerCode = @FilerCode and EntryNo = @EntryNo
GO


As it was explained to me, the parameters are set to DEFAULT so that if the update tries to update a field with NULL instead of data, it will instead preserve the original data and not update the field.

Is it as simple as that? Or is DEFAULT defined somehow in the database?

Thanks in advance!

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-07-25 : 14:11:22
You are completely misunderstanding what DEFAULT is for.

From SQL Server Books Online:
"Is a default value for the parameter. If a default is defined, the procedure can be executed without specifying a value for that parameter. The default must be a constant or it can be NULL..."

quote:
Originally posted by Munchausen

I'm working on a database and I'm not too familiar with using the default keyword.

Here's a partial example of a stored procedure that resides in the database.


CREATE PROCEDURE UpdateRecord

@MessageID varchar(254) = DEFAULT,
@FilerCode varchar(3) = DEFAULT,
@EntryNo varchar(9) = DEFAULT

AS

UPDATE table SET
MessageID = @MessageID,
EntryNo = @EntryNo,
FilerCode = @FilerCode

WHERE FilerCode = @FilerCode and EntryNo = @EntryNo
GO


As it was explained to me, the parameters are set to DEFAULT so that if the update tries to update a field with NULL instead of data, it will instead preserve the original data and not update the field.

Is it as simple as that? Or is DEFAULT defined somehow in the database?

Thanks in advance!



CODO ERGO SUM
Go to Top of Page

Munchausen
Starting Member

25 Posts

Posted - 2005-07-25 : 14:27:50
I kind of thought I might be.

So in the case of the example I provided, all of the parameters would be set to whatever the default value for the column is, as established during table creation?
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-07-25 : 15:07:59
No. They would be set to the values you passed to your stored procedure.

quote:
Originally posted by Munchausen

I kind of thought I might be.

So in the case of the example I provided, all of the parameters would be set to whatever the default value for the column is, as established during table creation?



CODO ERGO SUM
Go to Top of Page

Munchausen
Starting Member

25 Posts

Posted - 2005-07-25 : 15:34:31
Hmmm... I'm just not getting it, yet. I guess the question I have to ask is:

How does


@MessageID varchar(254) = DEFAULT,
@FilerCode varchar(3) = DEFAULT,
@EntryNo varchar(9) = DEFAULT


differ from


@MessageID varchar(254),
@FilerCode varchar(3),
@EntryNo varchar(9)


I'm sorry if this seems like a stupid question. I'm sure once I understand it I'll slap my forehead.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-07-25 : 16:35:26
When you execute a stored procedure, you must supply a value for all parameters, unless you have defined a parameter with a default value. If you specify a default value, and do not supply that parameter in your execute statement, the parameter gets the default value you specify.

Default values defined for stored procedures have nothing to do with default values for table columns.

You should read about the CREATE PROCEDURE statement in SQL Server Book Online.

quote:
Originally posted by Munchausen

Hmmm... I'm just not getting it, yet. I guess the question I have to ask is:

How does


@MessageID varchar(254) = DEFAULT,
@FilerCode varchar(3) = DEFAULT,
@EntryNo varchar(9) = DEFAULT


differ from


@MessageID varchar(254),
@FilerCode varchar(3),
@EntryNo varchar(9)


I'm sorry if this seems like a stupid question. I'm sure once I understand it I'll slap my forehead.



CODO ERGO SUM
Go to Top of Page

Munchausen
Starting Member

25 Posts

Posted - 2005-07-25 : 17:52:40
Thanks for the info. I think I've got it now.
Go to Top of Page
   

- Advertisement -