Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Triger

Posted on 2005-04-25
10
Medium Priority
?
353 Views
Last Modified: 2008-03-10
Could you please convert the below oracle script trigger  to  sql 200 script

PROMPT  TRIGGER FA_Trigger:
create sequence fa_seq;
 
create or replace trigger fa_trigger before insert on fadebtp for each row
begin
if ( :new.faeyne is null ) then
select fa_seq.nextval into :new.faeyne from dual;
end if;
end;
/
0
Comment
Question by:Richardsoet
  • 3
  • 2
  • 2
7 Comments
 
LVL 26

Expert Comment

by:Hilaire
ID: 13857702
MS SQL Server does not have an equivalent to Oracle Sequences,
(you can emulate them but it's not worth the hassle in this case IMHO)
instead, I suggest using SQL Server built-in functionlalities :

make the faeyne column an IDENTITY column and SQL Server will insert an incremental sequence number for you each time a new record is inserted in the fadebtp table
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 2000 total points
ID: 13857740
in short, it means modify your table creation script as follows :
change

CREATE TABLE fadebtp (
      faeyne INT NOT NULL,
      field2 field2_datatype,
      field3 .....
)

to

CREATE TABLE fadebtp (
      faeyne INT IDENTITY,
      field2 field2_datatype,
      field3 .....
)

by default an IDENTITY field is created with a seed(first value) of 1 and an increment of 1,
but you can give explicit values if need be
eg
     faeyne INT IDENTITY(10,2),

would create an incremental sequence number starting at 10 with a step of 2

If the table is already created and if you don't want to drop-create the table,
you can make this column an identity column jsut by changing its properties in Enterprise Manager: opne the table in design mode and set the field to identity

As a side note, if you're planning to use this table with sequence numbers above
2^31, it might be a good idea to use the BIGINT rather than INT datatype.


0
 

Author Comment

by:Richardsoet
ID: 13857859
i have already got table fadebtp in the database, as shown below

PRINT'Creating table FADEBTP'

CREATE TABLE FADEBTP (
    FAFYNE  CHAR     (3)       DEFAULT ' ' NOT NULL,
    FAEYNE  NUMERIC  (11, 0)   DEFAULT 0   NOT NULL,
    FAWCCU  NUMERIC  (8, 0)    DEFAULT 0   NOT NULL,
    FAVQTM  NUMERIC  (6, 0)    DEFAULT 0   NOT NULL,
    FAYQVN  CHAR     (10)      DEFAULT ' ' NOT NULL,
    FAEZNE  NUMERIC  (11, 0)   DEFAULT 0   NOT NULL,
    FAGTSW  CHAR     (3)       DEFAULT ' ' NOT NULL,
    FAGOSW  CHAR     (1)       DEFAULT ' ' NOT NULL,
    FAGPSW  CHAR     (1)       DEFAULT ' ' NOT NULL,
    FAGQSW  CHAR     (1)       DEFAULT ' ' NOT NULL,
    FAE0NE  NUMERIC  (11, 0)   DEFAULT 0   NOT NULL,
    FAE1NE  NUMERIC  (3, 0)    DEFAULT 0   NOT NULL,
    FAJYVC  NUMERIC  (18, 2)   DEFAULT 0   NOT NULL,
    FAJZVC  NUMERIC  (18, 2)   DEFAULT 0   NOT NULL,
    FAJ0VC  NUMERIC  (18, 2)   DEFAULT 0   NOT NULL,
    FARNCG  CHAR     (6)       DEFAULT ' ' NOT NULL,
    FARMCG  CHAR     (6)       DEFAULT ' ' NOT NULL,
    FAROCG  CHAR     (2)       DEFAULT ' ' NOT NULL,
    FARPCG  CHAR     (2)       DEFAULT ' ' NOT NULL,
    FARQCG  CHAR     (2)       DEFAULT ' ' NOT NULL,
    FARRCG  CHAR     (2)       DEFAULT ' ' NOT NULL,
    FARSCG  CHAR     (2)       DEFAULT ' ' NOT NULL,
    FARTCG  CHAR     (3)       DEFAULT ' ' NOT NULL,
    FAE2NE  NUMERIC  (6, 1)    DEFAULT 0   NOT NULL,
    FARUCG  CHAR     (3)       DEFAULT ' ' NOT NULL,
    FAE3NE  NUMERIC  (3, 0)    DEFAULT 0   NOT NULL,
    FAJ1VC  NUMERIC  (18, 2)   DEFAULT 0   NOT NULL,
    FARVCG  CHAR     (3)       DEFAULT ' ' NOT NULL,
    FAGUSW  CHAR     (1)       DEFAULT ' ' NOT NULL,
    FAGVSW  CHAR     (1)       DEFAULT ' ' NOT NULL,
    FARWCG  CHAR     (2)       DEFAULT ' ' NOT NULL,
    FARXCG  CHAR     (2)       DEFAULT ' ' NOT NULL,
    FANWTA  CHAR     (12)      DEFAULT ' ' NOT NULL,
    FAGYSW  CHAR     (1)       DEFAULT ' ' NOT NULL,
    FAGZSW  CHAR     (1)       DEFAULT ' ' NOT NULL,
    FARZCG  CHAR     (2)       DEFAULT ' ' NOT NULL,
    FAJ8VC  NUMERIC  (18, 2)   DEFAULT 0   NOT NULL,
    FAWECU  NUMERIC  (8, 0)    DEFAULT 0   NOT NULL)   ON [DATA]

PRINT'Creating primary key and supporting index for FADEBTP'

ALTER TABLE FADEBTP ADD CONSTRAINT FADEBTP_PK PRIMARY KEY (
    FAFYNE,
    FAEYNE)

PRINT'Creating unique index FADEBTL2_IX ON FADEBTP'

CREATE UNIQUE INDEX FADEBTL2_IX ON FADEBTP (
    FAGOSW,
    FAFYNE,
    FAEYNE)  ON [INDEX]


i just want to convert the below oracle trigger to sql trigger

PROMPT  TRIGGER FA_Trigger:
create sequence fa_seq;
 
create or replace trigger fa_trigger before insert on fadebtp for each row
begin
if ( :new.faeyne is null ) then
select fa_seq.nextval into :new.faeyne from dual;
end if;
end;
/


thanks
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 26

Expert Comment

by:Hilaire
ID: 13858205
Actually I think that making the faeyne column an identity column is not an option since it sounds like it will not necessarily be UNIQUE
Currently trying various solutions ... with not much success ATM :-(

Even using an INSTEAD OF INSERT trigger, the NOT NULL constraint on the FAEYNE column makes it rather difficult ... it raises an error even before running the trigger's code.

I'll let you know if I find something better.
Hilaire




0
 

Author Comment

by:Richardsoet
ID: 13858214
OK THANKS
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 13859085
The way it works here on this site, (abbreviated) is that after you get the solution to a question and not longer than 3 weeks you close the question.  See here for more info:
How do I accept a comment?
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/help.jsp#hi68

In other words, if you now have a solution to this question please close it.

Also, please follow-up these abandoned questions:
1 03/30/2005 500 SQL Profiler  Open Microsoft SQL Server
2 03/29/2005 500 Windows Server 2003 Enterprise Edition {...  Open Windows Server 2003
3 03/14/2005 500 WebSphere with GZIP  Open Websphere
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 14035928
>>I would appreciate any comments by the experts that would help me in making a recommendation.<<
Award points to Hilaire.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

810 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