Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 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 November 10th, 2006, 11:25 PM
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Darrell86
Default Need help! Setting PK value at runtime!

Hello, Im new too,

Im following a wrox ebook - beginning sql server 2005 express - starter kit edition, and i am having problems with chapter 15 - basically i have a dataset on a form and the stored procedure is meant to return the next integer primary key value automatically to populate the "PersonID" feild in the grid view control. I have run the example file just to make sure i havent made an error copying the programming text but it gives the same result. Basically it allways puts a 0 in the personID column, yet if i execute the stored procedure, the return_value given in the debugging window is correct. Basically the stored procedure looks up the last integer used for this particular table in a seperate table called pkvalue. It takes the table name as an input, as well as another input which is left to default. Im not worried about the stored procedure as when it is run it returns the correct value. I am worried about how i should execute this correctly from a from so that when i add a record it uses the stored procedure correctly to retrieve the return value.

Please please please can someone help. I have tried everything so now i am reduced to begging. I even converted my filesystem to NTFS from Fat32 on a whim. I have used the surface area configuration utility to ensure networking is on and that CLR is on.

Any insight anyone can give, or even a working example file of automatically generating an integer primary key value for a multiuser application? I have had no problems using GUID to set the primary key values, but i want to learn how they can be set with a stored procedure.

Thankyou

 
Old November 12th, 2006, 12:14 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

I don't know why they even teach it that way... I'd use an IDENTITY column (an autonumbering column type) as the PK column and use Scope_Identity() to return the PK of the record just inserted. Using a separate "sequence" table to do this is a major source of deadlocks and "Death by SQL".

--Jeff Moden
 
Old November 12th, 2006, 10:14 AM
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Darrell86
Default

aaaah ok - thankyou

I havent used scope_identity before, it is not covered in this ebook! Would anyone know of any examples i could read up on, or any ebooks i could buy - in which it runs you through the basics of using scope_identity to set the PK feild of new record on a form,

I take it is still run from a stored procedure?

Thankyou

 
Old November 12th, 2006, 02:58 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Right after you do an insert to a table that has an IDENTITY column as the PK, you would do a ...

SELECT @SomeVariable = SCOPE_IDENTITY()

@SomeVariable will contain the last value automatically created in the IDENTITY column.

To create a table with an IDENTITY column...

 CREATE TABLE SomeTableName
        (SomePKColumnName INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
         othercolumnnames ....
        )

Anytime you insert into SomeTableName, the SomePKColumnName will automatically increment by 1 for the new row. You cannont insert into the SomePKColumnName... it is an autonumbering column and will take care of it's self.

--Jeff Moden
 
Old November 12th, 2006, 03:02 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

p.s.

You might want to look up IDENTITY, CREATE TABLE, and SCOPE_IDENTITY in Books Online. If you don't know what that is, it's the "help" system for MS-SQL Server... I don't know if they still call it "Books Online" in SQL Server 2005 but it is generally disquised as "Help, Transact SQL" in SQL Server 2000. Might be able to find it in SQL Sever listings under the Windows Start Button.

--Jeff Moden
 
Old November 12th, 2006, 07:37 PM
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Darrell86
Default

ahh Thank you,

This was exatly the help i needed, its a shame they dont teach you that in the book instead they give you that whole seperate pktable approach,

Anyway this is fantastic thanks for your help

Regards

 
Old November 20th, 2006, 01:33 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

I'm not sure why anyone would teach such a thing... and, they should be publically executed for being so bloody foolish! SQL Server has the IDENTITY property... Oracle has Sequences... why would anyone want to make a PkTable (also called a Sequence or NextID table) which will become a true hotspot and cause more deadlocks than you can shake a stick at? We had a third party vendor that did such a thing to our software... before I got there, they were up to 4000 (not a misprint, it's four THOUSAND) deadlocks per day. I was able to come up with a work around and get down to about 12 deadlocks a week but that's still unsatisfactory in my book and it took a hell of a long time to find all the abhorrent code that used the table. As you can tell by the number of deadlocks, we still haven't repaired all the code.

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection String setting at runtime TwoTrees BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 3 July 2nd, 2010 11:41 AM
ConnectionString setting at runtime TwoTrees ASP.NET 3.5 Basics 2 July 22nd, 2008 03:30 AM
Setting Runtime DB connection from JSP ssivakumar76 BOOK: Beginning Java 2, JDK 5 Edition 0 June 16th, 2006 03:07 AM
PK problem reindeerw Access 10 April 15th, 2004 01:26 AM
setting DBNull to a string in a dataset at runtime texasraven Classic ASP Professional 2 September 3rd, 2003 04:28 PM





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