SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Converting an ORACLE function into a SQL Server function


Converting an ORACLE function into a SQL Server function

Author
Message
5280_Lifestyle
5280_Lifestyle
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 325
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"
PaulB-TheOneAndOnly
PaulB-TheOneAndOnly
SSCertifiable
SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)

Group: General Forum Members
Points: 5567 Visits: 4639
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.
5280_Lifestyle
5280_Lifestyle
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 325
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"
PaulB-TheOneAndOnly
PaulB-TheOneAndOnly
SSCertifiable
SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)

Group: General Forum Members
Points: 5567 Visits: 4639
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search