代码改变世界

How to compare string in PL/SQL

2023-07-21 11:20  jetwill  阅读(17)  评论(0编辑  收藏  举报

Bash quoted variable substitution in sqlplus + here doc

-- https://stackoverflow.com/questions/6201942/bash-quoted-variable-substitution-in-sqlplus-heredoc
-- https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Redirections

sqlplus -s user/pw@db > $TMPFILE <<EOF
    SET ECHO OFF;
    SET FEEDBACK OFF;
    SET HEADING OFF;
      select x
      from a_table
      where a_field like '$AAA';
    EXIT;
EOF



cat << HERE
 $BASHPID
 '$BASHPID'
 HERE

# returns

4608
 '4608'


How to compare string in PL/SQL

-- You just need one equals, not two

IF SHIPMENT_EXPEDITE = 'PD' THEN
    DBMS_OUTPUT.PUT_LINE('Same');
END IF;

Oracle / PLSQL: REPLACE Function

-- https://www.techonthenet.com/oracle/functions/replace.php
REPLACE( string1, string_to_replace [, replacement_string] )
REPLACE('123123tech', '123');
Result: 'tech'

REPLACE('123tech123', '123');
Result:'tech'

REPLACE('222tech', '2', '3');
Result: '333tech'

REPLACE('0000123', '0');
Result: '123'

REPLACE('0000123', '0', ' ');
Result: '    123'

%TYPE Attribute

-- https://docs.oracle.com/cd/B13789_01/appdev.101/b10807/13_elems052.htm#:~:text=The%20%25TYPE%20attribute%20lets%20use,variables%2C%20fields%2C%20and%20parameters.

Examples

DECLARE
-- We know that BUFFER2 and BUFFER3 will be big enough to hold
-- the answers. If we have to increase the size of BUFFER1, the
-- other variables will change size as well.
   buffer1 VARCHAR2(13) := 'abCdefGhiJklm';
   buffer2 buffer1%TYPE := UPPER(buffer1);
   buffer3 buffer1%TYPE := LOWER(buffer1);

-- We know that this variable will be able to hold the contents
-- of this table column. If the table is altered to make the
-- column longer or shorter, this variable will change size as well.
   tname user_tables.table_name%TYPE;

-- %TYPE is great for subprogram parameters too, no need to
-- recompile the subprogram if the table column changes.
   PROCEDURE print_table_name(the_name user_tables.table_name%TYPE)
   IS
   BEGIN
      dbms_output.put_line('Table = ' || the_name);
   END;
BEGIN
   SELECT table_name INTO tname FROM user_tables WHERE ROWNUM < 2;
   print_table_name(tname);
END;
/