Solved

SQL Server 2000 creating sequence and equivalent of currval and nextval

Posted on 2004-09-28
4
1,723 Views
Last Modified: 2007-12-19
Currently I have a need in a web interface I'm building in ASP to call a sequence via SQL for an insert statement. However, I have a strong Oracle background and don't know how to create a sequence in SQL Server 2000. Additionally I have a need for a second insert statement  on the page to use the currval or same number as the first sequence.

I can explain this better if need be - but basically I'm trying to do the following in SQL Server 2000 like the Oracle equivalent of the following:

insert into students (id, first_name, last_name) values student_sequence.NEXTVAL, 'Scott', 'Smith');

then a second insert statement will place data into another table but use that same id's value:

insert into other_students(id, last_name, grade) values
students_sequence.CURRVAL, 'Smith, 'A')

Thus, I don't know how to create a sequence in SQL Server 2000 nor use the equivalent of nextval and currval

Thanks
0
Comment
Question by:dylanone
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 200 total points
Comment Utility
SQL Server doesn't use sequences like Oracle does. Here's how to handle it.

Define the ID column in the Students table as having the IDENTITY value. If you're using Enterprise Manager to create the table, the Identity option is at the bottom of the window. If you're using SQL, it's like this:

CREATE TABLE Students(ID as IDENTITY(1,1), First_Name varchar(50), Last_Name varchar(50))

Now you can do an insert while ignoring the ID column, and the value will be incremented and inserted automatically:
INSERT Students(First_Name, Last_Name) VALUES('Scott','Smith')

However since you want to use the new ID in second insert, the easiest way is by creating a stored procedure to do both inserts:

CREATE PROCEDURE up_InsertStudent @FirstName varchar(50), @LastName varchar(50), @Grade tinyint AS

BEGIN TRAN
  INSERT students(id, first_name, last_name) VALUES (@FirstName, @LastName)
  INSERT other_students(id, last_name, grade) VALUES(@@Identity, @LastName, @Grade)
COMMIT


You use this stored procedure like this:
EXECUTE up_InsertStudent 'Scott', 'Smith', 95
0
 

Author Comment

by:dylanone
Comment Utility
The above solution looks - good I haven't tried to run it yet - since it appears the primary key columns of id are varchars and don't have an identity associated with them and I can't add them because they are generated via an outside process.

Therefore is still some other round about way - to generating some kind of unique sequence using SQL Server - they don't have any kind of built in function to do something like this that I can just call during my insert of those values?

Could I also possible create my own outside sequence table?

0
 

Author Comment

by:dylanone
Comment Utility
I closed this one out because the above did answer my question - I just posted a new question as I'm still fiddling around with the sequences:

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21150107.html

0
 
LVL 15

Expert Comment

by:jdlambert1
Comment Utility
Nuts. I composed a long response to your post from 9:26 -- but I must have gotten distracted before I hit submit. I'll try to check out your next Q when I get home tonight. Sorry...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now