473,395 Members | 1,915 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,395 software developers and data experts.

Sequences in MS Server

22
Hello all,

I am having some trouble with functions and triggers within MSDE; the majority of my database experience comes from Oracle and I want to implement similar functions and trigger used in Oracle within MS Server.

I am trying to create a sequence on an ID that reference the last ID (before insert) in a table and then increments by 1 to create the next number in the sequence to act as a primary key.

The syntax in Oracle is below (if this helps clarify my problem)

DROP SEQUENCE squence_name;
CREATE SEQUENCE squence_name
START WITH 1
INCREMENT BY 1
nomaxvalue;

CREATE OR REPLACE TRIGGER TABLE_ID_TRIGGER
BEFORE INSERT
ON TABLE
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT squence_name.nextval INTO :NEW.TABLE_ID FROM dual;
END;
/

What I have been able to find so far is this “Auto-Generated Sequence” that is meant to work in MS Server.

CREATE FUNCTION FUNCTION_ID()
RETURNS NUMERIC
AS
BEGIN
DECLARE @LASTVAL NUMERIC
SET @LASTVAL = (SELECT MAX(FUNCTION_ID) from TABLE)
IF @LASTVAL IS NULL SET @LASTVAL = + 1
DECLARE @i INT
RETURN @i
END

I am able to create this function but how does this function fire without some sort of BEFORE INSERT call to input the next number as a primary key in the table?

Regards

-- Quish
Mar 9 '08 #1
2 1289
ck9663
2,878 Expert 2GB
Hello all,

I am having some trouble with functions and triggers within MSDE; the majority of my database experience comes from Oracle and I want to implement similar functions and trigger used in Oracle within MS Server.

I am trying to create a sequence on an ID that reference the last ID (before insert) in a table and then increments by 1 to create the next number in the sequence to act as a primary key.

The syntax in Oracle is below (if this helps clarify my problem)

DROP SEQUENCE squence_name;
CREATE SEQUENCE squence_name
START WITH 1
INCREMENT BY 1
nomaxvalue;

CREATE OR REPLACE TRIGGER TABLE_ID_TRIGGER
BEFORE INSERT
ON TABLE
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT squence_name.nextval INTO :NEW.TABLE_ID FROM dual;
END;
/

What I have been able to find so far is this “Auto-Generated Sequence” that is meant to work in MS Server.

CREATE FUNCTION FUNCTION_ID()
RETURNS NUMERIC
AS
BEGIN
DECLARE @LASTVAL NUMERIC
SET @LASTVAL = (SELECT MAX(FUNCTION_ID) from TABLE)
IF @LASTVAL IS NULL SET @LASTVAL = + 1
DECLARE @i INT
RETURN @i
END

I am able to create this function but how does this function fire without some sort of BEFORE INSERT call to input the next number as a primary key in the table?

Regards

-- Quish
Would an IDENTITY\ column solve your problem?

-- CK
Mar 10 '08 #2
Quish
22
Thanks for the help,

Quish
Mar 10 '08 #3

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

Similar topics

4
by: temp | last post by:
Hi All, I wonder could someone help me with this? What I want to do is search through a list of letters and look for adjacent groups of letters that form sequences, not in the usual way of...
0
by: Jim McAtee | last post by:
Is there any means of running MySQL (3.23, mostly MyISAM tables running on Win2k Server) so that escaped character sequences (those preceded by a backslash \) are _not_ interpretted as anything...
0
by: Petr Matousek | last post by:
Hello, I have a table containing integer counters associated with particular datetime like this: CREATE TABLE `counters` ( ... `when` datetime NOT NULL default '0000-00-00 00:00:00',...
10
by: Vilson farias | last post by:
Greetings, I'm getting a big performance problem and I would like to ask you what would be the reason, but first I need to explain how it happens. Let's suppose I can't use sequences (it seams...
4
by: Hemant Shah | last post by:
Folks, I have created bunch of sequences in DB2 7.2. How do I list all the defined sequences and if possible their current values? Thanks. --
3
by: Ken | last post by:
HI: I'm reading a string that will be displayed in a MessageBox from a resource file. The string in the resource file contains escape sequences so they will be broken up into multiple lines. ...
18
by: Bruno Baguette | last post by:
Hello, I have to design a table wich will store some action reports. Each report have an ID like this 1/2004, 2/2004, ... and each years, they restart to 1 (1/2004, 1/2005, 1/2006,...). So, I...
3
by: Sean Shanny | last post by:
To all, We dumped our instance from a 7.4.2 instance compiled with a block size of 32k running on OSX 10.3.3 server.. We loaded this dump on Fedora 2.6.5-1.358smp running postgresql 7.4.2...
5
by: Michael Fuhr | last post by:
I'd like to propose that certain GRANTs on a table cascade to the table's implicit sequences. In the current implementation (as of 7.4.5 and 8.0.0beta3), a table owner must typically issue GRANT...
4
by: JJ | last post by:
Is there a way of checking that a line with escape sequences in it, has no strings in it (apart from the escape sequences)? i.e. a line with \n\t\t\t\t\t\t\t\r\n would have no string in it a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.