将SQL Server / T-SQL查询转换为Oracle PL / SQL

时间:2017-01-18 17:45:36

标签: sql sql-server tsql plsql

我有一个SQL Server T-SQL查询,我需要将其转换为Oracle PL-SQL。

我的SQL Server查询是:

DECLARE @inputData

BEGIN 
    SELECT @inputData = column_name 
    FROM table;
END

select DECLARE variable = column_name from any table做了什么?

修改

我已经尝试过自己去研究这个但是找不到合适的解释。 这里所谓的专家的SOme不喜欢这里的小块代码是我试图为oracle翻译的整个过程。

USE [Mon422_GA]
    GO
     /****** Object:  StoredProcedure [dbo].[p_DisbAmtCheck]    Script Date:        1/18/2017 11:33:19 AM ******/
   SET ANSI_NULLS ON
   GO
  SET QUOTED_IDENTIFIER ON
  GO

 ALTER                                  PROCEDURE [dbo].[p_DisbAmtCheck]
 (
  @amount decimal(18,2),
 @type nvarchar(10),
  @id int,
  @app_id int,
  @loan_id int,
  @disb_fee_id int = 0,
  @disb_cs_fees nvarchar(255) = '0'
 )
 AS
BEGIN
   DECLARE @TOTAL_DISB decimal(18,2), @DISB_DISB decimal(18,2), @FEE_DISB          decimal(18,2),
        @WRK_TOTAL decimal(18,2), @CUR_DISB decimal(18,2), @FEE_CLOSE char(1),
        @FEE_FIN char(1), @TMP_SQL nvarchar(512)
SET @TOTAL_DISB = 0.00
SET @DISB_DISB = 0.00
SET @FEE_DISB = 0.00
SET @WRK_TOTAL = 0.00
SET @CUR_DISB = 0.00
IF @type NOT LIKE 'cs_disb'
BEGIN
    SELECT @DISB_DISB = SUM(d.disburse_amt)
    FROM t_ccs_disburse d, t_ccs_loan_disburse_rel ldr, t_ccs_app_loan_rel alr
    WHERE ldr.disburse_id = d.disburse_id AND ldr.loan_id = alr.loan_id
        AND alr.app_id = @app_id AND alr.loan_id = @loan_id
        AND d.disburse_loan_fee_ind != 'F'
END
IF @DISB_DISB IS NULL
    SET @DISB_DISB = 0.00
IF @type NOT LIKE 'cs_fee'
BEGIN
    SET @TMP_SQL = 'SELECT SUM(f.fee_amt)
        FROM t_ccs_fee f, t_ccs_app_fee_rel r
        WHERE r.fee_id = f.fee_id
        AND r.app_id = ' + CONVERT(NVARCHAR, @app_id) +
        ' AND r.loan_id = ' + CONVERT(NVARCHAR, @loan_id) + '
        AND (fee_financed_yn = ''Y'' OR fee_collect_at_closing_yn = ''Y'')
        AND f.fee_id NOT IN ('
    IF LEN(@disb_cs_fees) > 0
        SET @TMP_SQL = @TMP_SQL + @disb_cs_fees + ')'
    ELSE
        SET @TMP_SQL = @TMP_SQL + '0)'
    EXEC ('DECLARE fees CURSOR FOR '+ @TMP_SQL)
    OPEN fees
    FETCH NEXT FROM fees INTO @FEE_DISB
    CLOSE fees
    DEALLOCATE fees
END
IF @FEE_DISB IS NULL
    SET @FEE_DISB = 0.00
SELECT @TOTAL_DISB = ln_amount FROM t_ccs_loan WHERE loan_id = @loan_id
IF @TOTAL_DISB IS NULL
    SET @TOTAL_DISB = 0.00
SET @WRK_TOTAL = @DISB_DISB + @FEE_DISB + @amount
IF @WRK_TOTAL IS NULL
    SET @WRK_TOTAL = 0.00
IF @id > 0
BEGIN
    IF @type LIKE 'fee'
        SELECT @CUR_DISB = fee_amt FROM t_ccs_fee WHERE fee_id = @id
    ELSE IF @type LIKE 'disb'
        SELECT @CUR_DISB = disburse_amt FROM t_ccs_disburse WHERE disburse_id = @id
    ELSE
        SET @CUR_DISB = 0.00
    IF @CUR_DISB IS NULL
        SET @CUR_DISB = 0.00
END
ELSE
BEGIN
    SET @CUR_DISB = 0.00
    IF @type LIKE 'disb'
    BEGIN
        -- check to see if this fee is financed or collect at close if so subtract from total
        SELECT @FEE_FIN = fee_financed_yn, @FEE_CLOSE = fee_collect_at_closing_yn
        FROM t_ccs_fee WHERE fee_id = @disb_fee_id
        IF @FEE_FIN = 'Y' OR @FEE_CLOSE = 'Y'
            SET @WRK_TOTAL = @WRK_TOTAL - @amount
    END
END
SET @WRK_TOTAL = @WRK_TOTAL - @CUR_DISB
IF @WRK_TOTAL > @TOTAL_DISB
    SELECT '0' as PASSED
ELSE
    SELECT '1' as PASSED

END 我不认为它的任何问题的重复,所以我刚刚发布了我的一小部分代码。由于一些所谓的专家不喜欢它,所以这里是我需要在oracle中翻译的完整代码。我已经做了但我很难理解这个案例

是什么         从anytable中选择anydeclaredvariable = column_name吗?

3 个答案:

答案 0 :(得分:1)

  

variable = column_name

将列的值分配给您的变量。

这样的事情应该在Oracle中起作用:

Wales vs Italy

答案 1 :(得分:1)

你的问题是

  

DECLARE variable = column_name做了什么?

声明:DECLARE @Variable int = 5将创建名为@Variable的变量,并将其内容初始化为值5

如果您的问题是:" SELECT @inputData = column_name FROM table做什么?",那么答案就是它将column_name返回的值分配给变量{{1} }。

我希望这能回答你的问题。

答案 2 :(得分:1)

MS Sql和Oracle之间的重要区别在于MS

SELECT @inputData = column_name FROM table;

即使没有谓词也运行正常,即使用多行结果集,从任意行返回column_name的值。在Oracle中

SELECT column_name INTO input_data FROM my_table;
如果结果集包含多行,

将失败。 你不应该轻率地翻译语法。