Oracle TVF Sample

In some enterprise scenarios, it is useful to integrate data from other database management systems with data that is stored in Microsoft SQL Server. This sample demonstrates how to invoke the managed code interface to Oracle to expose the results of any Oracle query as a table-valued function in SQL Server. This approach makes it easy to join data from both Oracle and SQL Server in a relatively simple SELECT statement that you run on SQL Server.

This sample contains the GetDataFromOracle table-valued function. This function uses the managed Oracle provider to run arbitrary Oracle queries against an Oracle database and provide the results in table form.

Install directory: drive:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\OracleTVF\

Scenario

Jane is a developer for Adventure Works Cycles. She has to integrate data from an Oracle database with data that is stored in a database.

Languages

Transact-SQL, Visual C# and Visual Basic.

Features

The Oracle TVF sample uses the following features of Microsoft SQL Server.

Application Area Features

Overall

Common language runtime, Oracle managed provider, Transact-SQL

Prerequisites

Before running this sample, make sure that the following software is installed:

  • Microsoft SQL Server 2005 or Microsoft SQL Server 2005 Express Edition (SQL Server Express). You can obtain SQL Server Express free of charge from the SQL Server 2005 Express Edition Documentation and Samples Web site.
  • The SQL Server 2005 Database Engine samples. These samples are included with SQL Server 2005. You can download the latest version of the samples at the SQL Server Developer Web site.
  • .NET Framework SDK 2.0 or Microsoft Visual Studio 2005. You can obtain the .NET Framework SDK free of charge. For more information, see Installing the .NET Framework SDK.
  • A server that is running Oracle 10g. If you have an earlier version of Oracle installed, you will have to modify the table and columns that are accessed to match an existing table on your older server. This sample might not work on x64-based hardware that is using Oracle versions earlier than to 10g. If you want to use this sample on Windows Vista, contact your Oracle customer service representative for information about availability of Oracle 10g on Windows Vista.

Building the Sample

If you have not already created the strong-name key file UnsafeSampleKey.snk, generate the key file by using the following procedure.

To generate a strong name key file

  1. Open a Microsoft Visual Studio 2005 command prompt. Click Start, point to All Programs, point to Microsoft Visual Studio 2005, point to Visual Studio Tools, and then click Visual Studio 2005 Command Prompt.

    —Or—

    Open a Microsoft .NET Framework command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

  2. At the command prompt, use the change directory (CD) command to change the current folder of the Command Prompt window to the Samples folder.

    Note

    To determine the folder where the samples are located, click Start, point to All Programs, point to Microsoft SQL Server 2005, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\90\Samples.

  3. At the command prompt, run the following command to generate the key file:

    sn -k UnsafeSampleKey.snk

    Important

    For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" at the Microsoft .NET Development Center on MSDN.

To build the Oracle TVF sample

  1. Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution, or by using Microsoft MSBuild, which is included in the .NET Framework SDK 2.0. Run commands similar to the following at a .NET Framework command prompt:

    msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\OracleTVF.sln

  2. Make sure that the AdventureWorks database is installed.

  3. If you did not install the SQL Server engine samples in the default location, modify the path in the CREATE ASSEMBLY part of the script in Scripts\InstallCS.sql to refer to the location where the samples were installed.

  4. If you are not an administrator for the SQL Server instance that you are using, you must have an administrator grant you CreateAssembly permissions to complete the installation.

  5. Open the scripts\installCS.sql or scripts\installVB.sql file, depending on whether you compiled the Visual C# project or the Visual Basic project, in Microsoft SQL Server Management Studio. Run the script that is contained in the file, or run a command similar to the following in a Command Prompt window:

    sqlcmd -E -I -i Scripts\InstallCS.sql

Running the Sample

To run the Oracle TVF sample

  1. Open the Scripts\Test.sql file in Microsoft Management Studio or a text editor such as Notepad. Modify the script by replacing <server name>, <user name>, and <password> with the name of the Oracle server that you use and the credentials that you use on that server. This sample is designed to run with the sample database distributed by Oracle and by default associated with the user scott although your database administrator may require different credentials. Run the script in Management Studio, or save the script from Notepad. Then, run a command similar to the following in a Command Prompt window:

    sqlcmd -E -I -i Scripts\Test.sql

Removing the Sample

To remove the Oracle TVF sample

  1. Open the scripts\cleanup.sql file in Management Studio. Run the script that is contained in the file, or run the following command in a Command Prompt window:

    sqlcmd -E -I -i Scripts\cleanup.sql

Comments

The common language runtime for SQL Server 2005 or SQL Server Express must be enabled for this sample to work correctly.

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples. Sample applications and assemblies should not be connected to or used with your production SQL Server database or your report server without the permission of the system administrator.