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

IDENTITY as Default values

 
   Database Help (Home) -> Programming RSS
Next:  creating a script that can create OR alter a spro..  
Author Message
DM Unseen

External


Since: Mar 27, 2006
Posts: 1



(Msg. 1) Posted: Thu Nov 09, 2006 4:51 am
Post subject: IDENTITY as Default values
Archived from groups: microsoft>public>sqlserver>programming (more info?)

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]

Given the following function

CREATE FUNCTION dbo.IDENT(@table sysname)
RETURNS bigint
AS
BEGIN
RETURN(IDENT_CURRENT(@table))
END

The idea is when id2 is not overwritten, it will be identical to id.

The table is actually a denormalized parent child relationship, with id
the internal key of the child, and id2 the key of the parent. You can
use this to have versioning of records within the table.

In this table id2 is not unique within the table, but a new parent
record id needs to be created sometimes. When the parent id2 exists, a
copy action within the table of records suffices.

Note that this is not my idea of a good design. I just need to fix an
app without going into the application code, and without creating a
trigger or proc, so my manouvering is extremely limited.

I checked this setup, even on a interleaved transaction scenario with 2
updating connections, and it seems to work, but I cannot prove to
myself this is really foolproof. Note that you need the function, using
IDENT_CURRENT directly in the default constraint does not work!

Dm Unseen

 >> Stay informed about: IDENTITY as Default values 
Back to top
Login to vote
Dan Guzman1

External


Since: Aug 22, 2004
Posts: 840



(Msg. 2) Posted: Thu Nov 09, 2006 7:14 am
Post subject: Re: IDENTITY as Default values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Note that this is not my idea of a good design. I just need to fix an
> app without going into the application code, and without creating a
> trigger or proc, so my manouvering is extremely limited.

Why not a trigger? That should be transparent to application code.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"DM Unseen" wrote in message

>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]
>
> Given the following function
>
> CREATE FUNCTION dbo.IDENT(@table sysname)
> RETURNS bigint
> AS
> BEGIN
> RETURN(IDENT_CURRENT(@table))
> END
>
> The idea is when id2 is not overwritten, it will be identical to id.
>
> The table is actually a denormalized parent child relationship, with id
> the internal key of the child, and id2 the key of the parent. You can
> use this to have versioning of records within the table.
>
> In this table id2 is not unique within the table, but a new parent
> record id needs to be created sometimes. When the parent id2 exists, a
> copy action within the table of records suffices.
>
> Note that this is not my idea of a good design. I just need to fix an
> app without going into the application code, and without creating a
> trigger or proc, so my manouvering is extremely limited.
>
> I checked this setup, even on a interleaved transaction scenario with 2
> updating connections, and it seems to work, but I cannot prove to
> myself this is really foolproof. Note that you need the function, using
> IDENT_CURRENT directly in the default constraint does not work!
>
> Dm Unseen
>

 >> Stay informed about: IDENTITY as Default values 
Back to top
Login to vote
Mubashir Khan

External


Since: Mar 12, 2007
Posts: 15



(Msg. 3) Posted: Thu Nov 09, 2006 7:15 am
Post subject: Re: IDENTITY as Default values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

i suggest you use calculated field. coalese(id2,id1)
"DM Unseen" wrote in message

>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]
>
> Given the following function
>
> CREATE FUNCTION dbo.IDENT(@table sysname)
> RETURNS bigint
> AS
> BEGIN
> RETURN(IDENT_CURRENT(@table))
> END
>
> The idea is when id2 is not overwritten, it will be identical to id.
>
> The table is actually a denormalized parent child relationship, with id
> the internal key of the child, and id2 the key of the parent. You can
> use this to have versioning of records within the table.
>
> In this table id2 is not unique within the table, but a new parent
> record id needs to be created sometimes. When the parent id2 exists, a
> copy action within the table of records suffices.
>
> Note that this is not my idea of a good design. I just need to fix an
> app without going into the application code, and without creating a
> trigger or proc, so my manouvering is extremely limited.
>
> I checked this setup, even on a interleaved transaction scenario with 2
> updating connections, and it seems to work, but I cannot prove to
> myself this is really foolproof. Note that you need the function, using
> IDENT_CURRENT directly in the default constraint does not work!
>
> Dm Unseen
>
 >> Stay informed about: IDENTITY as Default values 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Identity/Seed Values - I have a question regarding using the Identity/Seed values. I have migrated a number of tables form Access to SQL. In Access the identity/seed values were called autonumbers. The big problem with autonumbers in Access was that they could change at any...

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..

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)
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 ]