简体   繁体   English

将Sql Server(如果不存在)转换为Oracle

[英]Converting Sql Server If not exists to Oracle

I have a snippet of a SQL Server procedure that looks like this: 我有一个SQL Server过程的片段,如下所示:

IF NOT EXISTS(SELECT ID FROM IDTABLE WHERE ID=@ID)
BEGIN
    DECLARE @MSG VARCHAR(100)
    SET @MSG = 'The ID (ID: ' + CONVERT(VARCHAR(50), @ID) + ') does not exist'
    EXEC SP_LOGAUDITMESSAGE 'Warning', 'Bill report', @MSG
    SET @RETVAL = 0
    RETURN
END

This runs the select query and if no results are returned, runs the begin/end block. 这将运行选择查询,如果没有返回结果,则运行begin / end块。 It declares a message variable and sends this, along with some other values, to a logging stored procedure before returning from the method. 它声明一个消息变量,并将其与其他一些值一起发送到日志存储过程,然后从该方法返回。

I'm on a project now though where I have to convert all of this stuff into Oracle (which I have zero familiarity with). 我现在在一个项目中,尽管我必须将所有这些东西都转换成Oracle(我对零熟悉)。 I ran it through the scratch editor in Sql Developer and got this: 我通过Sql Developer中的草稿编辑器运行了它,并得到了以下信息:

BEGIN
    SELECT 1
    INTO v_temp
    FROM DUAL
    WHERE NOT EXISTS
      ( SELECT ID FROM ID WHERE IDTABLE = v_ID
      );
  EXCEPTION
  WHEN OTHERS THEN
    NULL;
  END;
  IF v_temp = 1 THEN
    DECLARE
      v_MSG VARCHAR2(100);
    BEGIN
      v_MSG := 'The ID (ID: ' || UTILS.CONVERT_TO_VARCHAR2(v_ID,50) || ') does not exist' ;
      BEGIN
        SP_LOGAUDITMESSAGE('Warning', 'Bill report', v_MSG) ;
      EXCEPTION
      WHEN OTHERS THEN
        v_sys_error := SQLCODE;
      END;
      v_RETVAL := 0 ;
      RETURN;
    END;
  END IF;

Now, I get what it's trying to do - it's just taking a few more steps to do it, saving the results of the select statement into v_temp and then if it is set to equal 1, running the if block. 现在,我了解了它正在尝试做的事情-仅需执行一些步骤,将select语句的结果保存到v_temp中,然后将其设置为1,然后运行if块。

However, I've a few queries. 但是,我有一些疑问。 Firstly, some simple googling has taught me that the first Exception block (when others then null) is terrible practice. 首先,一些简单的谷歌搜索告诉我,第一个Exception块(当其他块为null时)是很糟糕的做法。 What would an alternative be in this situation, or is it acceptable given the following if statement? 在这种情况下,替代方案是什么?或者考虑以下if语句,可以接受吗? Secondly, is this entire rewrite necessary, or is it possible to amalgamate the if and select statements (similar to the SQL above) to make things a little more succinct? 其次,是否需要整个重写,或者是否可以合并if和select语句(类似于上面的SQL)以使事情更简洁?

Something like this: 像这样:

DECLARE
  v_MSG   VARCHAR2(100);
  v_temp  integer;
BEGIN

  SELECT count(*)
    into v_temp
  FROM COF 
  WHERE ID = v_ID

  IF v_temp = 0 THEN
    v_MSG := 'The ID (ID: ' || to_char(v_id) || ') does not exist' ;
    SP_LOGAUDITMESSAGE('Warning', 'Bill report', v_MSG) ;
    v_RETVAL := 0;
  ELSE 
    v_RETVAL := 1; -- not sure about this though
  END IF;

RETURN;

EXCEPTION
   WHEN OTHERS THEN
      v_sys_error := SQLCODE;
   raise;
END;

Changes: 变化:

  • the test if the id exists is better done using a count(*) as that doesn't throw an exception if the id is not found 测试如果ID存在使用比较好做count(*)为不抛出异常,如果ID 找到
  • don't nest the declare blocks. 不要嵌套declare块。 That is confusing. 令人困惑。
  • I would use only a single exception handler for the whole procedure 我只会在整个过程中使用一个异常处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM