Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Converting an ORACLE function into a SQL Server function Expand / Collapse
Author
Message
Posted Thursday, January 24, 2013 3:27 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 20, 2014 12:49 PM
Points: 39, Visits: 278
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"
Post #1411369
Posted Thursday, January 24, 2013 6:02 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,065, Visits: 4,639
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.
Post #1411417
Posted Friday, January 25, 2013 12:00 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 20, 2014 12:49 PM
Points: 39, Visits: 278
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"
Post #1411490
Posted Friday, January 25, 2013 7:32 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,065, Visits: 4,639
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.
Post #1411718
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse