Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

sql - Keyword IF not expected

I have the following inline SQL code, running on an AS400. The IF comes up as unexpected but I don't understand why. What I'm trying to do is to build a stored procedure to update, or insert, a table row. The code shown below is inline to ensure my logic is correct.

I've tried a number of different approaches including IF EXISTS, CASE, SET @COUNT = SELECT COUNT(*). I continue to run into odd errors.

In the code shown below, I fail when I hit the IF statement.

CREATE OR REPLACE VARIABLE IN_LOC         CHAR(02);     
CREATE OR REPLACE VARIABLE IN_MCO         CHAR(02);     
CREATE OR REPLACE VARIABLE IN_SYMBOL      CHAR(03); 
CREATE OR REPLACE VARIABLE IN_POLICYNUM   CHAR(07); 
CREATE OR REPLACE VARIABLE IN_MODULE      CHAR(02); 
CREATE OR REPLACE VARIABLE IN_RISKLOC     CHAR(05); 
CREATE OR REPLACE VARIABLE IN_PRCCLASS    VARCHAR(05); 
CREATE OR REPLACE VARIABLE IN_PRCTER      VARCHAR(03); 
CREATE OR REPLACE VARIABLE IN_MESSAGE     CHAR(02); 
    
SET IN_LOC='00';    
SET IN_MCO='01';    
SET IN_SYMBOL='CPP';    
SET IN_POLICYNUM='0003748'; 
SET IN_MODULE='00'; 
SET IN_RISKLOC='00003'; 
SET IN_PRCCLASS='09';   
SET IN_PRCTER='1';  
SET IN_MESSAGE='NOT FOUND FOR ODD PROGRAMMING';

UPDATE BASPPRC01
    SET PRCCLASS=IN_PRCCLASS,PRCTER=IN_PRCTER,MESSAGE=IN_MESSAGE
WHERE CAST(IN_LOC||IN_MCO||IN_SYMBOL||IN_POLICYNUM||IN_MODULE||IN_RISKLOC AS CHAR(21))
    = CAST(LOCATION||MASTERCO||SYMBOL||POLICYNUM||MODULE||RISKLOC AS CHAR(21)) ;

IF @@ROWCOUNT = 0
BEGIN
    INSERT INTO BASPPRC01
        (LOCATION,MASTERCO,SYMBOL,POLICYNUM,MODULE,RISKLOC,PRCCLASS,PRCTER,MESSAGE)
    VALUES
        (IN_LOC
        , IN_MCO
        , IN_SYMBOL
        , IN_POLICYNUM
        , IN_MODULE
        , IN_RISKLOC
        , IN_PRCCLASS
        , IN_PRCTER
        , IN_MESSAGE ) ;
END

COMMIT TRANSACTION ;
question from:https://stackoverflow.com/questions/65849927/keyword-if-not-expected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should show the error you get...

with the following:

CREATE OR REPLACE PROCEDURE myproc ()
        LANGUAGE SQL
BEGIN
UPDATE BASPPRC01
    SET PRCCLASS=IN_PRCCLASS,PRCTER=IN_PRCTER,MESSAGE=IN_MESSAGE
WHERE CAST(IN_LOC||IN_MCO||IN_SYMBOL||IN_POLICYNUM||IN_MODULE||IN_RISKLOC AS CHAR(21))
    = CAST(LOCATION||MASTERCO||SYMBOL||POLICYNUM||MODULE||RISKLOC AS CHAR(21)) ;

IF @@ROWCOUNT = 0
BEGIN
    INSERT INTO BASPPRC01
        (LOCATION,MASTERCO,SYMBOL,POLICYNUM,MODULE,RISKLOC,PRCCLASS,PRCTER,MESSAGE)
    VALUES
        (IN_LOC
        , IN_MCO
        , IN_SYMBOL
        , IN_POLICYNUM
        , IN_MODULE
        , IN_RISKLOC
        , IN_PRCCLASS
        , IN_PRCTER
        , IN_MESSAGE ) ;
END

COMMIT TRANSACTION ;
END;

I get [SQL0199] Keyword BEGIN not expected. Valid tokens: THEN.

This passes a syntax check, note THEN and END IF instead of BEGIN also, COMMIT instead of COMMIT TRANSACTION

CREATE OR REPLACE PROCEDURE procedure2 ()
        LANGUAGE SQL
BEGIN
UPDATE BASPPRC01
    SET PRCCLASS=IN_PRCCLASS,PRCTER=IN_PRCTER,MESSAGE=IN_MESSAGE
WHERE CAST(IN_LOC||IN_MCO||IN_SYMBOL||IN_POLICYNUM||IN_MODULE||IN_RISKLOC AS CHAR(21))
    = CAST(LOCATION||MASTERCO||SYMBOL||POLICYNUM||MODULE||RISKLOC AS CHAR(21)) ;

IF @@ROWCOUNT = 0 THEN
    INSERT INTO BASPPRC01
        (LOCATION,MASTERCO,SYMBOL,POLICYNUM,MODULE,RISKLOC,PRCCLASS,PRCTER,MESSAGE)
    VALUES
        (IN_LOC
        , IN_MCO
        , IN_SYMBOL
        , IN_POLICYNUM
        , IN_MODULE
        , IN_RISKLOC
        , IN_PRCCLASS
        , IN_PRCTER
        , IN_MESSAGE ) ;
END IF;

COMMIT;
END;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...