Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Giving a strong name to Default Values

 
   Database Help (Home) -> Programming RSS
Next:  Calculate the procurement cost of Web reporting t..  
Author Message
sloan

External


Since: Jan 10, 2008
Posts: 210



(Msg. 1) Posted: Wed Apr 01, 2009 12:47 pm
Post subject: Giving a strong name to Default Values
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Can you strong-name default values.

I'm looking for the "inline" way, and the "after the fact way".

Inline = NumberOfFloors

After the Fact = NumberOfSides

Here is a working example.



Why? When I need to drop a column at a later date.......I need to drop the
default value first.

I'm trying to get away from the Microsoft random names so I can do the drop
easier.

RandomName Example : DF__Buildin__NumberOf__4FDCC2C0



--start TSQL

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[Building]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

BEGIN

DROP TABLE [dbo].[Building]

END

GO

CREATE TABLE [dbo].[Building] (

BuildingUUID [uniqueidentifier] not null default NEWSEQUENTIALID() ,

BuildingName varchar(64) not null ,


NumberOfFloors int not null DEFAULT 1 /* Can you name the default HERE */ ,

NumberOfSides int not null

)

GO



ALTER TABLE [dbo].[Building] ADD DEFAULT (4) FOR NumberOfSides --<<Can you
name this





GO



Insert Into dbo.Building ( BuildingName ) values ('Sears Tower')

select * from dbo.Building

 >> Stay informed about: Giving a strong name to Default Values 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 2) Posted: Wed Apr 01, 2009 1:11 pm
Post subject: Re: Giving a strong name to Default Values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Yes, and in many shops this is a best practice.


CREATE TABLE #foo
(
id INT,
a INT NOT NULL CONSTRAINT [default_for_a] DEFAULT 10,
b INT NOT NULL
);
GO
ALTER TABLE #foo ADD CONSTRAINT [default_for_b] DEFAULT(20) FOR b;
GO
INSERT #foo(id) SELECT 1;
GO
SELECT id,a,b FROM #foo;
GO
DROP TABLE #foo;
GO


On 4/1/09 12:47 PM, in article #60wAmusJHA.1240@TK2MSFTNGP02.phx.gbl,
"sloan" wrote:

>
>
> Can you strong-name default values.
>
> I'm looking for the "inline" way, and the "after the fact way".
>
> Inline = NumberOfFloors
>
> After the Fact = NumberOfSides
>
> Here is a working example.
>
>
>
> Why? When I need to drop a column at a later date.......I need to drop the
> default value first.
>
> I'm trying to get away from the Microsoft random names so I can do the drop
> easier.
>
> RandomName Example : DF__Buildin__NumberOf__4FDCC2C0
>
>
>
> --start TSQL
>
> if exists (select * from dbo.sysobjects where id =
> object_id(N'[dbo].[Building]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
>
> BEGIN
>
> DROP TABLE [dbo].[Building]
>
> END
>
> GO
>
> CREATE TABLE [dbo].[Building] (
>
> BuildingUUID [uniqueidentifier] not null default NEWSEQUENTIALID() ,
>
> BuildingName varchar(64) not null ,
>
>
> NumberOfFloors int not null DEFAULT 1 /* Can you name the default HERE */ ,
>
> NumberOfSides int not null
>
> )
>
> GO
>
>
>
> ALTER TABLE [dbo].[Building] ADD DEFAULT (4) FOR NumberOfSides --<<Can you
> name this
>
>
>
>
>
> GO
>
>
>
> Insert Into dbo.Building ( BuildingName ) values ('Sears Tower')
>
> select * from dbo.Building
>
>

 >> Stay informed about: Giving a strong name to Default Values 
Back to top
Login to vote
allmhuran

External


Since: Aug 01, 2008
Posts: 7



(Msg. 3) Posted: Wed Apr 01, 2009 1:11 pm
Post subject: Re: Giving a strong name to Default Values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Yeah, I'd definitely recommend this. Otherwise you can get into
situations where you generate a create script on one machine (eg
development) machine and then transfer it to another (eg production),
and end up with two different automagic names. Then later if you
generate a change script on your dev machine, for instance to drop or
change a default, and try to execute it against production... boom!
 >> Stay informed about: Giving a strong name to Default Values 
Back to top
Login to vote
sloan

External


Since: Jan 10, 2008
Posts: 210



(Msg. 4) Posted: Wed Apr 01, 2009 2:18 pm
Post subject: Re: Giving a strong name to Default Values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks.

I kept putting [default_for_a] in there, but forgot about the CONSTRAINT
keyword.



"Aaron Bertrand [SQL Server MVP]" wrote in message

> Yes, and in many shops this is a best practice.
>
>
> CREATE TABLE #foo
> (
> id INT,
> a INT NOT NULL CONSTRAINT [default_for_a] DEFAULT 10,
> b INT NOT NULL
> );
> GO
> ALTER TABLE #foo ADD CONSTRAINT [default_for_b] DEFAULT(20) FOR b;
> GO
> INSERT #foo(id) SELECT 1;
> GO
> SELECT id,a,b FROM #foo;
> GO
> DROP TABLE #foo;
> GO
>
>
> On 4/1/09 12:47 PM, in article #60wAmusJHA.1240@TK2MSFTNGP02.phx.gbl,
> "sloan" wrote:
>
>>
>>
>> Can you strong-name default values.
>>
>> I'm looking for the "inline" way, and the "after the fact way".
>>
>> Inline = NumberOfFloors
>>
>> After the Fact = NumberOfSides
>>
>> Here is a working example.
>>
>>
>>
>> Why? When I need to drop a column at a later date.......I need to drop
>> the
>> default value first.
>>
>> I'm trying to get away from the Microsoft random names so I can do the
>> drop
>> easier.
>>
>> RandomName Example : DF__Buildin__NumberOf__4FDCC2C0
>>
>>
>>
>> --start TSQL
>>
>> if exists (select * from dbo.sysobjects where id =
>> object_id(N'[dbo].[Building]') and OBJECTPROPERTY(id, N'IsUserTable') =
>> 1)
>>
>> BEGIN
>>
>> DROP TABLE [dbo].[Building]
>>
>> END
>>
>> GO
>>
>> CREATE TABLE [dbo].[Building] (
>>
>> BuildingUUID [uniqueidentifier] not null default NEWSEQUENTIALID() ,
>>
>> BuildingName varchar(64) not null ,
>>
>>
>> NumberOfFloors int not null DEFAULT 1 /* Can you name the default HERE */
>> ,
>>
>> NumberOfSides int not null
>>
>> )
>>
>> GO
>>
>>
>>
>> ALTER TABLE [dbo].[Building] ADD DEFAULT (4) FOR NumberOfSides --<<Can
>> you
>> name this
>>
>>
>>
>>
>>
>> GO
>>
>>
>>
>> Insert Into dbo.Building ( BuildingName ) values ('Sears Tower')
>>
>> select * from dbo.Building
>>
>>
>
 >> Stay informed about: Giving a strong name to Default Values 
Back to top
Login to vote
sloan

External


Since: Jan 10, 2008
Posts: 210



(Msg. 5) Posted: Wed Apr 01, 2009 2:35 pm
Post subject: Re: Giving a strong name to Default Values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Got it! Thanks. I threw in the NEWSEQUENTIALID Named Default as well.


Yes, I concur, its a best practice.


--start TSQL

if exists (select * from dbo.sysobjects where id =

object_id(N'[dbo].[Building]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

BEGIN

DROP TABLE [dbo].[Building]

END

GO

CREATE TABLE [dbo].[Building] (

BuildingUUID [uniqueidentifier] not null CONSTRAINT [DEFAULT_BuildingUUID]
DEFAULT NEWSEQUENTIALID() ,

BuildingName varchar(64) not null ,



NumberOfFloors int not null CONSTRAINT [DEFAULT_FLOORS] DEFAULT 1 ,

NumberOfSides int not null

)

GO





ALTER TABLE [dbo].[Building] ADD CONSTRAINT [DEFAULT_SIDES] DEFAULT(4) FOR
NumberOfSides









GO





Insert Into dbo.Building ( BuildingName ) values ('Sears Tower')

select * from dbo.Building
 >> Stay informed about: Giving a strong name to Default Values 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 6) Posted: Thu Apr 02, 2009 2:26 am
Post subject: Re: Giving a strong name to Default Values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I agree with Aaron that in most cases yuo should name CONSTAINTs
Also please read my article for another hit
http://dimantdatabasesolutions.blogspot.com/2008/12/when-constraint-re...ns-in-s




"sloan" wrote in message

>
>
> Can you strong-name default values.
>
> I'm looking for the "inline" way, and the "after the fact way".
>
> Inline = NumberOfFloors
>
> After the Fact = NumberOfSides
>
> Here is a working example.
>
>
>
> Why? When I need to drop a column at a later date.......I need to drop
> the default value first.
>
> I'm trying to get away from the Microsoft random names so I can do the
> drop easier.
>
> RandomName Example : DF__Buildin__NumberOf__4FDCC2C0
>
>
>
> --start TSQL
>
> if exists (select * from dbo.sysobjects where id =
> object_id(N'[dbo].[Building]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
>
> BEGIN
>
> DROP TABLE [dbo].[Building]
>
> END
>
> GO
>
> CREATE TABLE [dbo].[Building] (
>
> BuildingUUID [uniqueidentifier] not null default NEWSEQUENTIALID() ,
>
> BuildingName varchar(64) not null ,
>
>
> NumberOfFloors int not null DEFAULT 1 /* Can you name the default HERE */
> ,
>
> NumberOfSides int not null
>
> )
>
> GO
>
>
>
> ALTER TABLE [dbo].[Building] ADD DEFAULT (4) FOR NumberOfSides --<<Can you
> name this
>
>
>
>
>
> GO
>
>
>
> Insert Into dbo.Building ( BuildingName ) values ('Sears Tower')
>
> select * from dbo.Building
>
>
 >> Stay informed about: Giving a strong name to Default Values 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Column default values - Hello everyone, I have the following problem I am not sure how to solve: I am using sql 2005. I have a database that has a product table (already with around 2000 products). The product table has a price column. I need to add another column called..

IDENTITY as Default values - I need some feedback on a tricky subject: Given: CREATE TABLE [TEST] ( [id] [bigint] IDENTITY (1, 1) NOT NULL , [id2] [bigint] NULL CONSTRAINT [DF__TEST__id2__7C104AB9] DEFAULT (dbo.IDENT('test')) [field] varchar NULL ) ON [PRIMARY] ....

Default constraints and values - Hi, Does anyone know why I am able to create a table such as follows? create table test (col1 nvarchar(1) default 'AA', col2 nvarchar(1) null) go Notice that I have a default value of 'AA' for a column that is defined as an nvarchar(1). SQL Server....

create table, default values - Hello, I'm trying recreate tables using the script that can be generated using enterprise manager. However the default values of columns are not generated. Can anyone advise how this can be remedied? Thanks Soc.

Obtaining default values after an INSERT - I am inserting a row into a database, and some columns insert default values (such as an @@identity column and a (getdate()) column). Though I can get the identity through @@identity, is there a way to get the other default values without having to..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]