?
Solved

SQL Server 2000 creating sequence and equivalent of currval and nextval

Posted on 2004-09-28
4
Medium Priority
?
1,839 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 800 total points
ID: 12176101
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
ID: 12179523
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
ID: 12184223
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
ID: 12184379
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

764 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