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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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




RichardsoetAuthor Commented:
OK THANKS
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
Anthony PerkinsCommented:
>>I would appreciate any comments by the experts that would help me in making a recommendation.<<
Award points to Hilaire.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.