Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 22nd, 2005, 05:46 AM
Authorized User
 
Join Date: Aug 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to CarlosV
Default AutoIncrement in Insert on SQL Server 2K

Hello everyone, I've got a problem and I hope someone can help me. I need to inset a registry using Store procedure in SQL server 2k but my primary key isn't a autonumeric, so I need to give it a next ID, but I don't know how to use trigger so I try to make samething like this...

CREATE PROCEDURE sp_Insert_InvXn
            @VInvNo nvarchar(50),
            @VIDClient int,
            @VIDContact int,
            @VIDItem int,
            @VIDProduct int,
            @VInvName nvarchar(50),
            @VInvDetalle nvarchar(50),
            @VInvQuant int,
            @VInvUPrice money,
            @VInvAmount money
AS
          DECLARE @VNextID int
          SELECT @VNextID = Max(ID) FROM InXn

    INSERT INTO InvXn (
            ID,
            InvNo,
            IDClient,
            IDContact,
            IDItem,
            IDProduct,
            InvName,
            InvDetalle,
            InvQuant,
            InvUPrice,
            InvAmount
            )
    VALUES (
            @VNextID + 1,
            @VInvNo,
            @VIDClient,
            @VIDContact,
            @VIDItem,
            @VIDProduct,
            @VInvName,
            @VInvDetalle,
            @VInvQuant,
            @VInvUPrice,
            @VInvAmount
            )
GO

But when I run it, it told me that de ID can not be NULL, that mean @VNextID isn't work.
SO, PLEASE TO TELL ME WHAT I'VE TO DO, THANKS EVERYONE !!!

 
Old October 24th, 2005, 01:31 PM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CREATE PROCEDURE sp_Insert_InvXn
            @VInvNo nvarchar(50),
            @VIDClient int,
            @VIDContact int,
            @VIDItem int,
            @VIDProduct int,
            @VInvName nvarchar(50),
            @VInvDetalle nvarchar(50),
            @VInvQuant int,
            @VInvUPrice money,
            @VInvAmount money
AS
          DECLARE @VNextID int
          SELECT @VNextID = Max(ID)+1 FROM InXn

    INSERT INTO InvXn (
            ID,
            InvNo,
            IDClient,
            IDContact,
            IDItem,
            IDProduct,
            InvName,
            InvDetalle,
            InvQuant,
            InvUPrice,
            InvAmount
            )
    VALUES (
            @VNextID,
            @VInvNo,
            @VIDClient,
            @VIDContact,
            @VIDItem,
            @VIDProduct,
            @VInvName,
            @VInvDetalle,
            @VInvQuant,
            @VInvUPrice,
            @VInvAmount
            )
GO



“I sense many useless updates in you... Useless updates lead to defragmentation... Defragmentation leads to downtime...Downtime leads to suffering..Defragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/
 
Old November 8th, 2005, 01:28 AM
Authorized User
 
Join Date: Feb 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT @VNextID = Max(ID) FROM InXn

Modify the above line syntax like this to sort your problem.

SELECT @VNextID = ISNULL(Max(ID),0) FROM InXn

Cheers,
Pooja Falor
 
Old January 12th, 2006, 12:09 PM
Authorized User
 
Join Date: Sep 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Lalit_Pratihari
Default

Hi,

Or alternatively you can use this one:

SELECT @VNextID = CASE
            WHEN [ID] IS NULL THEN 1
            ELSE [ID] + 1
          END
FROM InXn

Hope this Helps.

Njoy,
Lalit
 
Old January 12th, 2006, 03:30 PM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

Those are good beginnings. However, they are not ready for concurrent stored procedure calls. You must wrap them in a transaction or else a concurrent call to the same stored procedure could result in an attempt to enter the same ID number and (presuming you have a Primary Key Constraint defined) will cause a Primary Key Constraint Violation.

BEGIN TRANSACTION
SELECT -- rest of your select statement

INSERT -- rest of your insert statement
COMMIT TRANSACTION

David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com
 
Old January 19th, 2006, 06:01 PM
Registered User
 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

He there,

I have similar question, relevant to autoincrement a column value while inserting a record. But this time, it is regarding adding multiple records at a time than adding single row. Here in this post, I wrote the solution I know and was looking for better one.

 Previously, if we need to do single INSERT, I did get @maxid from target table and a new row is added after incrementing the @maxid ...

 'SET @COL02 = ... value you want to insert
 'SET @COL03 = ... value you want to insert

 SELECT @MAXID = ISNULL(MAX(PRIMARYCOL01),0) FROM TARGET_TABLE
 INSERT INTO TARGET_TABLE(PRIMARYCOL01, COL02, COL03)
    VALUES(@MAXID + 1, @COL02, @COL03)


If I apply similar idea to process, dynamically increment the PRIMARYCOL01 while adding multiple rows at a time, the query might look like this

 INSERT INTO TARGET_TABLE(PRIMARYCOL01, COL02, COL03)
    SELECT @MAXID + 1, COL02, COL03
      FROM SOURCE_TABLE

However, it did NOT solve the incremental issue as SELECTION of Rows is done before insertion starts. Therefore, every row will have same @maxID + 1 value, but not @maxID + 1, @maxID + 2, @maxID + 3 ... so on.

I had an Idea to deal this but unfortunately it does work only on Oracle. The following query gets the values for PRIMARYCOL01 as required.

    SELECT @MAXID + ROWID, COL02, COL03
      FROM SOURCE_TABLE

Later I fetched on web and found following link to reproduce ROWID in SQL Server: http://support.microsoft.com/default...;EN-US;q186133

I concept works great but damn, the recent query I wrote to get ROWID (of a Complex Query OUTPUT - 4039 rows) took 3 min 34 secs. Obviously, no one would every want to spend waiting 3 mins 34 secs to just get ROWIDs v/s running the Complex query to generate results without ROWID in just 8 secs.

I know, there are some DB professionals out there, so please let us know if there is any better solution for inserting multiple rows while incrementing a primary column value and / or generating the ROWID in SQL server

Shyam Arjarapu
http://www.arjarapu.com









Similar Threads
Thread Thread Starter Forum Replies Last Post
Connect SQL Server 2k with VB from DSN and DBQ ayan.mukherjee SQL Language 0 February 12th, 2008 02:26 AM
SQL Server 2K, Access 2003 and VBA SteveBH SQL Server 2000 1 January 11th, 2007 01:23 PM
SQL Server 2k to XML using DTS Problem kwilliams SQL Server DTS 1 December 12th, 2005 12:11 PM
Professional SQL Server Development with Access 2K Fern All Other Wrox Books 0 January 29th, 2004 01:00 AM
Connectivity Problem To SQL Server 2k ctanchan Pro VB Databases 4 October 7th, 2003 11:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.