Variable database name in SQL Server query using Oracle database link

Hi All,
I have an ApEx 4.1 app running on 11g x64 (11.2.0.1) on Windows Server 2008 x64, and I have some data integration points with a SQL Server (2005 and 2008) that I need to establish. I have configured the database link with dg4odbc and it works beautifully... I can execute queries against the SQL Server database without any problems using the database link.
However, there is a scenario where the SQL Server database name is dynamic, and I need to generate it on the fly in a PL/SQL block, and then use that in a dynamic SQL query (all of this in ApEx). This is where I run into problems... when I am querying the default database based on the ODBC connection and I don't have to specify the database name, there is no issue. But when I need to access one of several other non-default databases, I keep receiving the "invalid table" error.
This runs fine:* (note that "fv" is the name of my database link)
v_query1 := 'select "ReleaseDate" from dbo.Schedules@fv where dbo.Schedules."SchedID" = :schedule';
EXECUTE IMMEDIATE v_query1 into rel_date using schedule;
I then take that rel_date variable, convert to a varchar2 (rel_date_char), and then use it as the database name in the next query...
This returns an error_ (Error ORA-00903: invalid table name)
v_query2 := 'select "PARTNO" from :rel_date_char.dbo.ProdDetails@fv where "SchedID" = :schedule and "UnitID" = :unit
and "MasterKey" = :master and "ParentKey" = :parent';
EXECUTE IMMEDIATE v_query2 into part_number using schedule, unit, master, parent;
I have also tried using all of the following to no avail:
'select "PARTNO" from ' || :rel_date_char || '.dbo.ProdDetails@fv where "SchedID"...
'select "PARTNO" from ' || rel_date_char || '.dbo.ProdDetails@fv where "SchedID"...
'select "PARTNO" from ' || @rel_date_char || '.dbo.ProdDetails@fv where "SchedID"...
'select "PARTNO" from @rel_date_char.dbo.ProdDetails@fv where "SchedID"...
Is there a way to do this in PL/SQL?
Thanks for any help!
-Ian C.
Edited by: 946532 on Jul 15, 2012 7:45 PM

Just did a test using passthrough:
SQL> set serveroutput on
SQL> declare
2 val varchar2(100);
3 c integer;
4 nr integer;
5 begin
6 c:= dbms_hs_passthrough.open_cursor@FREETDS_DG4ODBC_EMGTW_11_2_0_3;
7 dbms_hs_passthrough.parse@FREETDS_DG4ODBC_EMGTW_11_2_0_3 (c, 'select count(*) from EMP');
8 LOOP
9 nr:= DBMS_Hs_Passthrough.fetch_row@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c);
10 exit when nr=0;
11 dbms_hs_passthrough.get_value@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c,1,val);
12 dbms_output.put_line(val);
13 end loop;
14 dbms_hs_passthrough.close_cursor@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c);
15 end;
16 /
24576
PL/SQL procedure successfully completed.
SQL> declare
2 val varchar2(100);
3 c integer;
4 nr integer;
5 begin
6 c:= dbms_hs_passthrough.open_cursor@FREETDS_DG4ODBC_EMGTW_11_2_0_3;
7 dbms_hs_passthrough.parse@FREETDS_DG4ODBC_EMGTW_11_2_0_3 (c, 'select count(*) from dbo.EMP');
8 LOOP
9 nr:= DBMS_Hs_Passthrough.fetch_row@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c);
10 exit when nr=0;
11 dbms_hs_passthrough.get_value@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c,1,val);
12 dbms_output.put_line(val);
13 end loop;
14 dbms_hs_passthrough.close_cursor@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c);
15 end;
16 /
24576
PL/SQL procedure successfully completed.
So all 3 ways work for me.
Edited by: kgronau on Jul 23, 2012 10:08 AM
Now using variables to perform the select:
SQL> declare
2 val varchar2(100);
3 c integer;
4 nr integer;
5 tabname varchar2(20) :='EMP';
6 ownr varchar2(20) :='dbo';
7 dbname varchar2(20) :='gateway';
8 begin
9 c:= dbms_hs_passthrough.open_cursor@FREETDS_DG4ODBC_EMGTW_11_2_0_3;
10 dbms_hs_passthrough.parse@FREETDS_DG4ODBC_EMGTW_11_2_0_3 (c, 'SELECT count(*) FROM '||dbname||'.'|| ownr || '.'||tabname||'');
11 LOOP
12 nr:= DBMS_Hs_Passthrough.fetch_row@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c);
13 exit when nr=0;
14 dbms_hs_passthrough.get_value@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c,1,val);
15 dbms_output.put_line(val);
16 end loop;
17 dbms_hs_passthrough.close_cursor@FREETDS_DG4ODBC_EMGTW_11_2_0_3(c);
18 end;
19 /
24576
PL/SQL procedure successfully completed.
=> instead of executing the statement using "execute Immediate" we have to use PASTHROUGH package to pass the statement to the SQL Server.
Edited by: kgronau on Jul 23, 2012 10:10 AM

Similar Messages

  • Querying SQL server table from Oracle database 11g

    Hi all
    We are using oracle database 11g R2 on REL 5 and i have an SQL server database used by one of our application.
    In my Oracle database, i want to query one table in my SQL server database and to link it in a table in Oracle database to compare data.
    Is there a way like database link or something else to do this need>
    Please send a link or pots here step by step the way on how to accomplish my query.
    Thank you.

    Yes, you can set up your SQL Server as an external database link, using Oracle Heterogeneous Services with SQL Server as an ODBC connection (easier to do if your Oracle server is a Windows server, a little more tricky if your Oracle server is *nix based)
    Here's the documentation... for 11gR2
    http://www.oracle.com/pls/db112/search?remark=quick_search&word=heterogeneous+services

  • How to Migrate Stored procedure on Sql server 2008 to Oracle Database

    Guys, I need help vey badly as I am new in this field.
    Problem is that, I have to migrate stored procedure on Sql server 2008 to oracle Oracle database:
    Whole scenario--
    1. Sql Server 2008 application on Windows server (source machine)
    2. I have to migrate 70 Stored Procedure
    3. To Oracle Database on Linux machine (Target machine)
    Any method (no problem)
    Please, help me or give me any reference as I don't know which keyword is differ in both database.
    Thanks in advance

    Hi,
      You could the free Oracle SQL*Developer to do this.
    There is information and a download link here -
    Oracle SQL Developer</title><meta name="Title" content="Oracle SQL Developer"><meta n…
    and information on using it for migrations here -
    http://www.oracle.com/technetwork/database/migration/index-084442.html
    You could use it in 2 ways -
    1. Go through a migration but just pull the stored procedure code from the file created after you generate the SQL from the SQL*Server database
    2. Use the scratch editor accessed from -
    - Tools - Migration - Scratch Editor
    and paste the SQL*Server stored procedure code into the window and it will convert it to Oracle code. The tool is very good but may have problems if you have very complicated procedures that use SQL*Server specific utilities.
    Regards,
    Mike

  • Convert this sql server query to Oracle?

    I've some SQL Server queries that I need to make a copy for to run against Oracle databases. (10G), any help would be appreciated.
    SELECT COUNT(*) COUNT, CONVERT(DATETIME, CONVERT(VARCHAR(8), M.START_DATE, 1)) [DATE]
         FROM OPTC.ORD_M_ORDER M
         INNER JOIN OGEN.GEN_M_PATIENT_MAST P ON M.PAT_NUMBER = P.PAT_NUMBER
         INNER JOIN OPTC.ORD_C_ORDER_TYPE C ON C.ORDER_TYPE_KEY = M.ORDER_TYPE_KEY
         WHERE OGEN.DATEONLY(END_DATE)- OGEN.DATEONLY(START_DATE) < '1900/03/01'
         --AND OGEN.DATEONLY(CREATED_ON) = OGEN.DATEONLY(CURRENT_TIMESTAMP)
         AND ORDER_STATUS IN (4,7,8)
         AND M.FACILITY_KEY IN (SELECT VALUE FROM OGEN.COMMA_TO_TABLE(@FACILITYKEY))
         AND [START_DATE] BETWEEN @STARTDATE AND @ENDDATE
         AND CAST(M.ORDER_FLAGS AS BIGINT) & 64 <> 64 AND P.DISCHARGE_DATE IS NULL
         GROUP BY CONVERT(DATETIME, CONVERT(VARCHAR(8), [START_DATE], 1))

    Hi,
    You could use the 'Translation Scratch Editor' in SQL*Developer to translate SQL*Server statements to Oracle.
    SQL*Developer is free and can be downloaded from here -
    http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index.html
    Once installed start it up, then go to 'Tools' - 'Migration' - 'Translation Scratch Editor'
    I did run the statement you have posted here through it but it gave errors so looks like there may be a problem with the syntx in any case.
    Does this query run as it is in SQL*Server ?
    Regards,
    Mike
    Edited by: mkirtley on Jan 5, 2012 12:45 PM

  • How to migrate an existing Microsoft SSIS deployment if it is decided to replace SQL Server with an Oracle database?

    Hi Oracle Gurus!
    Currently, I am designing an ETL solution that transforms and loads a lot of data from flat files and sends it to an SQL Server 2008 R2 database for storage. However, at a future point of time, it may be decided to add or even replace SQL Server with an Oracle 11g database.
    Currently, I am writing script transforms in C# to dynamically generate SSIS packages to tansform and load the data into SQL Server. But considering that in future, an Oracle 11g or 12c database might be added to, or replace the SQL Server database, how do I make my script transforms (or whatever else I am developing currently for SQL Server) reusable to the extent possible?
    Or more precisely, what steps do I take, from an Oracle point of view, to ensure that any future migration of data to an Oracle database would be smooth to the extent possible?
    Looking up to my Oracle Gurus for enlightenment in this matter!
    Novice Kid

    When you're writing your on C# code to load data into the SQL Server you have to modify the routines so that they will work with Oracle.
    One approach is to use the extproc agent which would allow you to directly call external programs with all the logic in it to perform the load of your files and to put the data into the Oracle database. Another option would be to use utl_file package (or equivalents) which will allow you to open external files from your Oracle database and to directly read its content and then to pass it to the related tables.

  • Converting SQL Server Query to Oracle

    Hello TechFriends,
    Can any one of u please tell me equivalent of following SQL Server Query?
    The same query runs in Oracle but givesa same reocrds in different order!! I want such an equivalent oracle query that gives records in same order as Sql Server does.
    select
    ProfValue_ProfScaleFK ProfScale,
    ProfValue_Value ProfValue,
    isnull(ProfValueDesc.name, ProfValue_name) ProfName
    from
    ProfValueDesc inner join TBL_LMS_Lang
    on lang_fk = lang_pk and langid = 'en'
    right outer join ProfValue
    on ProfValue_ProfScaleFK = ProfScale_fk and ProfValue_Value = ProfValue_FK ;
    Regards & TIA.
    Anand.

    If you want a specific ordering why don't you add an ORDER BY clause?
    Donal

  • Siebel database migration from Sql Server 2005 to Oracle 11g RAC.

    Hi,
    We would like to migrate our Siebel database from Sql Server 2005 to Oracle 11g RAC. Can you suggest the best way to do that ?
    The current configurations are:
    Application : Siebel
    database : SQL Server 2005
    The hardware can be changed if needed. Can you please suggest the best approach ?
    Thanks,
    Naveen.
    Edited by: Naveen Kumar C on Mar 18, 2011 9:10 PM
    Edited by: Naveen Kumar C on Mar 18, 2011 11:44 PM

    Hi Naveen,
    We would like to migrate our Siebel database from Sql Server 2005 to Oracle 11g RAC. Can you suggest the best way to do that ?You will need much more which suggestions.
    You will need a Siebel professional which has done this type of migration. Migrating the Database is not a task so hard, after migrating your environment should be functional and healthy it is usually the task problematic.
    The hardware can be changed if needed. Can you please suggest the best approach ?We can recommend the minimum recommended, but this documentation already does, you need to do a analysis on your environment and ask a vendor Siebel tell the which is recommended for you and how to find the best "number" for you, in your case is very hard because everything changes.
    Check this Step-by-Step
    Migrating a Microsoft SQL Server Database to Oracle Database 11g
    http://st-curriculum.oracle.com/obe/db/hol08/sqldev_migration/mssqlserver/migrate_microsoft_sqlserver_otn.htm
    Regards,
    Levi Pereira

  • How to transfer database tables from sql server 2000 to oracle 10g

    Hi,
    I have a database and tables in sql server 2000. I have to transfer those data to Oracle 10g. I have installed Oracle warehouse Builder ETL Tool. Using this how can i transfer data. Any help is vary helpful for me.
    Thanks in advance.

    you have to do it using ODBC HS.
    1. Configure ODBC connection through gateway.
    2. Create a initxxx.ora file with HS config.
    restart gateway listener services
    3. on target o/s add entries to your tnsnames.ora
    4. On your target o/s create a db link
    restart listener on target
    cheeck this out.Non-Oracle connection through HS issue
    Edited by: Darthvader-647181 on Jan 29, 2009 2:02 AM

  • Converting MS SQL Server Query to Oracle Query

    Hi There,
    I've a strange problem. My project uses both MS SQL Server and Oracle server at run time. I've lot of queries which are written in MS SQL Style. Now, iam planning to write a helper class whic converts MS SQL Query to Oracle Query. Please Help me if any one has that kind of Helper with you.
    Thanks And Regards,
    Sasi Kanth

    That is why persistence applications like Hibernate or
    CMP get used for apps that will use more than one DB,
    but it takes upfront planning.
    If you have a set of automated unit tests that work
    with SQL Server, they will be a big help in getting
    your Oracle code up and running.Indeed - JUnit and Ant would be a big help here.
    It sounds like you have SQL in your JSPs, that will
    work against you as well if so. If you are using a
    DAO pattern, this will be much easier, as you can
    re-implement each DAO for Oracle.If you'd layered this app properly, you might just implement an OracleDAOFactory and be done with it. Interfaces and a DAO layer would go a long way.
    This is why layering is such a good idea. It isolates changes in a smaller subset of classes.
    But your problem sounds pretty big. It'd be daunting even if it were well designed.

  • Problem: Database Migraton from Sql Server 2005 to Oracle 10g

    Hi all
    i am trying to migrate sql server 2005 Database to Oracle 10g, Using Oracle Sql developer 3.0.04
    Whole Daabase including Schemas,user-passwordTables,Views,Indexes,Functions,Procedures etc. migrated sucesfuly
    But All Procedures are not correctly migrated
    many of procedures showing errors...
    how to migrate procedures correctly?
    SO.....
    How To Resolve thios issue?
    please help me out
    thanks
    Edited by: user10226917 on Jun 8, 2011 1:42 AM

    Hi,
    I have tested using the following -
    SQL*Developer Version 3.0.04 Build MAIN-04.34
    and did the following -
    Tools - Migration - Translation Scratch Editor
    - made sure I was using the 'Microsoft SQL Server T-SQL to PL/SQL ' option in the 'Translateor' tab.
    Then in the worksheet put in your entry -
    PRINT '@HID=' + CAST(@HID AS VARCHAR(100))
    This is then translated to the following -
    BEGIN
    DBMS_OUTPUT.PUT_LINE('@HID=' || CAST(v_HID AS VARCHAR2(100)));
    END;
    Is this what you did and the entry you typed into the scratch pad ?
    If not, can you give the full code that gave the error -
    DBMS_OUTPUT.PUT_LINE('@HierID=' || CAST(v_HierID AS VARCHAR2(100)));
    DBMS_OUTPUT.PUT_LINE('@NodeType=' || CAST(v_NodeType AS VARCHAR2(100)));
    DBMS_OUTPUT.PUT_LINE('@NodeTypeReqd=' || CAST(v_NodeTypeReqd AS VARCHAR2(100)));
    Error(36,64): PLS-00103: Encountered the symbol "(" when expecting one of the following: . ) @ % The symbol ")" was substituted for "(" to continue. gives error in compiling error
    Regards,
    Mike

  • Calling SQL Server Function from Oracle Database link

    Hi ,
    i have some data from a old SQL server i would i can access with a database link but i cant call function from my database link.
    i would like to do something like
    @SPAN_PROD = Database link
    _EnerttObtApReelBassSys is a Table function from my SQL server
    SELECT *
    FROM "_EnerttObtApReelBassSys('20120504',4,1)"@SPAN_PROD
    WHERE DateEffectiveDebut <= GetDate()
    AND DateEffectiveFin > GetDate()
    any help ?
    Thnx

    951879 wrote:
    I have a SP in SQL Server which will return a Result Set.
    My requirement is to call that procedure in ORACLE using DB Link and insert that resultset(Data) in the temp table.First you need to setup Oracle to SQL Server connectivity. To do that you can either use HS - heterogeneous connectivity which comes for free or use Oracle Transparent Gateway which is not free. HS uses ODBC, so if your Oracle database in not on windows, you'll have to get ODBC SQL Server driver for Unix/Linux (e.g. from EasySoft). Since SQL Server selecting from table function syntax is different from Oracle's you will have to, if you use HS, to use DBMS_HS_PASSTHROUGH package. I never worked with Oracle Transparent Gateway to SQL Server, so I don't know if and how it supports selecting from SQL Server table function.
    SY.

  • I will be working on a project to migrate from Sybase ASE (15.7) to SQL Server 2014 using Oracle golden gate........I am trying to find documentation which can guide me to use a separate box installation of OGG and how to configure it for Sybase and SQL s

    Hi,
    I need guide to install OGG on a separate box which will help me to migrate Sybase to SQL server.
    Viral Dave

    hi - just an fyi
    we have SQL Server 2012 and from my reads and understanding OGG supports SQL Server up to 2008. not to say it wouldn't work on higher versions if installed. in the link above check out the SQL Server installation chapter. before purchasing the license I would contact Oracle.
    maybe someone here has installed it on a higher version and got it to work. we are currently working on setting up OGG to work on Oracle to Oracle servers.

  • Translate this Sql Server Query into Oracle 9i ?

    UPDATE E
    SET lvl =M.LVL+1
    FROM Employees AS E JOIN inserted AS I
    ON I.empid = E.empid
    LEFT OUTER JOIN Employees AS M
    ON E.mgrid = M.empid

    There is no FROM clause in an ANSI SQL update statement. Folk here don't necessarily know SQL Server, but if you re-write your update statement as an ANSI one and it doesn't work we'll likely be able to help. See page 391 of this doc --
    ANSI SQL 92

  • Database Migration MS Sql Server 2008 to Oracle

    I have downloaded the latest updates for SQL Developer and am attempting to migrate my SQL Server 2008 DB to Oracle 11g. I get the following error when attempting to test my Sql Server connection:
    Status : Failure - Test failed: I/O Error: SSO Failed: Native SSPI library not loaded. Check the java.library.path system property.
    After attempting and following several posts, I have been unsuccessful in getting past this error - I have downloaded and placed in the specified directories the various .dll's suggested....
    Any help is of course appreciated.

    Boy do I look foolish. I told this guy that the owners of the application answer questions here... suerly he would get a reply.
    Such is the problem with "free" software. No one to blame when it doesn't work.

  • Reading BLOB from SQL Server 2005 using DB Link from Oracle 10g

    Hi All,
    I am trying to read a table's data from SQL Server 2005 using ODBC DB Link created in Oracle 10g (10.2.0.3/4) database. I am not using oracle gateway.
    I am able read all data except for the BLOB data from SQL server 2005. It gives error given below when I try to execute query SELECT * from T_TRANSACTION_DATA@sdeslink inside a stored procedure:
    ORA-22992: cannot use LOB locators selected from remote tables
    Kindly help how to read BLOB from SQL server 2005 inside Oracle 10g.
    Best Regards!
    Irfan

    Irfan,
    If you can read the blob data using 10.2 HSODBC then there should be no problem using the 11g gateway.
    I recommend you download the latest version which is 11.2.0.3 available from My oracle Support as -
    Patch 10404530: 11.2.0.3.0 PATCH SET FOR ORACLE DATABASE SERVER
    This is a full version and does not need a previous version to have bene installed. When installing it needs to be in a completlely separate ORACLE_HOME from the existing 11.2 install.
    For the Ora-22992 problem have a look at this note in My Oracle Support -
    Ora-22992 has a workaround in 10gR2 (Doc ID 436707.1)
    Regards,
    Mike

Maybe you are looking for

  • Is null validation not working correctly

    I have an application in which I have created a data entry page. We are using apex version 3.0.1.00.08. I have an item whose source is a database column which has a select list associated with it, as I want to show the user that they have not yet pic

  • Populating dynamic values in the combobox with XML form Builder.

    I am trying to  populate dynamic value in the combobox with xml form builder. I  see the document saying create property group and document property Id with respecitive values.  I am able to create the property group with system admin -> System confi

  • Speed adjustment/audio?

    This should be another easy one. Whenever I select a clip to edit and change the speed from anything other than 100% (normal) the speed changes but the audio goes away. I want to speed up or slow motion the clip but I want to keep the audio as well.

  • Stickies that sync to my iPhone?

    i am trying to finalize my "notes organization". I've been through Omni Focus and Things and I am simply using STICKIES now. Is there a way to get these to sync to my iPhone? anyone have software suggestions that will do the same without all the othe

  • Load Balance Configuration for OCS 10g

    Hi guys, A few question on the load balancer requirement and setup for a OCS 10g cluster. 1. Can Pound be used as the load balancer for the OCS cluster. (pound is an application for load balancer and reverse proxy). 2. Basically the load balancer wil