473,382 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

SP to add next sequential user ID

1,271 Expert 1GB
I'm just starting to write stored procedures. It took me hours to get this simple thing to work. Now I'm wondering what is the right way to do this. All I want to do is add a new row to the Users table and return the newly created UserID.

Expand|Select|Wrap|Line Numbers
  1. USE MessyTextSQL
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[AddNewUser]    Script Date: 11/06/2011 07:40:19 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROCEDURE [dbo].AddNewUser
  9. -- =============================================
  10. -- Author:        Jim Wolf
  11. -- Create date: 11/7/11
  12. -- Description:    Add new MessyText project
  13. -- =============================================
  14.   -- Add the parameters for the stored procedure here
  15.   @NewUserID int output
  16. AS
  17. BEGIN
  18.   SET NOCOUNT ON;
  19.  
  20.   -- Insert statements for procedure here
  21.   INSERT INTO Users(UserID)
  22.   VALUES (99999999)
  23.   SET @NewUserID =SCOPE_IDENTITY()
  24.   Update Users
  25.   Set UserID = cnt from Users where Users.cnt =@NewUserID
  26.  
  27. END
  28.  
Thanks for any advice,

Jim
Nov 7 '11 #1
9 2115
ck9663
2,878 Expert 2GB
Here, read this...

Happy Coding!!!

~~ CK
Nov 7 '11 #2
jimatqsi
1,271 Expert 1GB
Thanks, but that doesn't tell me much except that I could use @@IDENTITY or IDENT_CURRENT instead of SCOPE_IDENTITY.

Is there no way to set the UserID column to = the new identity without doing it in two separate steps of INSERT and UPDATE?

Thanks,
Jim
Nov 7 '11 #3
Rabbit
12,516 Expert Mod 8TB
I don't understand. Why are you storing the cnt field twice?
Nov 7 '11 #4
jimatqsi
1,271 Expert 1GB
Because I believe it's a really bad idea to use an auto-increment field as a primary key. Is that still true?

Jim
Nov 7 '11 #5
Rabbit
12,516 Expert Mod 8TB
I've heard the opposite. For all intents and purposes, using a surrogate key as a primary key has more advantages than disadvantages. And many of those disadvantages can be mitigated by careful design and consideration.

You can read more about surrogate keys here.

But more importantly, when someone says that using a surrogate key is a bad idea, they don't mean that you should copy the key into another field to use as a primary key. They mean that you should drop the surrogate key altogether and use a natural key.
Nov 7 '11 #6
Maraj
24
Hy jimatqsi!Why are you making it so complex if you want to return user id you only need a select statement after you have inserted new row.
Nov 9 '11 #7
jimatqsi
1,271 Expert 1GB
Thanks very much, Rabbit. The idea to copy the auto-generated ID to another column for the key is simply to guard against a problem if the table ever has to be rebuilt, which could generate new ID numbers and cause PK-FK problems.

My newly generated ID is, in fact, a natural key. New users get a new ID and ID numbers are generated sequentially. It's not like an order or invoice table that could have a key that matches a document ID.

So, in summary, it looks like using auto-generated ID numbers for keys is no longer frowned upon. I'll change my habit.

Thanks,

Jim
Nov 9 '11 #8
Rabbit
12,516 Expert Mod 8TB
If you ever have to rebuild a table with an identity (autonumber), you can still insert into that table with the identity defined and it won't create a new number. Also, you could drop the identity and put it back on later if you wanted.
Nov 9 '11 #9
ck9663
2,878 Expert 2GB
Just remember, unless the identity column is the primary key, it cannot insure uniqueness. Whether it is the primary key or not, it cannot insure sequence either.

Happy Coding!!!


~~ CK
Nov 9 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cliff williams | last post by:
How can I get a form to open with the next sequential number as set by me
4
by: cliff williams | last post by:
how can I set a form so that when i open it , it appears with the next sequential number. I want to set the number pattern and not use autonumber
6
by: Earl Anderson | last post by:
In A97 on WinXP, I'm trying to use a KB function to provide the next sequential number in a custom counter (a table with two fields-- and ). When I run it, I get the "User-defined type not...
1
by: Bijoy Naick | last post by:
I am inserting controls dynamically into my page; both Web Controls and User Controls. Code Below.. I noticed that inspite of specifying the ID for the user controls, in the HTML, the name is...
7
by: timasmith | last post by:
Hi, I have this app which is relatively database independent. I would like to serve the smallest scale (MS Access) up to the largest database server (Oracle). I recoginize that I will need...
0
by: Mythran | last post by:
I have: Class Library - User Control with Build Action set to "Embedded Resource" in a class library. User Control code-behind file Build Action set to "Compile". Web Application - Web page...
0
by: Mythran | last post by:
I apologize for the need to x-post this thread. The original thread is getting pretty old and no replies in microsoft.public.dotnet.framework.aspnet. Also, I'm aware that this isn't a C# only...
0
by: azoapes | last post by:
Hi, I've made a Setup Project to my C# WinApp and added a custom dialog to it that asks for user/pass for a database server. Then I created a Custom Action class and override the Install() to check...
9
by: Axxe | last post by:
I have searched high and low for cogent, well-explained coding to complete a project on which I have spent six months of work. I stumbled across something on this site that is close to what I...
2
by: ghjk | last post by:
In my php page I have many results. I want to display only 20 records per page and there should be link like 1,2,3,Next >>. When user click 2 it will goes to the second page. How can I do that?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.