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

Obtaining default values after an INSERT

 
   Database Help (Home) -> Programming RSS
Next:  New To SQL-Help With Views  
Author Message
Agendum

External


Since: May 27, 2008
Posts: 1



(Msg. 1) Posted: Tue May 27, 2008 2:11 pm
Post subject: Obtaining default values after an INSERT
Archived from groups: microsoft>public>sqlserver>programming (more info?)

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 query the table again using the @@identity? The
only two options I see are:

1) Manually create the default value in the stored procedure and pass the
value into the INSERT. This gives me the value after the INSERT.

2) Call INSERT and do a SELECT to query for the default value.

Is there any other way to get the default value without manually create the
value in the stored procedure, or having to SELECT for it?

Thanks

 >> Stay informed about: Obtaining default values after an INSERT 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 2) Posted: Tue May 27, 2008 2:53 pm
Post subject: Re: Obtaining default values after an INSERT [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I am inserting a row into a database, and some columns insert DEFAULT
values (such as an @@IDENTITY column [sic: it is not a column, but a
property; don't use it at all] and a GETDATE() [use CURRENT_TIMESTAMP
now that we have it] column). Though I can get the IDENTITY through
@@IDENTITY, is there a way to get the other DEFAULT values without
having to query the table again using the @@IDENTITY? <<

>> Is there any other way to get the DEFAULT value without manually create the value in the stored procedure, or having to SELECT for it? <<

Unh? Look at the DDL and find out what the DEFAULT is for each
column. You don't create them in a procedure; they are part of the
table declarations. Can you explain what you are talking about?

Instead of writing GETDATE(), use the Standard CURRENT_TIMESTAMP now
that we have it. And you would never use IDENTITY at all in a
properly designed schema.

You might also want to look up the "INSERT INTO <table name> DEFAULT
VALUES;"

 >> Stay informed about: Obtaining default values after an INSERT 
Back to top
Login to vote
Eric Isaacs

External


Since: May 13, 2008
Posts: 367



(Msg. 3) Posted: Tue May 27, 2008 4:50 pm
Post subject: Re: Obtaining default values after an INSERT [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

There are 3 ways to get the default values when you insert...

1) Put them in when you insert, so you know them, (if you can know
them.)
2) Use Scope_Identity to determine the key and query for the values
after the insert.
3) Look the default values up in the system tables, assuming they are
fixed values (not CURRENT_TIMESTAMP or GETDATE())

#1 would be the most preferred if possible. #2 would be acceptable.
#3 is more difficult because if the default value is a calculated
default value such as GETDATE() or something of the sort, you won't
know the exact value without quering the actual row in the table
anyway.

#1 sometimes works.
#2 always works.
#3 sometimes works.
 >> Stay informed about: Obtaining default values after an INSERT 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 4) Posted: Tue May 27, 2008 6:24 pm
Post subject: Re: Obtaining default values after an INSERT [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Is there any other way to get the default value without manually create
> the
> value in the stored procedure, or having to SELECT for it?

What is wrong with querying for it? Theoretically, you put data into a
table for exactly that reason, don't you?
 >> Stay informed about: Obtaining default values after an INSERT 
Back to top
Login to vote
David Portas

External


Since: Nov 11, 2003
Posts: 854



(Msg. 5) Posted: Tue May 27, 2008 10:28 pm
Post subject: Re: Obtaining default values after an INSERT [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Agendum" wrote in message

>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 query the table again using the @@identity? The
> only two options I see are:
>
> 1) Manually create the default value in the stored procedure and pass the
> value into the INSERT. This gives me the value after the INSERT.
>
> 2) Call INSERT and do a SELECT to query for the default value.
>
> Is there any other way to get the default value without manually create
> the
> value in the stored procedure, or having to SELECT for it?
>
> Thanks
>

Don't use @@IDENTITY. SCOPE_IDENTITY() is the best way to return the
IDENTITY value in most cases.

You can use the OUTPUT clause on your INSERT statement to return other
column values (SQL Server 2005 and 2008 only).

--
David Portas
 >> Stay informed about: Obtaining default values after an INSERT 
Back to top
Login to vote
Tony Rogerson

External


Since: Jan 10, 2008
Posts: 213



(Msg. 6) Posted: Wed May 28, 2008 5:27 am
Post subject: Re: Obtaining default values after an INSERT [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> that we have it. And you would never use IDENTITY at all in a
> properly designed schema.
>

Yes you would - for a surrogate key; it's only you that chooses to ignore
that fact.

Thankfully most designers have the natural key on the single base table and
use surrogate keys to the foriegn tables and in the application plumbing -
that follows Codd's surrogate rules despite how you personally read it.

--
Tony Rogerson, SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson
[Ramblings from the field from a SQL consultant]
http://sqlserverfaq.com
[UK SQL User Community]
 >> Stay informed about: Obtaining default values after an INSERT 
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.

Which systemtable or view hold the default values - CREATE TABLE [dbo].[ASSESSMENT]( [est_id] [int] NOT NULL DEFAULT ((0)), [overall_assessmnt] [float] NULL, [bldg_assessmnt] [float] NULL, [land_assessmnt] [float] NULL, [value_rounding] [int] NULL, ) this value DEFAULT ((0))adv thanks
   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 ]