Friday, February 10, 2012
autonumbering?
First post to this forum.
My question: I'm sure I read somewhere that ideally you shouldn't use the auto-increment facility in a table's id field but should generate you're own when you insert. Does this make any sense to anyone or is it just some rubbish I read online somewhere.
Any thoughts appreciated.
zingit's rubbish, sorry to say
go ahead and use IDENTITY, lots of people do|||Hello
First post to this forum.
My question: I'm sure I read somewhere that ideally you shouldn't use the auto-increment facility in a table's id field but should generate you're own when you insert. Does this make any sense to anyone or is it just some rubbish I read online somewhere.
Any thoughts appreciated.
zing
The only problem you'll have is if you want to copy the data from the one server to another i.e moving from development to production or whatever, as your id's will be reset to match the current counter in the target table.|||Hello
First post to this forum.
My question: I'm sure I read somewhere that ideally you shouldn't use the auto-increment facility in a table's id field but should generate you're own when you insert. Does this make any sense to anyone or is it just some rubbish I read online somewhere.
Any thoughts appreciated.
zing
Read up in Books Online about IDENTITY values and GUIDs (globally unique identifiers). You will find that each has advantages and disadvantages in different situations. IDENTITY columns should not be generated outside the database layer, but GUIDs can be generated at any layer of the application and there are some occasions when this is advantageous.|||Another issue that is sometimes a problem is that IDENTITY values are guaranteed to be unique. That does not mean (in fact it nearly prohibits) them from being sequential. Any ROLLBACK or DELETE operation leaves gaps in the numbering. This isn't a problem for code that needs a unique, consistantly ascending or descending number sequence, but it is a serious problem for poorly written code that demands a contiguous sequence.
I'll also second Blindman's notiion of giving GUID values consideration. They tend to either expose or fix many of the problems I've seen caused by poor use of sequences.
-PatP
Autonumbering the Records from XML
Hi,
I have a table "Del_Table", which must contain a column with autoincrement ability. I have added one column named as "Auto" and put it as Primary key, Identity and Integer. I am grabbing the data from an XML file. I have tried with the following Stored Procedure, but it gives an error as "An explicit value for the identity column in Del_Table can only be specified when a column list is used and IDENTITY_INSERT is ON". I have tried to to turn on IDENTITY_INSERT but still it does not work.
Code Snippet
CREATE PROCEDURE insertForecast
(@.OrderDoc ntext)
AS
DECLARE
@.hDoc int
EXEC sp_xml_preparedocument
@.hDoc output,
@.OrderDoc
INSERT INTO [Del_Table]
SELECT *
from OPENXML (@.hDoc,'Data/Delivery_Forecast/Forecast',3)
with
(
[ID] numeric '@.mp:id',
Document_Number char(35) '@.Document_Number',
Com_Date char(35) '@.Com_Date',
Code char (35) '@.code',
Port char (35) '@.port',
EPort char (35) '@.eport',
Number char (35) '@.number',
Instruction_Code char (35) '@.inst_Code',
Delivery_Date char (35) '@.del_date',
Quantity char (35) '@.quantity',
Status_Indicator char (35) '@.status_indicator'
)
ORDER BY
Document_Number
EXEC sp_xml_removedocument @.hDoc
GO
Any suggestions?
Thanks...
Specify the columns in the INSERT statement like the error message is stating (and still use the IDENTITY_INSERT of course).
WesleyB
Visit my SQL Server weblog @. http://dis4ea.blogspot.com
autonumbering question - newbie
question. Most of my experience with DBs comes from Oracle, so that's
the reference from which I'm working.
I'm trying to use the identity property in MS SQL Server to autonumber
a unique identifier field, and I'm wondering if it's possible to
manually assign a value for this field in my insert statement, or if
using the identity property ties me to the sequence. I know in
Oracle, since it uses sequences, one can choose whether or not to use
the next number in the sequence to assign a value, or to do it
manually. Since the insert statement for SQL Server doesn't allow a
value (or a reference or any kind of placeholder) of any sort, I'm at
a loss as to how to manually assign a value. Is it possible?
If it's not possible, could somebody lead me in the direction of
another way to address the situation? My main concern is having to
import data from another DB x years from now and that I'll want to
keep the unique contraints and all referential integrity associated
with the importing database.
Any help anybody could give me would be greatly appreciated. Thanks!You can manually assign a value to an identity column by turning on
IDENTITY_INSERT and explicitly specifying a column list. For example:
CREATE TABLE MyTable
(
MyIdentityColumn int NOT NULL IDENTITY(1, 1),
MyData int NOT NULL
)
GO
SET IDENTITY_INSERT MyTable ON
GO
INSERT INTO MyTable (MyIdentityColumn, MyData) VALUES(1, 1)
INSERT INTO MyTable (MyIdentityColumn, MyData) VALUES(2, 1)
INSERT INTO MyTable (MyIdentityColumn, MyData) VALUES(3, 1)
GO
SET IDENTITY_INSERT MyTable OFF
GO
--
Hope this helps.
Dan Guzman
SQL Server MVP
--------
SQL FAQ links (courtesy Neil Pike):
http://www.ntfaq.com/Articles/Index...epartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
--------
"systems_newbie" <ccrupper@.hotmail.com> wrote in message
news:5939e7a7.0308201121.4aca153b@.posting.google.c om...
> I'm just getting into SQL server, so I apologize if this is a basic
> question. Most of my experience with DBs comes from Oracle, so that's
> the reference from which I'm working.
> I'm trying to use the identity property in MS SQL Server to autonumber
> a unique identifier field, and I'm wondering if it's possible to
> manually assign a value for this field in my insert statement, or if
> using the identity property ties me to the sequence. I know in
> Oracle, since it uses sequences, one can choose whether or not to use
> the next number in the sequence to assign a value, or to do it
> manually. Since the insert statement for SQL Server doesn't allow a
> value (or a reference or any kind of placeholder) of any sort, I'm at
> a loss as to how to manually assign a value. Is it possible?
> If it's not possible, could somebody lead me in the direction of
> another way to address the situation? My main concern is having to
> import data from another DB x years from now and that I'll want to
> keep the unique contraints and all referential integrity associated
> with the importing database.
> Any help anybody could give me would be greatly appreciated. Thanks!|||It worked perfectly! Thank you very much for the solution and for the
prompt reply.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||"Charles Crupper" <ccrupper@.hotmail.com> wrote in message
news:3f43df99$0$200$75868355@.news.frii.net...
> It worked perfectly! Thank you very much for the solution and for the
> prompt reply.
I'll just point out a possible "flaw" in Dan's suggestion.
If two clients try to update the row with the same ID, you'll have to handle
that.
And that could cause issues if a client tries to do an insert and use the
Identity column vs. a client not using it.
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!|||> I'll just point out a possible "flaw" in Dan's suggestion.
> If two clients try to update the row with the same ID, you'll have to
handle
> that.
> And that could cause issues if a client tries to do an insert and use
the
> Identity column vs. a client not using it.
Good point. I should have mentioned that IDENTITY_INSERT should be
used for infrequent data migration or import and not part of routine
processing.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Greg D. Moore (Strider)" <mooregr@.greenms.com> wrote in message
news:R5S0b.122271$wk4.47486@.twister.nyroc.rr.com.. .
> "Charles Crupper" <ccrupper@.hotmail.com> wrote in message
> news:3f43df99$0$200$75868355@.news.frii.net...
> > It worked perfectly! Thank you very much for the solution and for
the
> > prompt reply.
> I'll just point out a possible "flaw" in Dan's suggestion.
> If two clients try to update the row with the same ID, you'll have to
handle
> that.
> And that could cause issues if a client tries to do an insert and use
the
> Identity column vs. a client not using it.
>
> > *** Sent via Developersdex http://www.developersdex.com ***
> > Don't just participate in USENET...get rewarded for it!|||"Dan Guzman" <danguzman@.nospam-earthlink.net> wrote in message
news:y4U0b.3512$sV.364@.newsread1.news.atl.earthlin k.net...
> > I'll just point out a possible "flaw" in Dan's suggestion.
> > If two clients try to update the row with the same ID, you'll have to
> handle
> > that.
> > And that could cause issues if a client tries to do an insert and use
> the
> > Identity column vs. a client not using it.
> Good point. I should have mentioned that IDENTITY_INSERT should be
> used for infrequent data migration or import and not part of routine
> processing.
I'll admit I had to test something first and was pleasantly surprised by the
results.
I wasn't sure if the seed was updated if you did IDENTITY_INSERTs, but
apparently it is, which is nice.
(I still have nightmares about SQL 6.5 and how easily it would fubar it's
IDENTITY Seed.)
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP|||> (I still have nightmares about SQL 6.5 and how easily it would fubar
it's
> IDENTITY Seed.)
Yea, the whole identity assignment method was re-written for SQL 7 and
above. I have no qualms about using IDENTITY is later releases.
--
Hope this helps.
Dan Guzman
SQL Server MVP
--------
"Greg D. Moore (Strider)" <mooregr@.greenms.com> wrote in message
news:bXU0b.122319$wk4.13930@.twister.nyroc.rr.com.. .
> "Dan Guzman" <danguzman@.nospam-earthlink.net> wrote in message
> news:y4U0b.3512$sV.364@.newsread1.news.atl.earthlin k.net...
> > > I'll just point out a possible "flaw" in Dan's suggestion.
> > > > If two clients try to update the row with the same ID, you'll have
to
> > handle
> > > that.
> > > > And that could cause issues if a client tries to do an insert and
use
> > the
> > > Identity column vs. a client not using it.
> > Good point. I should have mentioned that IDENTITY_INSERT should be
> > used for infrequent data migration or import and not part of routine
> > processing.
> I'll admit I had to test something first and was pleasantly surprised
by the
> results.
> I wasn't sure if the seed was updated if you did IDENTITY_INSERTs, but
> apparently it is, which is nice.
> (I still have nightmares about SQL 6.5 and how easily it would fubar
it's
> IDENTITY Seed.)
>
> > --
> > Hope this helps.
> > Dan Guzman
> > SQL Server MVP
Autonumbering causing deadlocks.
I have come into a system that uses a secondary table to generate (for
want of a better word) Identities.
eg
create table myidents
( name sysname not null, ident int not null)
create procedure getnextident @.table sysname, @.ident int output
as
begin
if not exists (select top 1 1 from myidents where name = @.table)
insert into myidents values (@.table, 0)
update myidents
set @.ident = ident = ident + 1
where name = @.table
end
now, (ignoring for now the use of reserved words) the problem is that
this is called frequently, from other procedures. Trouble is that the
calling procedures call it from within a transaction. We now have a
wickedly hot spot on this table, with frequent deadlocks.
Is there any relatively quick fix for this? Some locking hints or
whatever.
Or do we need to go and recode, moving this kind of thing outside the
transaction (which are all rather too long for my liking), and even
cosidering using identity columns as a replacement?
ThanksIMHO, IDENTITY would be the best approach with the current design. You
might also consider using natural keys instead of surrogate key values.
If your actual myident table has no primary key on name, this will
contribute to blocking/deadlocking. Unless your application always acquires
values in the same sequence, you will be vulnerable to deadlocks unless you
specify a TABLOCKX hint. This is obviously bad for concurrency (especially
for long-running transactions) and should be avoided if possible.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"WangKhar" <Wangkhar@.yahoo.com> wrote in message
news:bb269444.0401220321.38788fa2@.posting.google.c om...
> Gents,
> I have come into a system that uses a secondary table to generate (for
> want of a better word) Identities.
> eg
> create table myidents
> ( name sysname not null, ident int not null)
> create procedure getnextident @.table sysname, @.ident int output
> as
> begin
> if not exists (select top 1 1 from myidents where name = @.table)
> insert into myidents values (@.table, 0)
> update myidents
> set @.ident = ident = ident + 1
> where name = @.table
> end
> now, (ignoring for now the use of reserved words) the problem is that
> this is called frequently, from other procedures. Trouble is that the
> calling procedures call it from within a transaction. We now have a
> wickedly hot spot on this table, with frequent deadlocks.
> Is there any relatively quick fix for this? Some locking hints or
> whatever.
> Or do we need to go and recode, moving this kind of thing outside the
> transaction (which are all rather too long for my liking), and even
> cosidering using identity columns as a replacement?
> Thanks|||You could try this:
if not exists (select top 1 1 from myidents ROWLOCK, UPDLOCK where name =
@.table)
Hope it will help.
Igor
"WangKhar" <Wangkhar@.yahoo.com> wrote in message
news:bb269444.0401220321.38788fa2@.posting.google.c om...
> Gents,
> I have come into a system that uses a secondary table to generate (for
> want of a better word) Identities.
> eg
> create table myidents
> ( name sysname not null, ident int not null)
> create procedure getnextident @.table sysname, @.ident int output
> as
> begin
> if not exists (select top 1 1 from myidents where name = @.table)
> insert into myidents values (@.table, 0)
> update myidents
> set @.ident = ident = ident + 1
> where name = @.table
> end
> now, (ignoring for now the use of reserved words) the problem is that
> this is called frequently, from other procedures. Trouble is that the
> calling procedures call it from within a transaction. We now have a
> wickedly hot spot on this table, with frequent deadlocks.
> Is there any relatively quick fix for this? Some locking hints or
> whatever.
> Or do we need to go and recode, moving this kind of thing outside the
> transaction (which are all rather too long for my liking), and even
> cosidering using identity columns as a replacement?
> Thanks|||WangKhar (Wangkhar@.yahoo.com) writes:
> create procedure getnextident @.table sysname, @.ident int output
> as
> begin
> if not exists (select top 1 1 from myidents where name = @.table)
> insert into myidents values (@.table, 0)
> update myidents
> set @.ident = ident = ident + 1
> where name = @.table
> end
> now, (ignoring for now the use of reserved words) the problem is that
> this is called frequently, from other procedures. Trouble is that the
> calling procedures call it from within a transaction. We now have a
> wickedly hot spot on this table, with frequent deadlocks.
> Is there any relatively quick fix for this? Some locking hints or
> whatever.
Deadlocks can usually be avoided by:
BEGIN TRANSACTION
SELECT @.ident = ident FROM tbl (UPDLOCK) WHERE name = @.table
UPDATE tbl SET ident = @.ident WHERE name = @.table
COMMIT TRANSACTION
However, if you have more work in the transaction, you will have lot of
blocking and not much concurrency in the system. And if you in some
procedures perform work before you come here, they might be blocking
other processes that aleady have an ident, and now wil block the other
prcoess.
The above is only really meaningful if you have busienss rules that
require consecutive series. Else you should move out the ident-generation
out of the transaction or use IDENTITY instead.
--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks thats pretty much what I thought ...
See if I can convince people that we need to take this action :(
Erland Sommarskog <sommar@.algonet.se> wrote in message news:<Xns94794AA4B719Yazorman@.127.0.0.1>...
> WangKhar (Wangkhar@.yahoo.com) writes:
> > create procedure getnextident @.table sysname, @.ident int output
> > as
> > begin
> > if not exists (select top 1 1 from myidents where name = @.table)
> > insert into myidents values (@.table, 0)
> > update myidents
> > set @.ident = ident = ident + 1
> > where name = @.table
> > end
> > now, (ignoring for now the use of reserved words) the problem is that
> > this is called frequently, from other procedures. Trouble is that the
> > calling procedures call it from within a transaction. We now have a
> > wickedly hot spot on this table, with frequent deadlocks.
> > Is there any relatively quick fix for this? Some locking hints or
> > whatever.
> Deadlocks can usually be avoided by:
> BEGIN TRANSACTION
> SELECT @.ident = ident FROM tbl (UPDLOCK) WHERE name = @.table
> UPDATE tbl SET ident = @.ident WHERE name = @.table
> COMMIT TRANSACTION
> However, if you have more work in the transaction, you will have lot of
> blocking and not much concurrency in the system. And if you in some
> procedures perform work before you come here, they might be blocking
> other processes that aleady have an ident, and now wil block the other
> prcoess.
> The above is only really meaningful if you have busienss rules that
> require consecutive series. Else you should move out the ident-generation
> out of the transaction or use IDENTITY instead.