Converting an ORACLE function into a SQL Server function

  • I've migrated some Oracle databases into some SQL Server databases and now I'm working on building reports from the Oracle queries. Some of the queries reference views, which, in turn, reference functions. I've managed to fare well up until this point. The function I'm trying to convert is using Oracle's 'rowid' data type, and I don't know much about cursors. I've included the messy looking Oracle function query below. How can this be converted to SQL Server?

    create or replace

    function getlong( p_tname in varchar2,

    p_cname in varchar2,

    p_rowid in rowid ) return varchar2

    as

    l_cursor integer default dbms_sql.open_cursor;

    l_n number;

    l_long_val varchar2(4000);

    l_long_len number;

    l_buflen number := 4000;

    l_curpos number := 0;

    begin

    dbms_sql.parse( l_cursor,

    'select ' || p_cname || ' from ' || p_tname ||

    ' where rowid = :x',

    dbms_sql.native );

    dbms_sql.bind_variable( l_cursor, ':x', p_rowid );

    dbms_sql.define_column_long(l_cursor, 1);

    l_n := dbms_sql.execute(l_cursor);

    if (dbms_sql.fetch_rows(l_cursor)>0)

    then

    dbms_sql.column_value_long(l_cursor, 1, l_buflen, l_curpos ,

    l_long_val, l_long_len );

    end if;

    dbms_sql.close_cursor(l_cursor);

    return l_long_val;

    end getlong;

    "Nicholas"

  • 5280_Lifestyle (1/24/2013)


    I've migrated some Oracle databases into some SQL Server databases and now I'm working on building reports from the Oracle queries. Some of the queries reference views, which, in turn, reference functions. I've managed to fare well up until this point. The function I'm trying to convert is using Oracle's 'rowid' data type, and I don't know much about cursors. I've included the messy looking Oracle function query below. How can this be converted to SQL Server?

    create or replace

    function getlong( p_tname in varchar2,

    p_cname in varchar2,

    p_rowid in rowid ) return varchar2

    as

    l_cursor integer default dbms_sql.open_cursor;

    l_n number;

    l_long_val varchar2(4000);

    l_long_len number;

    l_buflen number := 4000;

    l_curpos number := 0;

    begin

    dbms_sql.parse( l_cursor,

    'select ' || p_cname || ' from ' || p_tname ||

    ' where rowid = :x',

    dbms_sql.native );

    dbms_sql.bind_variable( l_cursor, ':x', p_rowid );

    dbms_sql.define_column_long(l_cursor, 1);

    l_n := dbms_sql.execute(l_cursor);

    if (dbms_sql.fetch_rows(l_cursor)>0)

    then

    dbms_sql.column_value_long(l_cursor, 1, l_buflen, l_curpos ,

    l_long_val, l_long_len );

    end if;

    dbms_sql.close_cursor(l_cursor);

    return l_long_val;

    end getlong;

    Issue may be deeper than the translation of the particular function.

    The Oracle concept of rowid is this, rowid is a pseudo-column that is supposed uniquely identify the physical location of each and every single row in a database. In some particular cases that go beyond the scope of this issue, two or more rows in a database might have the same rowid.

    This is what I would do. Figure out "what" the function is attempting to do, the logic of it then rewrite the whole thing without using rowid or try to use something that "looks" like the Oracle rowid.

    I understand virtual column %%physloc%% - which I think it is not documented does for SQL Server what rowid does for Oracle. Please somebody correct me if wrong.

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
  • Thanks for the reply. I took a good look over the function and what its output was supposed to be and how it plugged into the view script. I was able to completely eliminate the function and re-coded the view. Works like a charm!

    "Nicholas"

  • 5280_Lifestyle (1/25/2013)


    Thanks for the reply. I took a good look over the function and what its output was supposed to be and how it plugged into the view script. I was able to completely eliminate the function and re-coded the view. Works like a charm!

    Thank you for the feedback Nicholas. Unfortunately, in my experience - data is the easy part of a technology migration, code is usually where the fun is 🙂

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply