0

I have a SQL Server T-SQL query that I need to translate to Oracle PL-SQL.

My SQL Server query is:

DECLARE @inputData

BEGIN 
    SELECT @inputData = column_name 
    FROM table;
END

What does select DECLARE variable = column_name from any table do?

EDIT

I have tried on my own to research this but cannot a find proper explanation. SOme of so called expert here did not like the small block of code here is my entire procedure which I am trying to translate for 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 I dont think its duplicate of any question so Intially I had just posted small portion of my code. Since some of so called expert did not like it so here is my complete code that needs to be translated in oracle. Which I have done but i had hard time understanding the case

What does select anydeclaredvariable = column_name from anytable does?

Manoj
  • 327
  • 1
  • 11
  • 1
    The answer is literally in the first link on google when you seach "SQL Server Variables". Doesn't sound like you did much research. – dfundako Jan 18 '17 at 17:52
  • 1
    here you have an answer: [link](http://stackoverflow.com/questions/10886568/how-do-i-declare-and-use-variables-in-pl-sql-like-i-do-in-t-sql) – M84 Jan 18 '17 at 17:54
  • 1
    Collecting points at stack overflow doesnot make you expert and i hope downvoting may serve your ego but it doesnt help others. And its not duplicate of the above links and it has been answered by others here – Manoj Jan 18 '17 at 18:30

3 Answers3

2

variable = column_name

assigns column's value to your variable.

Something like this should work in Oracle:

declare
  input_data my_table.my_column%type;
begin
  select my_column into input_data from my_table where /* some predicate(s) */;
end;
/
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
2

Your question asks

What does DECLARE variable = column_name do?

The statement: DECLARE @Variable int = 5 will create the variable named @Variable and initialize its contents to the value 5.

If your question instead is: "What does SELECT @inputData = column_name FROM table do?", then the answer is that it assigns the value returned for column_name to the variable @inputData.

I hope this answers your question.

STLDev
  • 5,950
  • 25
  • 36
2

Important difference between MS Sql and Oracle is that in MS

SELECT @inputData = column_name FROM table;

runs OK even with no predicates i.e. with multirows result set, returning the value of column_name from an arbitrary row. In Oracle

SELECT column_name INTO input_data FROM my_table;

will fail if the result set contains more then one row. You shouldn't just translate syntax thoughtlessly.

Serg
  • 22,285
  • 5
  • 21
  • 48
  • 1
    I was just trying to understand i have found that in sql server it serves the purpose of retriving column value from single row. THanks it served my purpose. – Manoj Jan 18 '17 at 18:19
  • 1
    Thank you serg for you time and help it solved my problem. But I dont know why i get downvoted for genuine question – Manoj Jan 18 '17 at 18:27
  • 1
    I submit that even though the query might run in SQL server with multiple rows, the fact that you cannot determine what row will provide the value woudl be enough that the structure would not be suitable for any database querying that I have done, – HLGEM Jan 18 '17 at 18:29
  • 1
    @Manoj , i think the reason is that `SELECT @DISB_DISB = SUM(d.disburse_amt) FROM ..` and `SELECT @var = column_x FROM ..` are drastically different. First one returns exactly one row, the result of the second is unpredictable. My guess you won't be downvoted if started with the real query. – Serg Jan 18 '17 at 18:44
  • 1
    Serg, my purpose was to just to understand single line so didnt wish to post entire page of query, which you helped out and explained and that served my purpose. Thanks – Manoj Jan 18 '17 at 18:49