Triger

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;
/
RichardsoetAsked:
Who is Participating?
 
HilaireCommented:
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
 
HilaireCommented:
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
 
RichardsoetAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
HilaireCommented:
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
 
RichardsoetAuthor Commented:
OK THANKS
0
 
Anthony PerkinsCommented:
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
 
Anthony PerkinsCommented:
>>I would appreciate any comments by the experts that would help me in making a recommendation.<<
Award points to Hilaire.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.