Derived Table - Stored Procedure.

Hi all,
Can any tell how you call stored procedure from Derived table.
and how manage the IN parameters ?
thnx in advance

Hi Reddeppa,
Could you please specify the database you are working on?
Also , BO Supports Derived Tables if and only if the underlying database supports In-line Views:
Syntax: Select tot from ( select fun(x) as tot from table) dtab ;
Ex: Select workdays from ( select workdays('10/10/2004' , '20/4/2005') as workdays from dual ) DerivedTab ;
Run the above SQL at the Database end, if it runs successfully then follow the below steps
1. Create a function at the database end.
for example :
Create or replace function testf(no number) return number
as
Begin
Return(10);
End;
2. Open the universe
3. Create a derived table using the following syntax:
for example :
Select TestF(10) from dual;
4. Click on parse and Ok.
5. Now create a report using Derived table
Note: This is tested on Oracle database.
Hope this will help you.
Regards
Sheeba

Similar Messages

  • Linked tables, stored procedures, and locking

    I'm working on an interface between two Oracle systems. I don't know if they're on the same server or not, but they are definitely two different database instances. The plan for this interface is that when a record is created on one of the systems, it will call a stored procedure on the other system to create the record there as well. I believe the relevant information will be passed via parameters to the stored procedure (not by querying the data in the first system). The ID number created on the second system gets passed back to the first system via an output parameter.
    A concern was raised about whether something like this might cause "rev-locking". Similar issues were raised on a different interface, but that interface used a linked table between the two systems. The original design for that interface had a stored procedure initiated from the second system, and it was to update data via the linked table in the first system. But this caused some locking issues. So a different interface was written, that only used the linked table as read-only.
    So the question is, do stored procedure calls between linked databases have the same issues as updates directly to linked tables? And a better question is, is there a document or white paper out there somewhere that describes the locking issues between linked databases, and presenting the "best practices" for this type of coding?
    Any help is appreciated!
    Christine Wolak
    [email protected]

    So the question is, do stored procedure calls between linked databases have the same issues as updates directly to linked tables? I'm not aware of any issues with updates across databases. can you post a more detail version of what exact issues did you encounter when updating tables across database using database links?
    when you update a row in a table, from the same database or another one, a lock on that row will be placed for the duration of the transaction. Others will be able to read that row but not update it till the end of the local (or the remote) transaction.
    What issue(s) did you encounter?

  • Oracle - how to find columns in a table + stored procedures dependent on th

    Scenario:
    I need to list all the columns in a table1 and all stored procedures that depends on those columns of this table1. I need to populate column name and stored procedures to a new table.
    I created new_table(col1, sProc) and tried to populate the column name and respective stored procedure on this new_table. Code I wrote is given below:
    Declare
    Beginfor i in (select column_name from user_tab_columns where lower(table_name) like 'table1') loop
    insert into new_table
    select i.column_name,   name    from user_source   where lower(text) like '%' || i.column_name || '%';
    commit;
    end loop;
    end;
    Result: The scripts run successfully but the no data is populated on this new_table.
    Stress: I tried to solve it for 1 whole day and could not figure it out. Any help on this would be greately appreciated. Thank you once again.

    select i.column_name,   name    from user_source   where lower(text) like '%' || i.column_name || '%';Hi,
    At first check this,
    SQL> desc user_source
    Name                                      Null?    Type
    NAME                                               VARCHAR2(30)
    TYPE                                               VARCHAR2(12)
    LINE                                               NUMBER
    TEXT                                               VARCHAR2(4000)I tried it like this for my testing purpose,
    SQL> create table new_table(cname varchar2(40),pname varchar2(40));
    Table created.
    SQL> Declare
      2  v_pname varchar2(40);
      3  Begin
      4  for i in (select column_name from user_tab_columns where lower(table_name) like 'table1') loop
      5  dbms_output.put_line(i.column_name);
      6   select name into v_pname from user_source where lower(text) like '% '||lower(i.column_name)||' %';
      7  insert into new_table values(i.column_name ,v_pname);
      8  commit;
      9  end loop;
    10  end;
    11  /
    STR
    PL/SQL procedure successfully completed.
    SQL> select * from new_table;
    CNAME   PNAME
    STR       MY_PROCEDURETwinkle

  • How to list all tables/stored procedures used by the report

    All the reports i create are getting data from stored procedure(s). Is there a way to obtaining a listing of all the stored procedures without having to open report by report and check under Database > Set Datasource Location > Properties > Table Name?
    Finding this info it would be extremely valuable, as it would help me to judge the impact of any changes that i might be considering to one or more of the stored proc.
    So far i maintained a manual listing but it is not up-to-date and reliable. I would rather prefer to get an updated listing every time i want to change/drop a stored procedure.
    Thanks so much for your help.
    Rick

    Dell can you be a little bit more specific about the SDK solution. I could ask one of the developers to help me but i need to gather more details.
    I took a look .rpt inspector Pro but it does not do what i need. All i need is the the listing of all the database tables (in my case stored procs) used in my reports. No need to replace or change anything. I need to scan the directory where i have all the reports for the different applications and get report names and table/stored procs used. i can export the txt file to excel and that's all.

  • Stored Procedure to retirive column data

    Hello everyone... I am using sql server 2008. I want to retrieve column value if I pass column name as a parameter to my stored procedure without using case statement is there any way to retrieve column values....Please help me..

    Hi Sayali,
    If the columns LH1 and LC1 are having the correlation like LH2 and LC2,LH3 and LC3 ... LHxx and LCxx, as you'd like to pass only one column LH1, then LC1 can be hard coded in the stored procedure. Please see below.
    create table tbl(LH1 varchar(99),LC1 varchar(99));
    insert into tbl select 'this is LH1','this is LC1'
    go
    create proc proc1
    @columnName varchar(99)
    as
    declare @sqlStr nvarchar(max) = N'select @columnName from tbl';
    set @sqlStr = replace(@sqlStr,'@columnName',@columnName+','+replace(@columnName,'H','C'));
    exec sp_executesql @sqlStr;
    go
    exec proc1 @columnName='LH1'
    drop table tbl;
    drop proc proc1
    By the way, If this thread is derived from
    Stored Procedure with Column Name in Where Clause, the best solution would be the statement in your original post with cases, though it looks not that elegant.
    Best regards,
    Eric Zhang
    TechNet Community Support

  • Oracle Stored Procedure call in a Derived Table?

    Can a statement that defines derived table in the Designer contain a call to an Oracle Stored Proc? The proc takes one varchar2 parameter and returns a result set that consists of one field of several records and is currently implemented via sys_refcursor (return type is flexible as long as it allows to pass the result set back)
    Is there any difference between BOXI R2 and R3.0 regarding to the matter?

    Hi there,
    In XI R 3 you can now created a special universe to use stored procedures.A stored procedure universe is a special universe that enables Web Intelligence users to access stored procedures residing in the database. In BusinessObjects XI Release 3, you can use stored procedures with Desktop Intelligence and with universes in Designer. You also benefit from universes that contain stored procedures for Crystal Reports and Web Intelligence.
    XI R 2 no can do.
    Hope this helps
    Jacques

  • Please help - Can not use stored procedure with CTE and temp table in OLEDB source

    Hi,
       I am going to create a simple package. It has OLEDB source , a Derived transformation and a OLEDB Target database.
    Now, for the OLEDB Source, I have a stored procedure with CTE and there are many temp tables inside it. When I give like EXEC <Procedure name> then I am getting the error like ''The metadata  could not be determined because statement with CTE.......uses
    temp table. 
    Please help me how to resolve this ?

    you write to the temp tables that get created at the time the procedure runs I guess
    Instead do it a staged approach, run Execute SQL to populate them, then pull the data using the source.
    You must set retainsameconnection to TRUE to be able to use the temp tables
    Arthur My Blog

  • Execute Store procedure from derived table

    I would like to create a devired table to use in the LOV for my prompt.  Can I execute a store procedure in the defintion of the derived table instead of a sql statement ?  My store procedure is in a sql 2008 database

    Derived Table and SP are not the same. Derived table is more like a database view and is not a replacement for SP. I'd suggest you to use Derived table only if it is not possible to create a database table/view.
    Is it necessary for you to use your stored procedure as a data source? Are you trying to join it's result set to another table or view?
    You can create Materialized view in the database. Use that as a table in Universe.
    Try to use this object LOV in your prompt.

  • Can not delete data from table which is queried in my stored procedure

    Hi,
    Anyone knows how to fix it:
    I have a table. In a stored procedure, I have a simple query running on this table.
    When I want to delete one record from that table, I got error message:
    ORA-04091: table *** is mutating, trigger/function may not see it.
    Thanks first.

    Rick, the only time you should get a mutating table error is when a trigger is involved. FK problems have separate error codes (and would be a different problem than the one that started this thread). Using a DBA id check all the tables subject to DML by your function and by whatever calls your function for triggers. Only the owner or a DBA can see the existence of triggers on a non-owned table. This has caused some of the developers I work with to think tables that had triggers do not have them since they do not work under the owning or a DBA ID.
    By your description of what you are trying to do in the function we do that all the time without problem.
    How large is the function? How/When is it called?
    Mark D Powell

  • Table of records from a stored procedure

    Hi
    Where could I find an example or documentation about how to retrive a table of records from a Stored Procedure ??
    Thanks

    Try:
    CREATE OR REPLACE TYPE scott.MYRECORDTYPE as object
    (a int, b varchar2(40), c date, d number(10));
    CREATE OR REPLACE TYPE scott.MYTABLETYPE is table of myrecordtype;
    CREATE OR REPLACE PACKAGE BODY scott.MYPACKAGE
    as
    type number_collection is table of number(38) index by binary_integer;
    type varchar2_collection is table of varchar2(4000) index by binary_integer;
    type date_collection is table of date index by binary_integer;
    g_data myTableType;
    empno_col number_collection;
    ename_col varchar2_collection;
    hiredate_col date_collection;
    mgr_col number_collection;
    function my_function return myTableType
    is
    begin
    select empno, ename, hiredate, mgr
    bulk collect into empno_col, ename_col, hiredate_col, mgr_col
    from emp
    order by empno; -- Get some data
    g_data := myTableType(); -- Initialize
    for i in empno_col.first .. empno_col.last loop
    g_data.extend; -- Write something in the array
    g_data(i) := myRecordtype(empno_col(i), ename_col(i), hiredate_col(i), mgr_col(i));
    end loop;
    return g_data;
    end;
    end;
    -- Demonstration-View
    CREATE OR REPLACE VIEW scott.myview
    AS
    select a,b,c,d
    from table(cast(myPackage.my_function() as mytabletype));

  • Returning a table from a stored procedure

    hi, i need to return a table from a stored procedure and show it, and come to this, but a don't know hoy to run it, so i don't know if it is right, can anyone help me?
    uTable out objects_uptime%rowtype
    as
    begin
    select * into uTable from objects_uptime;
    end;

    well, i finally discovered how to do the trick
    this is the code for the function:
    CREATE OR REPLACE FUNCTION FN_GET_RECORDS RETURN UPTIME PIPELINED IS
    CURSOR cUptime is select * from objects_uptime;
    p refcur.refcur_t;
    temp p%ROWTYPE;
    temp2 OUPTIME := OUPTIME(null, null, null, null, null, null, null, null, null);
    BEGIN
    OPEN cUptime;
    LOOP
    FETCH cUptime into temp;
    temp2.OBJ_NAME := temp.OBJ_NAME;
    temp2.IP := temp.IP;
    temp2.STATUS := temp.STATUS;
    temp2.DOE := temp.DOE;
    temp2.ENABLED := temp.ENABLED;
    temp2.COMMENT00000 := temp.COMMENT00000;
    temp2.USERID := temp.USERID;
    temp2.OBJECTID := temp.OBJECTID;
    temp2.MNTID := temp.MNTID;
    pipe row(temp2);
    EXIT WHEN cUptime%NOTFOUND;
    END LOOP;
    RETURN;
    END FN_GET_RECORDS;
    and this for the auxiliar package, object and table:
    CREATE OR REPLACE PACKAGE REFCUR
    as
    TYPE refcur_t IS REF CURSOR RETURN objects_uptime%ROWTYPE;
    end REFCUR;
    CREATE OR REPLACE TYPE OUPTIME AS OBJECT ( "OBJ_NAME"
    VARCHAR2(255), "IP" VARCHAR2(20), "STATUS" VARCHAR2(10),
    "DOE" DATE, "ENABLED" NUMBER(10, 1), "COMMENT00000"
    VARCHAR2(1000), "USERID" VARCHAR2(50), "OBJECTID" NUMBER(10,
    1), "MNTID" NUMBER(10, 1) )
    CREATE TYPE TUPTIME AS
    TABLE OF OUPTIME
    i call it this way:
    select * from table(FN_GET_RECORDS ())
    if anyone knows how to do the same in a shorter manner, tell me please.
    thanks to everybody for the help.

  • OBIEE execute stored procedure to load tables before running report

    Hi..
    I want to execute a stored procedure to load database tables before running a report in OBIEE .
    I need to pass 2 parameters to the stored procedure which loads into tables.
    In the Connection Pool --> Connection Script Tab --> Execute before query, I wrote the below query using the repository variables VAR1 & VAR2 to execute the
    DECLARE VAR1 number; VAR2 number;
    BEGIN
    schema_name.package_name1.package_body('VALUE OF(VAR1)', 'VALUE OF(VAR2)'); COMMIT;
    schema_name.package_name2.package_body('VALUE OF(VAR1)', 'VALUE OF(VAR2)'); COMMIT;
    END;
    I am receiving the following error to declare the schema_name.package_name
    +++Administrator:2a0000:2a0004:----2010/06/21 14:29:00
    -------------------- Sending query to database named ACBS-OCC (id: <<49419>>):
    BEGIN schema_name.package_name1.package_body1('VALUE OF(VAR1', 'VALUE OF(VAR2'); COMMIT; schema_name.package_name2.package_body2('VALUE OF(VAR1)', 'VALUE OF(VAR2)'); COMMIT;END;
    +++Administrator:2a0000:2a0004:----2010/06/21 14:29:00
    -------------------- Query Status: Query Failed: [nQSError: 16001] ODBC error state: S1000 code: 6550 message: [Oracle][ODBC][Ora]ORA-06550: line 1, column 7:
    PLS-00201: identifier 'SCHEMA_NAME.PACKAGE_NAME1' must be declared
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored
    ORA-06550: line 1, column 93:
    PLS-00201: identifier 'SCHEMA_NAME.PACKAGE_NAME2' must be declared
    ORA-06550: line 1, column 93:
    PL/SQL: Statement ignored.
    [nQSError: 16015] SQL statement execution failed.
    Please suggest how to declare and execute the stored procedure.
    Thanks in advance.

    Hi,
    I know that any Function / Procedure needs to be called using a EVALUATE function in OBIEE.
    Thanks,
    Vijay

  • How to Store the result sets of a stored procedure that is returning more that one result set in two different table ?

    Hi Experts,
       I have on stored procedure which returns mote than one resultset i want that to store in two different temp table how can achieve this in SQL server.
     following is the stored procedure and table that i need to create.
    create procedure GetData as begin select * from Empselect * from Deptend 
    create table #tmp1 (Ddeptid int, deptname varchar(500),Location varchar(100))
    Insert into #tmp1 (Ddeptid , deptname ,Location )
    exec GetData
    create table #tmp (empid int , ename varchar(500),DeptId int , salary int)
    Insert into #tmp (empId,ename,deptId,salary)
    exec GetData
    Niraj Sevalkar

    You cant get two resultsets out of SP like this. The workaround is to merge and bring the resultsets.
    For this number of columns as well as corresponding datatypes have to be compatible. Also you will need one additional column which indicates resultset value. Then use this as filter to get your desired resultset out
    create procedure GetData as
    begin
    select 'resultset1' as Cat,*,.. N columns from Emp
    union all
    select 'resultset2' as Cat,*,.. N columns from Dept
    end
    create table #tmp1 (Ddeptid int, deptname varchar(500),Location varchar(100))
    Insert into #tmp1 (Ddeptid , deptname ,Location )
    Select column1,column2,column3
    from OPENROWSET('SQLOLEDB','Data Source=Server_name;Trusted_Connection=yes;
    Integrated Security=SSPI','Execute yourdb..GetData')
    WHERE Cat = 'resultset1'
    create table #tmp (empid int , ename varchar(500),DeptId int , salary int)
    Insert into #tmp (empId,ename,deptId,salary)
    Select column1,column2,column3, column4
    from OPENROWSET('SQLOLEDB','Data Source=Server_name;Trusted_Connection=yes;
    Integrated Security=SSPI','Execute yourdb..GetData')
    WHERE Cat = 'resultset2'
    also see
    http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/26/select-columns-from-exec-procedure-name-is-this-possible.aspx
    Another method is to populate table with relevant resultset within procedure itself and then select from the table directly outside.
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Problem with a stored procedure to search in a table

    h5.
    Hi,
    h5.
    As many people on the forum, I'm new on PL/SQL programming.
    h5.
    I'm trying to program a stored procedure that allow to search on my table fields with different parameters.
    h5.
    All this parameters doesn't need to be set, at least one.
    h5.
    I'm looking for the solution from 2 weeks ago.
    h5.
    I have looked everywhere
    h5.
    Could someone help me, please??
    h5.
    Here is my code:
    * HERE WE CREATE PACKAGE FOR THE CURSOR
    create or replace
    PACKAGE HOTEL_DEST_PKG
    IS
    /* Define the REF CURSOR type. */
    TYPE HOTEL_DEST_TYPE IS REF CURSOR;
    END HOTEL_DEST_PKG;
    * HERE WE CREATE OUR STORE PROCEDURE TO SEARCH
    create or replace
    PROCEDURE Search_Hotel (
    IdDocument IN number,
    MyFilter IN VARCHAR2,
    IdCountry IN number,
    DepartureDateFirst IN DATE,
    DepartureDateSecond IN DATE,
    HD_Cursor OUT HOTEL_DEST_PKG.HOTEL_DEST_TYPE)
    IS
    SQL_REQ VARCHAR2(5000);
    BEGIN
    /* all columns were entered */
    IF ((IdDocument > 0) OR
    ((MyFilter IS NOT NULL) AND (length(MyFilter) > 0)) OR
    (IdCountry > 0) OR
    (DepartureDateFirst IS NOT NULL) OR
    (DepartureDateSecond IS NOT NULL))
    THEN
    SQL_REQ := 'SELECT HOTEL_DESTINATION.*
    FROM HOTEL_DESTINATION
    WHERE 1=1';
    /* Search on the hotel id*/
    IF IdDocument > 0 THEN
    SQL_REQ := SQL_REQ || ' AND HOTEL_DESTINATION.HD_ID = :IdDocument';
    ELSE
    SQL_REQ := SQL_REQ || ' AND :IdDocument IS NULL';
    END IF;
         /*Search on different indexed fields*/
    IF MyFilter IS NOT NULL AND LENGTH(MyFilter)>0 THEN
    SQL_REQ := SQL_REQ || ' AND CONTAINS(HOTEL_DESTINATION.HD_FILTER, :MyFilter)';
    ELSE
    SQL_REQ := SQL_REQ || ' AND :MyFilter IS NULL';
    END IF;
    /* Search on the hotel country id*/
    IF IdCountry > 0 THEN
    SQL_REQ := SQL_REQ || ' AND HOTEL_DESTINATION.HD_CN_ID = :IdCountry';
    ELSE
    SQL_REQ := SQL_REQ || ' AND :IdCountry IS NULL';
    END IF;
    /* Search on the dates*/
    IF DepartureDateFirst IS NOT NULL THEN
    SQL_REQ := SQL_REQ || ' AND HOTEL_DESTINATION.HD_DEPARTURE_DATE >= :DepartureDateFirst';
    ELSE
    SQL_REQ := SQL_REQ || ' AND :DepartureDateFirst IS NULL' ;
    END IF;
    IF DepartureDateSecond IS NOT NULL THEN
    SQL_REQ := SQL_REQ || ' AND HOTEL_DESTINATION.HD_DEPARTURE_DATE <= :DepartureDateSecond';
    ELSE
    SQL_REQ := SQL_REQ || ' AND :DepartureDateSecond IS NULL';
    END IF;
    OPEN HD_CURSOR FOR SQL_REQ USING IdDocument,
    MyFilter,
    IdCountry,
    DepartureDateFirst,
    DepartureDateSecond;
    END IF;
    END;
    * HERE WE CALL AND EXECUTE OUR STORE PROCEDURE TO SEARCH
    * ON WORD
    set serveroutput on
    DECLARE
         IDDOCUMENT NUMBER;
         MYFILTER VARCHAR2(200);
         IDCOUNTRY NUMBER;
         DEPARTUREDATEFIRST IN DATE;
         DEPARTUREDATESECOND IN DATE;
         HD_CURSOR OUT HOTEL_DEST_PKG.HOTEL_DEST_TYPE);
    BEGIN
    IDDOCUMENT := 0;
    MYFILTER := 'test';
    IDCOUNTRY := 0;
    DEPARTUREDATEFIRST := NULL;
    DEPARTUREDATESECOND := NULL;
    SEARCH_HOTEL(
    IDDOCUMENT => IDDOCUMENT,
    MYFILTER => MYFILTER,
    IDCOUNTRY => IDCOUNTRY,
    DEPARTUREDATEFIRST => DEPARTUREDATEFIRST,
    DEPARTUREDATESECOND => DEPARTUREDATESECOND,
    HD_Cursor => HD_Cursor
    -- Modify the code to output the variable
    --DBMS_OUTPUT.PUT_LINE('HD_Cursor = ' || HD_Cursor);
    END;

    You need to grant right on the table to the owner of the stored procedure directly (not via Role).
    When there are only rights via role everything is fine in sql, but it will not work in stored procedures, functions or packages.
    So as Table-Owner do:
    grant select on <table> to <procedure, package or function-owner>;Edited by: hm on 22.10.2010 08:49

  • When inserting 2 column details in a single table using Stored Procedure.Only 2 Column details getting inserted.1column details are not getting into the table.Please see the below script and help me to change.

    Line 390 Under the (Insert into SALES_TRADEIN Table)
    I need to insert (TradeIn_1_VIN) Values If there is no values in (TradeIn_1_VIN) then i have to insert (TradeIn_2_VIN) values to the (SALES_TRADEIN) Table.
    After i run then below script only (TradeIn_2_VIN) values are get inserted in the table. (TradeIn_1_VIN) are not getting loaded in to the table.
    I think there is the problem from Line No (404 to 414) Please help me change those particular lines to insert (TradeIn_1_VIN) Values also.If there is no details then (TradeIn_2_VIN) need to be inserted.
    -- =============================================
    -- Stored Procedure for Flatfile_Sales
    -- =============================================
    USE [IconicMarketing]
    ---==========Sales_Cursor
    --USE [IconicMarketing]
    --GO
    DECLARE
    @FileType
    varchar(50),
    @ACDealerID
    varchar(50),
    @ClientDealerID
    varchar(50),
    @DMSType
    varchar(50),
    @DealNumber
    varchar(50),
    @CustomerNumber
    varchar(50),
    @CustomerName
    varchar(50),
    @CustomerFirstName
    varchar(50),
    @CustomerLastName
    varchar(50),
    @CustomerAddress
    varchar(50),
    @CustomerCity
    varchar(50),
    @CustomerState
    varchar(50),
    @CustomerZip
    varchar(50),
    @CustomerCounty
    varchar(50),
    @CustomerHomePhone
    varchar(50),
    @CustomerWorkPhone
    varchar(50),
    @CustomerCellPhone
    varchar(50),
    @CustomerPagerPhone
    varchar(50),
    @CustomerEmail
    varchar(50),
    @CustomerBirthDate
    varchar(50),
    @MailBlock
    varchar(50),
    @CoBuyerName
    varchar(50),
    @CoBuyerFirstName
    varchar(50),
    @CoBuyerLastName
    varchar(50),
    @CoBuyerAddress
    varchar(50),
    @CoBuyerCity
    varchar(50),
    @CoBuyerState
    varchar(50),
    @CoBuyerZip
    varchar(50),
    @CoBuyerCounty
    varchar(50),
    @CoBuyerHomePhone
    varchar(50),
    @CoBuyerWorkPhone
    varchar(50),
    @CoBuyerBirthDate
    varchar(50),
    @Salesman_1_Number
    varchar(50),
    @Salesman_1_Name
    varchar(50),
    @Salesman_2_Number
    varchar(50),
    @Salesman_2_Name
    varchar(50),
    @ClosingManagerName
    varchar(50),
    @ClosingManagerNumber
    varchar(50),
    @F_AND_I_ManagerNumber
    varchar(50),
    @F_AND_I_ManagerName
    varchar(50),
    @SalesManagerNumber
    varchar(50),
    @SalesManagerName
    varchar(50),
    @EntryDate
    varchar(50),
    @DealBookDate
    varchar(50),
    @VehicleYear
    varchar(50),
    @VehicleMake
    varchar(50),
    @VehicleModel
    varchar(50),
    @VehicleStockNumber
    varchar(50),
    @VehicleVIN
    varchar(50),
    @VehicleExteriorColor
    varchar(50),
    @VehicleInteriorColor
    varchar(50),
    @VehicleMileage
    varchar(50),
    @VehicleType
    varchar(50),
    @InServiceDate
    varchar(50),
    @HoldBackAmount
    varchar(50),
    @DealType
    varchar(50),
    @SaleType
    varchar(50),
    @BankCode
    varchar(50),
    @BankName
    varchar(50),
    @SalesmanCommission
    varchar(50),
    @GrossProfitSale
    varchar(50),
    @FinanceReserve
    varchar(50),
    @CreditLifePremium
    varchar(50),
    @CreditLifeCommision
    varchar(50),
    @TotalInsuranceReserve
    varchar(50),
    @BalloonAmount
    varchar(50),
    @CashPrice
    varchar(50),
    @AmountFinanced
    varchar(50),
    @TotalOfPayments
    varchar(50),
    @MSRP varchar(50),
    @DownPayment
    varchar(50),
    @SecurityDesposit
    varchar(50),
    @Rebate
    varchar(50),
    @Term varchar(50),
    @RetailPayment
    varchar(50),
    @PaymentType
    varchar(50),
    @RetailFirstPayDate
    varchar(50),
    @LeaseFirstPayDate
    varchar(50),
    @DayToFirstPayment
    varchar(50),
    @LeaseAnnualMiles
    varchar(50),
    @MileageRate
    varchar(50),
    @APRRate
    varchar(50),
    @ResidualAmount
    varchar(50),
    @LicenseFee
    varchar(50),
    @RegistrationFee
    varchar(50),
    @TotalTax
    varchar(50),
    @ExtendedWarrantyName
    varchar(50),
    @ExtendedWarrantyTerm
    varchar(50),
    @ExtendedWarrantyLimitMiles
    varchar(50),
    @ExtendedWarrantyDollar
    varchar(50),
    @ExtendedWarrantyProfit
    varchar(50),
    @FrontGross
    varchar(50),
    @BackGross
    varchar(50),
    @TradeIn_1_VIN
    varchar(50),
    @TradeIn_2_VIN
    varchar(50),
    @TradeIn_1_Make
    varchar(50),
    @TradeIn_2_Make
    varchar(50),
    @TradeIn_1_Model
    varchar(50),
    @TradeIn_2_Model
    varchar(50),
    @TradeIn_1_ExteriorColor
    varchar(50),
    @TradeIn_2_ExteriorColor
    varchar(50),
    @TradeIn_1_Year
    varchar(50),
    @TradeIn_2_Year
    varchar(50),
    @TradeIn_1_Mileage
    varchar(50),
    @TradeIn_2_Mileage
    varchar(50),
    @TradeIn_1_Gross
    varchar(50),
    @TradeIn_2_Gross
    varchar(50),
    @TradeIn_1_Payoff
    varchar(50),
    @TradeIn_2_Payoff
    varchar(50),
    @TradeIn_1_ACV
    varchar(50),
    @TradeIn_2_ACV
    varchar(50),
    @Fee_1_Name
    varchar(50),
    @Fee_1_Fee
    varchar(50),
    @Fee_1_Commission
    varchar(50),
    @Fee_2_Name
    varchar(50),
    @Fee_2_Fee
    varchar(50),
    @Fee_2_Commission
    varchar(50),
    @Fee_3_Name
    varchar(50),
    @Fee_3_Fee
    varchar(50),
    @Fee_3_Commission
    varchar(50),
    @Fee_4_Name
    varchar(50),
    @Fee_4_Fee
    varchar(50),
    @Fee_4_Commission
    varchar(50),
    @Fee_5_Name
    varchar(50),
    @Fee_5_Fee
    varchar(50),
    @Fee_5_Commission
    varchar(50),
    @Fee_6_Name
    varchar(50),
    @Fee_6_Fee
    varchar(50),
    @Fee_6_Commission
    varchar(50),
    @Fee_7_Name
    varchar(50),
    @Fee_7_Fee
    varchar(50),
    @Fee_7_Commission
    varchar(50),
    @Fee_8_Name
    varchar(50),
    @Fee_8_Fee
    varchar(50),
    @Fee_8_Commission
    varchar(50),
    @Fee_9_Name
    varchar(50),
    @Fee_9_Fee
    varchar(50),
    @Fee_9_Commission
    varchar(50),
    @Fee_10_Name
    varchar(50),
    @Fee_10_Fee
    varchar(50),
    @Fee_10_Commission
    varchar(50),
    @ContractDate
    varchar(50),
    @InsuranceName
    varchar(50),
    @InsuranceAgentName
    varchar(50),
    @InsuranceAddress
    varchar(50),
    @InsuranceCity
    varchar(50),
    @InsuranceState
    varchar(50),
    @InsuranceZip
    varchar(50),
    @InsurancePhone
    varchar(50),
    @InsurancePolicyNumber
    varchar(50),
    @InsuranceEffectiveDate
    varchar(50),
    @InsuranceExpirationDate
    varchar(50),
    @InsuranceCompensationDeduction
    varchar(50),
    @TradeIn_1_InteriorColor
    varchar(50),
    @TradeIn_2_InteriorColor
    varchar(50),
    @PhoneBlock
    varchar(50),
    @LicensePlateNumber
    varchar(50),
    @Cost varchar(50),
    @InvoiceAmount
    varchar(50),
    @FinanceCharge
    varchar(50),
    @TotalPickupPayment
    varchar(50),
    @TotalAccessories
    varchar(50),
    @TotalDriveOffAmount
    varchar(50),
    @EmailBlock
    varchar(50),
    @ModelDescriptionOfCarSold
    varchar(50),
    @VehicleClassification
    varchar(50),
    @ModelNumberOfCarSold
    varchar(50),
    @GAPPremium
    varchar(50),
    @LastInstallmentDate
    varchar(50),
    @CashDeposit
    varchar(50),
    @AHPremium
    varchar(50),
    @LeaseRate
    varchar(50),
    @DealerSelect
    varchar(50),
    @LeasePayment
    varchar(50),
    @LeaseNetCapCost
    varchar(50),
    @LeaseTotalCapReduction
    varchar(50),
    @DealStatus
    varchar(50),
    @CustomerSuffix
    varchar(50),
    @CustomerSalutation
    varchar(50),
    @CustomerAddress2
    varchar(50),
    @CustomerMiddleName
    varchar(50),
    @GlobalOptOut
    varchar(50),
    @LeaseTerm
    varchar(50),
    @ExtendedWarrantyFlag
    varchar(50),
    @Salesman_3_Number
    varchar(50),
    @Salesman_3_Name
    varchar(50),
    @Salesman_4_Number
    varchar(50),
    @Salesman_4_Name
    varchar(50),
    @Salesman_5_Number
    varchar(50),
    @Salesman_5_Name
    varchar(50),
    @Salesman_6_Number
    varchar(50),
    @Salesman_6_Name
    varchar(50),
    @APRRate2
    varchar(50),
    @APRRate3
    varchar(50),
    @APRRate4
    varchar(50),
    @Term2
    varchar(50),
    @SecurityDeposit2
    varchar(50),
    @DownPayment2
    varchar(50),
    @TotalOfPayments2
    varchar(50),
    @BasePayment
    varchar(50),
    @JournalSaleAmount
    varchar(50),
    @IndividualBusinessFlag
    varchar(50),
    @InventoryDate
    varchar(50),
    @StatusDate
    varchar(50),
    @ListPrice
    varchar(50),
    @NetTradeAmount
    varchar(50),
    @TrimLevel
    varchar(50),
    @SubTrimLevel
    varchar(50),
    @BodyDescription
    varchar(50),
    @BodyDoorCount
    varchar(50),
    @TransmissionDesc
    varchar(50),
    @EngineDesc
    varchar(50),
    @TypeCode
    varchar(50),
    @SLCT2
    varchar(50),
    @DealDateOffset
    varchar(50),
    @AccountingDate
    varchar(50),
    @CoBuyerCustNum
    varchar(50),
    @CoBuyerCell
    varchar(50),
    @CoBuyerEmail
    varchar(50),
    @CoBuyerSalutation
    varchar(50),
    @CoBuyerPhoneBlock
    varchar(50),
    @CoBuyerMailBlock
    varchar(50),
    @CoBuyerEmailBlock
    varchar(50),
    @RealBookDate
    varchar(50),
    @CoBuyerMiddleName
    varchar(50),
    @CoBuyerCountry
    varchar(50),
    @CoBuyerAddress2
    varchar(50),
    @CoBuyerOptOut
    varchar(50),
    @CoBuyerOccupation
    varchar(50),
    @CoBuyerEmployer
    varchar(50),
    @Country
    varchar(50),
    @Occupation
    varchar(50),
    @Employer
    varchar(50),
    @Salesman2Commission
    varchar(50),
    @BankAddress
    varchar(50),
    @BankCity
    varchar(50),
    @BankState
    varchar(50),
    @BankZip
    varchar(50),
    @LeaseEstimatedMiles
    varchar(50),
    @AFTReserve
    varchar(50),
    @CreditLifePrem
    varchar(50),
    @CreditLifeRes
    varchar(50),
    @AHRes
    varchar(50),
    @Language
    varchar(50),
    @BuyRate
    varchar(50),
    @DMVAmount
    varchar(50),
    @Weight
    varchar(50),
    @StateDMVTotFee
    varchar(50),
    @ROSNumber
    varchar(50),
    @Incentives
    varchar(50),
    @CASS_STD_LINE1
    varchar(50),
    @CASS_STD_LINE2
    varchar(50),
    @CASS_STD_CITY
    varchar(50),
    @CASS_STD_STATE
    varchar(50),
    @CASS_STD_ZIP
    varchar(50),
    @CASS_STD_ZIP4
    varchar(50),
    @CASS_STD_DPBC
    varchar(50),
    @CASS_STD_CHKDGT
    varchar(50),
    @CASS_STD_CART
    varchar(50),
    @CASS_STD_LOT
    varchar(50),
    @CASS_STD_LOTORD
    varchar(50),
    @CASS_STD_URB
    varchar(50),
    @CASS_STD_FIPS
    varchar(50),
    @CASS_STD_EWS
    varchar(50),
    @CASS_STD_LACS
    varchar(50),
    @CASS_STD_ZIPMOV
    varchar(50),
    @CASS_STD_Z4LOM
    varchar(50),
    @CASS_STD_NDIAPT
    varchar(50),
    @CASS_STD_NDIRR
    varchar(50),
    @CASS_STD_LACSRT
    varchar(50),
    @CASS_STD_ERROR_CD
    varchar(50),
    @NCOA_AC_ID
    varchar(50),
    @NCOA_COA_ADDSRC
    varchar(50),
    @NCOA_COA_MATCH
    varchar(50),
    @NCOA_COA_MOVTYP
    varchar(50),
    @NCOA_COA_DATE
    varchar(50),
    @NCOA_COA_DELCD
    varchar(50),
    @NCOA_COA_RTYPE
    varchar(50),
    @NCOA_COA_RTNCD
    varchar(50),
    @NCOA_COA_LINE1
    varchar(50),
    @NCOA_COA_LINE2
    varchar(50),
    @NCOA_COA_CITY
    varchar(50),
    @NCOA_COA_STATE
    varchar(50),
    @NCOA_COA_ZIP
    varchar(50),
    @NCOA_COA_ZIP4
    varchar(50),
    @NCOA_COA_DPBC
    varchar(50),
    @NCOA_COA_CHKDGT
    varchar(50),
    @NCOA_COA_CART
    varchar(50),
    @NCOA_COA_LOT
    varchar(50),
    @NCOA_COA_LOTORD
    varchar(50),
    @NCOA_COA_URB
    varchar(50),
    @NCOA_COA_Z4LOM
    varchar(50),
    @NCOA_COA_ACTION
    varchar(50),
    @NCOA_COA_QNAME
    varchar(50),
    @NCOA_DPV_AA
    varchar(50),
    @NCOA_DPV_A1
    varchar(50),
    @NCOA_DPV_BB
    varchar(50),
    @NCOA_DPV_CC
    varchar(50),
    @NCOA_DPV_M1
    varchar(50),
    @NCOA_DPV_M3
    varchar(50),
    @NCOA_DPV_N1
    varchar(50),
    @NCOA_DPV_P1
    varchar(50),
    @NCOA_DPV_P3
    varchar(50),
    @NCOA_DPV_RR
    varchar(50),
    @NCOA_DPV_R1
    varchar(50),
    @NCOA_DPV_STATUS
    varchar(50),
    @NCOA_DPV_F1
    varchar(50),
    @NCOA_DPV_G1
    varchar(50),
    @NCOA_DPV_U1
    varchar(50),
    @myerror
    varchar(500),
    @SalesID
    int,
    @errornumber int,
                @errorseverity varchar(500),
                @errorstate int,
                @errorprocedure varchar(500),
                @errorline varchar(50),
                @errormessage varchar(1000);
    DECLARE Sales_Cursor CURSOR FOR 
    SELECT * from FLATFILE_SALES;
    OPEN Sales_Cursor;
     :r C:\Clients\BlackBook\BlackBookMarketing\Bharath\LOG_SALES_INSERT.sql
    WHILE @@FETCH_STATUS = 0
    BEGIN
    PRINT @VehicleVIN    ;
    --===============================================================================
    -- ****************** insert into Sales Table ***********
    BEGIN TRY
        INSERT INTO Sales 
    IconicDealerID,
    DealNumber,
    CustomerNumber,
    DMSType,
    ContractDate
    VALUES (@ClientDealerID,@DealNumber,@CustomerNumber,@DMSType,@ContractDate);
    END TRY
    BEGIN CATCH
         SELECT
            @errornumber = ERROR_NUMBER()
            ,@errorseverity = ERROR_SEVERITY() 
            ,@errorstate = ERROR_STATE() 
            ,@errorprocedure = ERROR_PROCEDURE() 
            ,@errorline = ERROR_LINE()
            ,@errormessage = ERROR_MESSAGE();
           :r C:\Clients\BlackBook\BlackBookMarketing\Bharath\LOG_SALES_INSERT.sql
    @errornumber ,
                @errorseverity ,
                @errorstate,
                @errorprocedure,
                @errorline,
                @errormessage);
    END CATCH
    PRINT @errornumber;
    PRINT @errorseverity;
    PRINT @errorprocedure;
    PRINT @errorline;
    PRINT @errormessage;
    PRINT @errorstate;
    set @myerror = @@ERROR;
        -- This PRINT statement prints 'Error = 0' because
        -- @@ERROR is reset in the IF statement above.
        PRINT N'Error = ' + @myerror;
    set @SalesID = scope_identity();
    PRINT @SalesID;
    --================================================================================
    --Insert into SALES_TRADEIN Table
    BEGIN TRY
    INSERT INTO SALES_TRADEIN
    SalesID,
    TradeIn_VIN,
    TradeIn_Make,
    TradeIn_Model,
    TradeIn_ExteriorColor,
    TradeIn_Year,
    TradeIn_Mileage,
    TradeIn_Gross,
    TradeIn_Payoff,
    TradeIn_ACV,
    TradeIn_InteriorColor
    VALUES
    @SalesID,
    case when  @TradeIn_1_VIN is not null then @TradeIn_2_VIN end,
    case when  @TradeIn_1_Make is not null  then @TradeIn_2_Make end,
    case when  @TradeIn_1_Model is not null  then @TradeIn_2_Model end,
    case when  @TradeIn_1_ExteriorColor is not null  then @TradeIn_2_ExteriorColor end,
    case when @TradeIn_1_Year is not null  then @TradeIn_2_Year end,
    case when  @TradeIn_1_Mileage is not null  then @TradeIn_2_Mileage end,
    case when @TradeIn_1_Gross is not null  then @TradeIn_2_Gross end,
    case when @TradeIn_1_Payoff is not null  then @TradeIn_2_Payoff end,
    case when @TradeIn_1_ACV is not null  then @TradeIn_2_ACV end,
    case when  @TradeIn_1_InteriorColor is not null  then @TradeIn_2_InteriorColor end
    END TRY
    BEGIN CATCH
    SELECT
            @errornumber = ERROR_NUMBER()
            ,@errorseverity = ERROR_SEVERITY() 
            ,@errorstate = ERROR_STATE() 
            ,@errorprocedure = ERROR_PROCEDURE() 
            ,@errorline = ERROR_LINE()
            ,@errormessage = ERROR_MESSAGE();
          :r C:\Clients\BlackBook\BlackBookMarketing\Bharath\LOG_SALES_INSERT.sql
    END CATCH

    This is what I've understood from your question. You want to replace @TradeIn_2_VIN value if @TradeIn_1_VIN
    is NULL, else the value of @TradeIn_1_VIN.
    If this is the requirement then, your CASE statement is missing ELSE part. You can re-write this as below
    case when  @TradeIn_1_VIN is null then @TradeIn_2_VIN
    ELSE @TradeIn_1_VIN end,
    or simply you can replace the CASE statement with the below
    COALESCE function,
    COALESCE(@TradeIn_1_VIN, @TradeIn_2_VIN),
    Krishnakumar S

Maybe you are looking for