migrating sybase apis to sql server

Upload: amulya-krishna

Post on 03-Jun-2018

255 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    1/30

    Guide to Migrating Sybase Application Interfaces to SQL

    Server 2008

    SQL Server Technical Article

    Writers:Yuri Rusakov (DB Best Technologies), Alexander Pavlov (DB Best Technologies), Yuri

    Kovtun (DB Best Technologies)

    Technical Reviewer:Dmitry Balin (DB Best Technologies)

    Published:

    Applies to:SQL Server 2008

    Summary:This white paper describes methods of migrating applications with Sybase interfacesto SQL Server. We cover both Open Standard APIs (ODBC, JDBC, OLE DB, ADO.Net) and

    proprietary APIs (CT-Library, DB-Library).

    Created by: DB Best Technologies LLC

    P.O. Box 7461, Bellevue, WA 98008

    Tel.: (408) 202-4567

    E-mail: [email protected]

    Web: www.dbbest.com

    mailto:[email protected]://d/WP-JOEL/OUT/www.dbbest.comhttp://d/WP-JOEL/OUT/www.dbbest.comhttp://d/WP-JOEL/OUT/www.dbbest.commailto:[email protected]
  • 8/12/2019 Migrating Sybase aPis to sQl Server

    2/30

    2

    CopyrightThis is a preliminary document and may be changed substantially prior to final commercial

    release of the software described herein.

    The information contained in this document represents the current view of Microsoft Corporation

    on the issues discussed as of the date of publication. Because Microsoft must respond tochanging market conditions, it should not be interpreted to be a commitment on the part of

    Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the

    date of publication.

    This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES,

    EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.

    Complying with all applicable copyright laws is the responsibility of the user. Without limiting the

    rights under copyright, no part of this document may be reproduced, stored in or introduced into

    a retrieval system, or transmitted in any form or by any means (electronic, mechanical,

    photocopying, recording, or otherwise), or for any purpose, without the express writtenpermission of Microsoft Corporation.

    Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual

    property rights covering subject matter in this document. Except as expressly provided in any

    written license agreement from Microsoft, the furnishing of this document does not give you any

    license to these patents, trademarks, copyrights, or other intellectual property.

    Unless otherwise noted, the example companies, organizations, products, domain names, e-

    mail addresses, logos, people, places and events depicted herein are fictitious, and no

    association with any real company, organization, product, domain name, email address, logo,

    person, place or event is intended or should be inferred.

    2010 Microsoft Corporation. All rights reserved.

    Microsoft and SQL Server are registered trademarks of Microsoft Corporation in the United

    States and other countries.

    The names of actual companies and products mentioned herein may be the trademarks of their

    respective owners.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    3/30

    3

    Contents

    Copyright ................................................................................................................................... 2

    Introduction ................................................................................................................................ 4

    Conversion of Open Standard Application Interfaces ................................................................. 5

    Overview ................................................................................................................................ 5

    JDBC ...................................................................................................................................... 5

    ODBC ..................................................................................................................................... 5

    OLE DB .................................................................................................................................. 6

    ADO.NET ............................................................................................................................... 8

    Driver versions ....................................................................................................................... 9

    Open Client Libraries to ODBC Migration ..................................................................................11

    Overview ...............................................................................................................................11

    Main Migration Steps .............................................................................................................11

    CT-Library Schema migration ................................................................................................12

    Header files mapping .........................................................................................................12

    Routine mapping ................................................................................................................12

    Type and structure mapping...............................................................................................17

    DB-Library Schema migration ................................................................................................20

    Overview ............................................................................................................................20

    Header files mapping .........................................................................................................20

    Routine mapping ................................................................................................................20

    Type and structure mapping...............................................................................................25

    Other Types of Migration ...........................................................................................................27

    Using FreeTDS tool to migrate CT Library and DB Library applications to SQL Server .........27

    Migration of Open Client Embedded SQL ..............................................................................27

    Migration of ASE Job Scheduler objects ................................................................................28

    Conversion of Transact-SQL statements which are embedded in applications ......................28

    Conclusion ................................................................................................................................30

    About DB Best Technologies .................................................................................................30

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    4/30

    4

    Introduction

    DB Best Technologies, Inc. created several white papers which cover migration of databases,

    such as Oracle or Sybase ASE, to SQL Server 2008. However, in most of the cases there is

    also a necessity to change database access code in an application connecting to the migrateddatabase.

    The task is easiest when the application uses a database interface designed to serve many

    database servers. In this white paper we discuss Open Database Connectivity (ODBC), Java

    Database Connectivity (JDBC), Object Linking and Embedding Database (OLE DB) and ActiveX

    Data Objects for .NET (ADO.NET). Migration of such interfaces can be as simple as changing

    the database driver and/or connection strings.

    More complicated are the cases when we use proprietary interfaces of one database vendor,

    which do not provide connectivity to SQL Server 2008. Examples are Sybase ASE CT-Library

    and DB-Library. In this paper we describe how to change the application code to use SQLServer compatible ODBC interface.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    5/30

    5

    Conversion of Open Standard Application Interfaces

    Overview

    In this section we discuss methods of redirecting multi-database interfaces of Sybase Adaptive

    Server Enterprise (ASE) applications to SQL Server 2008.

    Note that the application code may contain pieces of SQL strings, which are sent to the

    database and which may not be compatible with SQL Server after the interface was redirected.

    In such cases, we should convert the embedded SQL according to the rules of SQL conversion.

    JDBC

    To convert a JDBC application, we need to change name of loaded JDBC driver class and

    modify its URL accordingly.

    Examples:

    JDBC connection to Sybase ASE:

    Class.forName(com.sybase.jdbc.SybDrive);

    String url = "jdbc:sybase:Tds:sybase_server_name:5000/test_base;

    Connection dbc = DriverManager.getConnection(url, login, password);

    See below the changes necessary to make this code connect to SQL Server. The class

    name of SQL Server driver should be specified as a parameter of Class.forName() method,

    and getConnection method of the loaded class should follow the URL syntax for SQL

    Server.

    Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);

    String url = jdbc:sqlserver://sql_server_name:1433;

    Connection dbc = DriverManager.getConnection(url, login, password);

    ODBC

    ODBC is a universal interface, which means we can change the servers just by specifying

    another Data Source Name. See below a typical example of switching the database in ODBC

    connect code.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    6/30

    6

    ODBC connection to Sybase:

    SQLRETURN retcode;

    SQLHENV env;

    SQLHDBC dbc;retcode = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env );retcode = SQLSetEnvAttr( env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,

    0);retcode = SQLAllocHandle( SQL_HANDLE_DBC, env, &dbc );retcode = SQLConnect(dbc,

    (SQLCHAR*) "sybase_DataSource_name", SQL_NTS,(SQLCHAR*) "sybase_login", SQL_NTS,(SQLCHAR*) "sybase_password", SQL_NTS);

    ODBC connection to SQL Server:

    SQLRETURN retcode;SQLHENV env;

    SQLHDBC dbc;

    retcode = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env );retcode = SQLSetEnvAttr( env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,

    0);retcode = SQLAllocHandle( SQL_HANDLE_DBC, env, &dbc );retcode = SQLConnect(dbc,

    (SQLCHAR*) "mssql_DataSource_name", SQL_NTS,(SQLCHAR*) "mssql_login", SQL_NTS,(SQLCHAR*) "mssql_password", SQL_NTS);

    OLE DB

    To establish a connection with a data source we need to create an instance of OLE DB

    provider, which exposes interfaces of the data source, and then to perform initialization of this

    instance. To do that, we should use function CoCreateInstance with OLE DB provider class ID

    as the first parameter. Next, we initialize the data source using array of structures DBPROP. In

    the simplest case, we need to specify four properties: prompt level, DSN name, user name and

    password.

    The difference between OLE DB connections for different databases is in OLE DB providerobject and object initialization parameters.

    Connecting to Sybase ASE with OLE DB:

    #include "msdasql.h"

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    7/30

    7

    IDBInitialize* pIDBInitialize = NULL;HRESULT hr;hr = CoCreateInstance(CLSID_MSDASQL,

    NULL,CLSCTX_INPROC_SERVER,

    IID_IDBInitialize,(void**)&pIDBInitialize);

    InitProperties[0].dwPropertyID = DBPROP_INIT_PROMPT;InitProperties[0].vValue.vt = VT_I2;InitProperties[0].vValue.iVal = DBPROMPT_NOPROMPT;InitProperties[1].dwPropertyID = DBPROP_INIT_DATASOURCE;InitProperties[1].vValue.vt = VT_BSTR;InitProperties[1].vValue.bstrVal =SysAllocString(OLESTR("sybase_DataSource_name"));InitProperties[2].dwPropertyID = DBPROP_AUTH_USERID;InitProperties[2].vValue.vt = VT_BSTR;

    InitProperties[2].vValue.bstrVal = SysAllocString(OLESTR("sybase_login"));InitProperties[3].dwPropertyID = DBPROP_AUTH_PASSWORD;InitProperties[3].vValue.vt = VT_BSTR;InitProperties[3].vValue.bstrVal = SysAllocString(OLESTR("sybase_password"));

    Connecting to SQL Server using OLE DB:

    #include "msdasql.h"

    IDBInitialize* pIDBInitialize = NULL;HRESULT hr;hr = CoCreateInstance(CLSID_MSDASQL,

    NULL,CLSCTX_INPROC_SERVER,IID_IDBInitialize,(void**)&pIDBInitialize);

    InitProperties[0].dwPropertyID = DBPROP_INIT_PROMPT;InitProperties[0].vValue.vt = VT_I2;InitProperties[0].vValue.iVal = DBPROMPT_NOPROMPT;InitProperties[1].dwPropertyID = DBPROP_INIT_DATASOURCE;

    InitProperties[1].vValue.vt = VT_BSTR;InitProperties[1].vValue.bstrVal =SysAllocString(OLESTR("mssql_DataSource_name"));InitProperties[2].dwPropertyID = DBPROP_AUTH_USERID;InitProperties[2].vValue.vt = VT_BSTR;InitProperties[2].vValue.bstrVal =SysAllocString(OLESTR("mssql_login"));InitProperties[3].dwPropertyID = DBPROP_AUTH_PASSWORD;InitProperties[3].vValue.vt = VT_BSTR;

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    8/30

    8

    InitProperties[3].vValue.bstrVal =SysAllocString(OLESTR("mssql_password"));

    When using ATL library, we should choose class CDataSource for connection to a database.

    Databases differ by parameters of Open() method.

    CDataSource dsDSN;HRESULT hr;

    // Connecting to Sybase ASE with ATL OLE DB:

    hr = dsDSN.Open(_T("MSDASQL"), sybase_DataSource_name,"sybase_login", "sybase_password");

    // Connecting to SQL Server with ATL OLE DB:

    hr = dsDSN.Open(_T("MSDASQL"), mssql_DataSource_name,"mssql_login", "mssql_password");

    ADO.NET

    ADO.NET architecture uses data providers to access a database. There are various

    implementations of the .NET interfaces for different DBMSes. For example, connections to SQL

    Server are handled by special class SqlConnection. Each provider includes its own set of .NET

    framework classes in a separate namespace. When performing migration, we should replace

    calls to objects of one namespace to the calls of SQL Server provider. The only exception isODBC and OLE DB providers, which by definition are compatible with multiple databases.

    When the original provider uses System.Data.Odbc, the Sybase to SQL Server migration may

    be as simple as replacement of the connection string.

    An example of Sybase connection:

    OdbcConnection connection = new OdbcConnection("Driver={SYBASE ASE ODBCDriver};Srvr=myServerAddress;Uid=myUsername;Pwd=myPassword;");

    connection.Open();

    Equivalent SQL Server connection:

    OdbcConnection connection = new OdbcConnection(

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    9/30

    9

    "Driver={SQLServer};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;");connection.Open();

    Similarly, we replace the connection string in case of ADO.NET OLE DB provider.

    Sybase OLE DB connection:

    OleDbConnection connection = new OleDbConnection("Provider=Sybase.ASEOLEDBProvider;Srvr=myASEserver,5000;Catalog=myDataBase;User Id=myUsername;Password=myPassword;");connection.Open();

    SQL Server connection:

    OleDbConnection connection = new OleDbConnection("Provider=SQLOLEDB;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;");connection.Open();

    When the source code uses Oracle specific provider defined in System.Data.AseClient, we will

    need to replace the class names with their counterparts in System.Data.SqlClient.

    Sybase connection:

    AseConnection connection = newAseConnection (connectionString);connection.Open();

    SQL Server connection:

    SqlConnection connection = newSqlConnection(connectionString);connection.Open();

    Driver versions

    We suggest to use this driver in case of JDBC conversion:

    http://www.microsoft.com/downloads/details.aspx?familyid=99B21B65-E98F-4A61-B811-

    19912601FDC9&displaylang=en

    You can take Microsoft SQL Server OLE DB Provider and ODBC driver from Microsoft Data

    Access Components (MDAC) :

    http://www.microsoft.com/downloads/details.aspx?familyid=99B21B65-E98F-4A61-B811-19912601FDC9&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?familyid=99B21B65-E98F-4A61-B811-19912601FDC9&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?familyid=99B21B65-E98F-4A61-B811-19912601FDC9&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?familyid=99B21B65-E98F-4A61-B811-19912601FDC9&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?familyid=99B21B65-E98F-4A61-B811-19912601FDC9&displaylang=en
  • 8/12/2019 Migrating Sybase aPis to sQl Server

    10/30

    10

    http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=6c050fe3-c795-

    4b7d-b037-185d0506396c

    ADO.NET Data Provider for SQL Server is shipped as a part of MDAC or as a part of .NET

    Framework.

    .

    http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=6c050fe3-c795-4b7d-b037-185d0506396chttp://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=6c050fe3-c795-4b7d-b037-185d0506396chttp://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=6c050fe3-c795-4b7d-b037-185d0506396chttp://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=6c050fe3-c795-4b7d-b037-185d0506396chttp://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=6c050fe3-c795-4b7d-b037-185d0506396c
  • 8/12/2019 Migrating Sybase aPis to sQl Server

    11/30

    11

    Open Client Libraries to ODBC Migration

    Overview

    Following are the basic, high-level steps for migrating a Sybase Open Client Libraries

    interfaces to SQL Server ODBC interface and what you must know about converting

    interface routines.

    Main Migration Steps

    From the point of view of the client program both Sybase Open Client and ODBC represent a

    set of the C modules connected to program, containing the description of necessary types

    and functions for use of opportunities of the corresponding interface.

    Base functionality of libraries is identical and realizes necessary commands for performanceof operations of initialization of the driver, connection to a database server, execute SQL

    statement, fetching of results and closings of connection.

    Converting a Sybase Open Client Libraries program to its SQL Server ODBC equivalent

    generally involves the following steps:

    1. Replace Open Client Libraries header file names with the ODBC header file names.

    2. Plan the code conversion. Client application code can be split roughly into the

    following categories:

    Initialization and cleanup code;

    Code that opens a connection;

    Error and message handlers; Code that sends commands;

    Code that processes results;

    Finalization code;

    3. Perform the conversion:

    Replace or remove Open Client Libraries declarations, as appropriate.

    Replace Open Client Libraries function calls with their ODBC equivalents, changing

    program logic as necessary.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    12/30

    12

    CT-Library Schema migration

    This section explains mappings and differences between CT-Library and ODBC modules,

    commands and types.

    Header files mapping

    The following table displays the list of required header C files for the use CT-Library and

    ODBC API.

    CT-Library ODBC

    #include #include

    #include

    Routine mapping

    The following table displays the commands, used in typical execution steps of CT-Library

    and ODBC applications, and their correspondence.

    CT-Library ODBC

    STEP 1: Set Up The Client-Library

    Programming Environment

    STEP 1: Connect To The Data Source

    cs_ctx_alloc

    Allocate a context structure.

    cs_configSet any CS-Library properties for the

    context.

    ct_init

    Initialize Client-Library.

    SQLAllocHandle(ENV)

    Load the Driver Manager and allocate the

    environment handle.

    Driver Manager allocates a structure in

    which to store information about the

    environment, and returns the environment

    handle.

    ct_config

    Set Client-Library properties for the

    context.

    SQLSetEnvAttr

    Sets attributes that govern aspects of

    environments.

    STEP 2: Define Error Handling

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    13/30

    13

    cs_config(CS_MESSAGE_CB)

    Install a CS-Library error callback.

    Replace with SQLGetDiagFieldand

    SQLGetDiagRecfunction calls in the

    return code handlers.

    SQLGetDiagField

    Returns the current value of a field of a

    record of the diagnostic data structure(associated with a specified handle) that

    contains error, warning, and status

    information.

    SQLGetDiagRec

    Returns the current values of multiple

    fields of a diagnostic record that contains

    error, warning, and status information.

    Unlike SQLGetDiagField, which returns one

    diagnostic field per call, SQLGetDiagRec

    returns several commonly used fields of a

    diagnostic record, including the SQLSTATE,the native error code, and the diagnostic

    message text.

    ct_callback

    Install a client or server message callback.

    Ct_callback installs Client-Library callback

    routines, which are application

    routines that Client-Library calls

    automatically when a triggering event of

    the appropriate type occurs.

    STEP 3: Connect To A Server

    ct_con_alloc

    Allocate a connection structure.

    SQLAllocHandle(DBC)

    Allocate a connection handle.

    Driver Manager allocates a structure in

    which to store information about the

    connection and returns the connection

    handle.

    ct_con_props

    Set any properties in the connection

    structure.

    SQLSetConnectAttr

    Sets attributes that govern aspects of

    connections.

    Some connection attributes must be set

    before the application attempts to connect,

    others can be set after the connection has

    completed.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    14/30

    14

    ct_connect

    Open a connection to a server.

    SQLConnector SQLDriverConnector

    SQLBrowseConnect

    SQLConnect establishes connections to a

    driver and a data source.

    ct_options

    Set any server options for this connection.

    STEP 4: Send A Language Command

    To The Server

    STEP 2: Initialize The Application

    ct_cmd_alloc

    Allocate a command structure.

    SQLAllocHandle(STMT)

    Driver Manager allocates a structure in

    which to store information about the

    statement and calls SQLAllocHandle in the

    driver with the SQL_HANDLE_STMT option.

    The driver allocates its own structure in

    which to store information about the

    statement and returns the driver

    statement handle to the Driver Manager.

    The Driver Manager returns the Driver

    Manager statement handle.

    ct_cmd_props

    Set, retrieve, or clear command structure

    properties.

    SQLSetStmtAttr

    Sets attributes related to a statement.

    STEP 3: Build And Execute An SQL

    Statement

    ct_command

    Define a command.

    SQLPrepare

    Prepares an SQL string for execution.

    ct_paramor ct_setparam

    Define a command parameter.

    SQLBindParameter

    Binds a buffer to a parameter marker in an

    SQL statement.

    ct_send

    Sends the command text to the server,

    which parses, compiles, and executes it.

    SQLExecute

    Executes a prepared statement, using the

    current values of the parameter marker

    variables.

    STEP 5: Process The Results Of The STEP 4: Fetch The Results; Fetch The

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    15/30

    15

    Command Row Count

    ct_results

    Set up result data to be processed.

    Defines type of command result:

    Values that indicate command status;

    Values that indicate fetchable results;

    Values that indicate information is

    available.

    ct_res_info

    Retrieve current result set or command

    information.

    The possible returned information:

    The number of the command that

    generated the current result set; The number of compute clauses in the

    current command;

    The number of items in the current

    result set;

    The number of columns specified in the

    order-by clause of the current command;

    The number of rows affected by the

    current command;

    and others.

    SQLNumResultCols

    Returns the number of columns in a result

    set.

    If this is 0, the statement did not create a

    result set, if it is any other number, the

    statement did create a result set.

    SQLRowCount

    Returns the number of rows affected by an

    UPDATE, INSERT, or DELETE statement.

    If a batch of SQL statements is executed,

    the count of affected rows might be a total

    count for all statements in the batch or

    individual counts for each statement in the

    batch.

    ct_describe

    Return a description of result data.

    An application can use ct_describe to

    retrieve a description of a regular result

    column, a return parameter, a stored

    procedure return status number, or a

    compute column.

    An application can call ct_res_info to find

    out how many result items are present in

    the current result set.

    An application generally needs to callct_describe to describe a result data item

    before it binds the result item to a

    program variable using ct_bind.

    SQLDescribeCol

    Returns the result descriptor column

    name, type, column size, decimal digits,

    and nullability for one column in the

    result set.

    ct_bind

    Bind server results to program variables

    SQLBindCol

    Binds application data buffers to columns

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    16/30

    16

    When the application calls ct_fetch to

    fetch the result data, it is copied into this

    variables.

    in the result set.

    ct_fetch

    Fetch result data.

    SQLFetch

    Fetches the next rowset of data from the

    result set and returns data for all bound

    columns.

    SQLGetData

    Retrieves data for a single column in the

    result set. It can be called multiple times

    to retrieve variable-length data in parts.

    The application now calls SQLFetch to

    retrieve the first row of data and place the

    data from that row in the variables bound

    with SQLBindCol. If there is any long data

    in the row, it then calls SQLGetData to

    retrieve that data. The application

    continues to call SQLFetch and

    SQLGetData to retrieve additional data.

    After it has finished fetching data, it calls

    SQLCloseCursor to close the cursor.

    SQLCloseCursor

    Closes a cursor that has been opened on a

    statement and discards pending results.

    STEP 5: Commit The Transaction

    The application performs this step only if it

    set the transaction commit mode to

    manual-commit; if the transaction commit

    mode is auto-commit, which is the default,

    the transaction is automatically committed

    when the statement is executed

    SQLEndTran

    Requests a commit or rollback operation

    for all active operations on all statementsassociated with a connection.

    STEP 6: Finish STEP 6: Disconnect From The Data

    Source

    ct_cmd_drop

    Deallocate a command structure.

    SQLFreeHandle(STMT)

    Free the statement. Driver releases the

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    17/30

    17

    structure used to store information about

    the statement.

    ct_close

    Close a connection.

    ct_con_drop

    Deallocate connection structure.

    SQLDisconnect

    Frees any statements that are allocated on

    the connection and disconnects the driver

    from the data source.

    ct_exit

    Exit Client-Library for a specific context.

    Closes and deallocates any open

    connections and cleans up internal Client-

    Library data space.

    SQLFreeHandle(DBC)

    Free the connection. Driver releases the

    structure used to store information about

    the connection.

    cs_ctx_dropDeallocates a context structure.

    SQLFreeHandle(ENV)Frees the environment handle. Driver

    releases the structure used to store

    information about the environment.

    Type and structure mapping

    The following table displays the correspondence of types and structures of CT-Library to

    types and structures of ODBC:

    CT-Library ODBC

    Structures

    CS_CONTEXT

    Context structure.

    HENV

    Environment handle.

    CS_CONNECTION

    Connection structure.

    HDBC

    Connection handle.

    CS_COMMAND

    Command structure.

    HSTMT

    Statement handle.

    Types

    Numeric types

    CS_TINYINT

    tinyint

    SQLSCHAR (SQL_C_STINYINT)

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    18/30

    18

    CS_SMALLINT

    smallint

    SQLSMALLINT (SQL_C_SSHORT)

    CS_USMALLINT

    usmallint

    SQLUSMALLINT (SQL_C_USHORT)

    CS_INT

    int

    SQLINTEGER (SQL_C_SLONG)

    CS_UINT

    uint

    SQLUINTEGER (SQL_C_ULONG)

    CS_BIGINT

    bigint

    SQLBIGINT (SQL_C_SBIGINT)

    CS_UBIGINT

    ubigint

    SQLUBIGINT (SQL_C_UBIGINT)

    CS_DECIMAL

    decimal

    SQL_NUMERIC_STRUCT (SQL_C_NUMERIC)

    structure

    CS_NUMERIC

    numeric

    SQL_NUMERIC_STRUCT (SQL_C_NUMERIC)

    structure

    CS_FLOAT

    float

    SQLDOUBLE, SQLFLOAT (SQL_C_DOUBLE)

    CS_REAL

    real

    SQLREAL (SQL_C_FLOAT)

    Money types

    CS_MONEY

    money

    SQL_NUMERIC_STRUCT (SQL_C_NUMERIC)

    structure

    CS_MONEY4smallmoney

    SQL_NUMERIC_STRUCT (SQL_C_NUMERIC)structure

    Character types

    CS_CHAR

    char varchar

    SQLCHAR (SQL_C_CHAR)

    CS_UNICHAR

    unichar univarchar

    SQLWCHAR (SQL_C_WCHAR)

    Datetime types

    CS_DATE

    date

    SQL_DATE_STRUCT (SQL_C_TYPE_DATE)

    structureCS_TIME

    time

    SQL_TIME_STRUCT (SQL_C_TYPE_TIME)

    structure

    CS_DATETIME

    datetime

    SQL_TIMESTAMP_STRUCT

    (SQL_C_TYPE_TIMESTAMP)

    structure

    CS_DATETIME4 SQL_TIMESTAMP_STRUCT

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    19/30

    19

    smalldatetime (SQL_C_TYPE_TIMESTAMP)

    structure

    Bit types

    CS_BIT

    bit

    SQLCHAR (SQL_C_BIT)

    Binary types

    CS_BINARY

    binary varbinary

    SQLCHAR (SQL_C_BINARY)

    Text and image types

    CS_TEXT

    text

    SQLCHAR (SQL_C_CHAR)

    CS_UNITEXT

    unitext

    SQLWCHAR (SQL_C_WCHAR)

    CS_IMAGE

    image

    SQLCHAR (SQL_C_BINARY)

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    20/30

    20

    DB-Library Schema migration

    Overview

    This section explains mappings and differences between DB-Library and ODBC modules,

    commands and types.

    Header files mapping

    The following table displays the list of required header C files for the use DB-Library and

    ODBC API.

    DB-Library ODBC

    #include

    #include

    #include

    #include

    #include

    Routine mapping

    The following table displays the commands, used in typical execution steps of DB-Library

    and ODBC applications, and their correspondence.

    DB-Library ODBC

    STEP 1: Initialization STEP 1: Connect To The Data Source

    dbinit()

    Initialize DB-Library.

    SQLAllocHandle(ENV)

    Load the Driver Manager and allocate theenvironment handle.Driver Manager allocates a structure inwhich to store information about theenvironment, and returns the environmenthandle.

    dbsetversion(dbproc, version)

    Specify the version level for desiredbehavior.

    SQLSetEnvAttr

    Sets attributes that govern aspects ofenvironments.

    STEP 2: Define Error Handling

    dberrhandle(handler)

    Install DB-Library error callback function.

    dbmsghandle(handler)

    Install DB-Library server message callbackfunction.

    Replace with SQLGetDiagFieldandSQLGetDiagRecfunction calls in the returncode handlers.

    SQLGetDiagField

    Returns the current value of a field of a

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    21/30

    21

    record of the diagnostic data structure(associated with a specified handle) thatcontains error, warning, and statusinformation.

    SQLGetDiagRec

    Returns the current values of multiple fieldsof a diagnostic record that contains error,warning, and status information. UnlikeSQLGetDiagField, which returns onediagnostic field per call, SQLGetDiagRecreturns several commonly used fields of adiagnostic record, including the SQLSTATE,the native error code, and the diagnosticmessage text.

    STEP 3: Connect To A Server

    dblogin()

    Allocate a LOGINREC for use in dbopen.

    SQLAllocHandle(DBC)

    Allocate a connection handle.Driver Manager allocates a structure inwhich to store information about theconnection and returns the connectionhandle.

    DBSETLUSER(loginrec, username)

    Set the username in the LOGINRECstructure.

    DBSETLPWD(loginrec, password)

    Set the user server password in theLOGINREC structure.

    DBSETLAPP(loginrec, application)

    Set the application name in the LOGINRECstructure.

    SQLSetConnectAttr

    Sets attributes that govern aspects ofconnections.Some connection attributes must be setbefore the application attempts to connect,

    others can be set after the connection hascompleted.

    dbopen(loginrec, server)

    Connect to a server (and allocate theDBPROCESS).

    SQLConnector SQLDriverConnectorSQLBrowseConnectSQLConnect establishes connections to adriver and a data source.

    STEP 4: Send A Language Command

    To The Server

    STEP 2: Initialize The Application

    SQLAllocHandle(STMT)

    Driver Manager allocates a structure inwhich to store information about thestatement and calls SQLAllocHandle in thedriver with the SQL_HANDLE_STMT option.The driver allocates its own structure in

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    22/30

    22

    which to store information about thestatement and returns the driver statementhandle to the Driver Manager.The Driver Manager returns the DriverManager statement handle.

    SQLSetStmtAttr

    Sets attributes related to a statement.

    STEP 3: Build And Execute An SQL

    Statement

    dbfcmd(dbproc, string, args...)

    Format text and add to the DBPROCESScommand buffer.

    dbcmd(dbproc, string)

    Add text to the DBPROCESS command

    buffer.

    dbrpcinit(dbproc, rpc_name, option)

    Initialize an RPC.

    SQLPrepare

    Prepares an SQL string for execution.

    dbrpcparam(dbproc, paramname,

    status, type, maxlen, datalen, data)

    Add a parameter to an RPC.

    SQLBindParameter

    Binds a buffer to a parameter marker in anSQL statement.

    dbsqlexec(dbproc)

    Send a command batch to the server for

    execution.dbrpcsend(dbproc)

    Send an RPC call to the server forexecution.

    SQLExecute

    Executes a prepared statement, using the

    current values of the parameter markervariables.

    STEP 5: Process The Results Of The

    Command

    STEP 4: Fetch The Results; Fetch The

    Row Count

    dbresults

    Sets up the results of the next query.

    dbnumaltsReturns the number of columns in acompute row.

    dbnumcols

    Determines the number of regular columnsfor the current set of results.

    dbnumrets

    Determines the number of return parameter

    SQLNumResultColsReturns the number of columns in a resultset.If this is 0, the statement did not create aresult set, if it is any other number, thestatement did create a result set.

    SQLRowCount

    Returns the number of rows affected by an

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    23/30

    23

    values generated by a stored procedure.

    DBCOUNT

    Returns the number of rows affected by aTransact-SQL command.

    DBCURCMD

    Returns the number of the current logicalcommand.

    UPDATE, INSERT, or DELETE statement.If a batch of SQL statements is executed,the count of affected rows might be a totalcount for all statements in the batch orindividual counts for each statement in thebatch.

    dbaltlen

    Returns the maximum length of the data fora particular compute column.

    dbalttype

    Returns the datatype for a compute column.

    dbaltutype

    Returns the user-defined datatype for a

    compute column.dbcollen

    Returns the maximum length of the data ina regular result column.

    dbcolname

    Returns the name of a regular resultcolumn.

    dbcoltype

    Returns the datatype for a regular resultcolumn.

    dbcolutype

    Returns the user-defined datatype for aregular result column.

    dbretname

    Determines the name of the storedprocedure parameter associated with aparticular return parameter value.

    dbrettype

    Determines the datatype of a returnparameter value generated by a storedprocedure.

    SQLDescribeCol

    Returns the result descriptorcolumnname, type, column size, decimal digits, andnullability for one column in the result set.

    dbaltbind, dbaltbind_ps

    Binds a compute column to a programvariable.

    dbbind, dbbind_ps

    Binds a regular result column to a programvariable.

    SQLBindCol

    Binds application data buffers to columns inthe result set.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    24/30

    24

    dbnextrow

    Reads the next result row.

    SQLFetch

    Fetches the next rowset of data from theresult set and returns data for all boundcolumns.

    SQLGetData

    Retrieves data for a single column in theresult set. It can be called multiple times toretrieve variable-length data in parts.The application now calls SQLFetch toretrieve the first row of data and place thedata from that row in the variables boundwith SQLBindCol. If there is any long data inthe row, it then calls SQLGetData to retrievethat data. The application continues to callSQLFetch and SQLGetData to retrieveadditional data.

    After it has finished fetching data, it calls

    SQLCloseCursor to close the cursor.

    SQLCloseCursor

    Closes a cursor that has been opened on astatement and discards pending results.

    STEP 5: Commit The Transaction

    The application performs this step only if itset the transaction commit mode to manual-commit; if the transaction commit mode isauto-commit, which is the default, the

    transaction is automatically committed whenthe statement is executed

    SQLEndTran

    Requests a commit or rollback operation forall active operations on all statementsassociated with a connection.

    STEP 6: Cleanup STEP 6: Disconnect From The Data

    Source

    SQLFreeHandle(STMT)

    Free the statement. Driver releases thestructure used to store information about thestatement.

    dbclose(dbproc)

    Close and deallocate a DBPROCESSstructure.

    SQLDisconnect

    Frees any statements that are allocated onthe connection and disconnects the driverfrom the data source.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    25/30

    25

    dbexit()

    Close and deallocate all DBPROCESSstructures and clean up any structuresinitialized by dbinit.

    SQLFreeHandle(DBC)

    Free the connection. Driver releases thestructure used to store information about theconnection.

    SQLFreeHandle(ENV)

    Frees the environment handle. Driverreleases the structure used to storeinformation about the environment.

    Type and structure mapping

    The following table displays the correspondence of types and structures of DB-Library to

    types and structures of ODBC:

    DB-Library ODBC

    Structures

    LOGINREC

    Contains typical logininformation

    HDBC

    Connection handle

    DBPROCESS

    Through the DBPROCESS,commands are sent to the

    server and query results arereturned to the application

    HSTMT

    Statement handle

    Types

    Numeric types

    SYBINT1

    1-byte integer

    SQLSCHAR (SQL_C_STINYINT)

    SYBINT2

    2-byte integer

    SQLSMALLINT (SQL_C_SSHORT)

    SYBINT4

    4-byte integer

    SQLINTEGER (SQL_C_SLONG)

    SYBDECIMAL

    decimal type

    SQL_NUMERIC_STRUCT (SQL_C_NUMERIC)

    structure

    SYBNUMERIC

    numeric type

    SQL_NUMERIC_STRUCT (SQL_C_NUMERIC)

    structure

    SYBFLT8 SQLDOUBLE, SQLFLOAT (SQL_C_DOUBLE)

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    26/30

    26

    8-byte float type

    SYBREAL

    4-byte float type

    SQLREAL (SQL_C_FLOAT)

    Money types

    SYBMONEY

    money type

    SQL_NUMERIC_STRUCT (SQL_C_NUMERIC)

    structure

    SYBMONEY4

    4-byte money type

    SQL_NUMERIC_STRUCT (SQL_C_NUMERIC)

    structure

    Character types

    SYBCHAR

    char type

    SQLCHAR (SQL_C_CHAR)

    Datetime types

    SYBDATETIME

    datetime type

    SQL_TIMESTAMP_STRUCT

    (SQL_C_TYPE_TIMESTAMP)

    structure

    SYBDATETIME4

    4-byte datetime type

    SQL_TIMESTAMP_STRUCT

    (SQL_C_TYPE_TIMESTAMP)

    structure

    Bit types

    SYBBIT

    bit type

    SQLCHAR (SQL_C_BIT)

    Binary types

    SYBBINARY

    binary type

    SQLCHAR (SQL_C_BINARY)

    Text and image types

    SYBTEXT

    text type

    SQLCHAR (SQL_C_CHAR)

    SYBIMAGE

    image type

    SQLCHAR (SQL_C_BINARY)

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    27/30

    27

    Other Types of Migration

    This section covers migration of several Sybase features related to application programming

    and products using connection to Sybase.

    Using FreeTDS tool to migrate CT Library and DB Library applications to

    SQL Server

    FreeTDS libraries can be used for redirecting Sybase client applications to SQL Server.

    FreeTDS provides implementations of DB-Library, CT-Library and ODBC driver, allowing many

    Sybase client applications that use these interfaces to communicate with Microsoft SQL Server.

    You can download the source code for FreeTDS from the Web site (http://www.freetds.org)and

    then build, install, and configure it for your target platform.

    Take into account that FreeTDS is an open source project and is still very much a work in

    progress. Although much of the core functionality is available, you may find it necessary to

    either implement any missing functionality in the FreeTDS libraries that your application

    requires, or modify your application to avoid these features.

    Migration of Open Client Embedded SQL

    Sybase Embedded SQL is a superset of Transact-SQL that lets place Transact-SQL statementsin application programs written in C.

    Open Client Embedded SQL enables to create programs that access and update Adaptive

    Server data. Embedded SQL programmers write SQL statements directly into an application

    program written in C. A preprocessing program the Embedded SQL precompiler

    processes the completed application program, resulting in a program that the host language

    compiler can compile. The program is linked with Open Client Client-Library before it is

    executed.

    Embedded SQL is one of the two programming methods Sybase provides for accessing

    Adaptive Server. The other programming method is the call-level interface. With the call-levelinterface, you place Client-Library calls directly into an application program, then link with Client-

    Library.

    Embedded SQL statements can be placed anywhere in a host program and mixed with host

    language statements. All Embedded SQL statements must begin with the keywords EXEC SQL

    and end with a semicolon (;). After you write an Embedded SQL program, run it through the

    precompiler, which translates the Embedded SQL statements into Client-Library function calls.

    http://www.freetds.org/http://www.freetds.org/http://www.freetds.org/http://www.freetds.org/
  • 8/12/2019 Migrating Sybase aPis to sQl Server

    28/30

    28

    To migrate Sybase C application with Embedded SQL on SQL Server, we suggest replace

    Open Client Embedded SQL statements with ODBC routine calls.

    Migration of ASE Job Scheduler objects

    The SQL Server counterpart of Sybase ASE Job Scheduler is the SQL Server Agent jobs. Like

    Sybase Job Scheduler the SQL Server Agent allows you to create and schedule jobs, and to

    share jobs and schedules. At the same time SQL Server Agent provides more functionality to

    job and schedule management.

    You can use SQL Server Agent jobs to automate routine administrative tasks and run them on arecurring basis, making administration more efficient.

    A job is a specified series of operations performed sequentially by SQL Server Agent. A job can

    perform a wide range of activities, including running Transact-SQL scripts, command-line

    applications, Microsoft ActiveX scripts, Integration Services packages, Analysis Services

    commands and queries, or Replication tasks. Jobs can run repetitive tasks or those that can be

    scheduled, and they can automatically notify users of job status by generating alerts, thereby

    greatly simplifying SQL Server administration.

    You can run a job manually, or you can configure it to run according to a schedule or in

    response to alerts.

    Conversion of Transact-SQL statements which are embedded in

    applications

    You can use the Statements branch in SSMA for Sybase to convert SQL strings embedded in

    the user's application code by executing the following steps:

    1. In Sybase tree, select the required database and schema and execute Add Statement

    command from the Statements branch context menu to create new Statement window.2. Cut the statement from the application (or reconstruct it if the statement is built according

    to an algorithm).

    3. Paste it into the Statement window.

    4. Execute Convert Schema.

    5. Paste the result back into the proper place in the application code.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    29/30

    29

    Temporary tables in code can create problems when their definitions are absent in the module

    you are converting. Generally, you should convert the module where a temporary table is

    created before converting the modules from which the temporary table is referenced. That way,

    SSMA is able to remember the table definition and to use it properly.

  • 8/12/2019 Migrating Sybase aPis to sQl Server

    30/30

    Conclusion

    This migration guide can give you some hints about migration of Sybase-based applications to

    SQL Server. We discussed typical solutions for most commonly used Sybase application

    interfaces.

    About DB Best Technologies

    DB Best Technologies is a leading provider of database and application migration services and

    custom software development. We have been focused on heterogeneous database

    environments (SQL Server, Oracle, Sybase, DB2, MySQL) since starting at 2002 in Silicon

    Valley. Today, with over 75 employees in the United States and Europe, we develop database

    tools and provide services to customers worldwide.

    DB Best developed migration tools to automate conversion between SQL dialects. In 2005Microsoft acquired this technology, which later became a family of SQL Server Migration

    Assistant (SSMA) products. We continue to develop new versions of SSMA, and support

    Microsoft customers who are migrating to SQL Server.

    We also provide migration services covering all major steps of a typical migration project:

    complexity assessment, schema conversion, data migration, application conversion, testing,

    integration, deployment, performance tuning, training, and support.

    For more details, visit us at www.dbbest.com, e-mail us at [email protected], or call 1-408-202-

    4567.

    For more information:

    http://www.microsoft.com/sqlserver/:SQL Server Web site

    http://technet.microsoft.com/en-us/sqlserver/ :SQL Server TechCenter

    http://msdn.microsoft.com/en-us/sqlserver/ :SQL Server DevCenter

    Did this paper help you? Please give us your feedback. Tell us on a scale of 1 (poor) to 5

    (excellent), how would you rate this paper and why have you given it this rating? For example:

    Are you rating it high due to having good examples, excellent screen shots, clear writing,

    or another reason?

    Are you rating it low due to poor examples, fuzzy screen shots, or unclear writing?

    This feedback will help us improve the quality of white papers we release.

    Send feedback.

    http://www.microsoft.com/sqlserver/http://www.microsoft.com/sqlserver/http://technet.microsoft.com/en-us/sqlserver/http://technet.microsoft.com/en-us/sqlserver/http://msdn.microsoft.com/en-us/sqlserver/http://msdn.microsoft.com/en-us/sqlserver/mailto:[email protected]?subject=White%20Paper%20Feedback:%20[Title%20of%20White%20Paper]mailto:[email protected]?subject=White%20Paper%20Feedback:%20[Title%20of%20White%20Paper]mailto:[email protected]?subject=White%20Paper%20Feedback:%20[Title%20of%20White%20Paper]http://msdn.microsoft.com/en-us/sqlserver/http://technet.microsoft.com/en-us/sqlserver/http://www.microsoft.com/sqlserver/