<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: obtain object_id value for executing SQL Anywhere code in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833431#M4864274</link>
    <description>&lt;P&gt;Thank's Volker for the very detailed response.  Indeed this will work.  It would be nice if there was a more direct way to get the object_id of executing server code.  Perhaps an @@ObjectId global variable, as suggested in my comment to Mark, above.&lt;/P&gt;
&lt;P&gt;I am curious why you use a 'waitfor delay' before the 'message' statement?&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
    <pubDate>Thu, 08 Dec 2016 09:51:27 GMT</pubDate>
    <dc:creator>dhkom</dc:creator>
    <dc:date>2016-12-08T09:51:27Z</dc:date>
    <item>
      <title>obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaq-p/13833421</link>
      <description>&lt;P&gt;How can I obtain the object_id value for executing SQL Anywhere code.  If accessible, I could then obtain the name of the executing SQL Anywhere code with:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;declare my_object_id integer;
declare executing_object_name char (128);

set my_object_id = ???;
set executing_object_name = object_name (my_object_id);&lt;/LI-CODE&gt;


&lt;P&gt;If I knew what to substitute into '???', I could obtain the name of any stored procedure or trigger that is currently executing.  &lt;/P&gt;
&lt;P&gt;Is there another way to do this?&lt;/P&gt;
&lt;P&gt;Thanks,
Dan.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2016 15:19:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaq-p/13833421</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2016-12-07T15:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833422#M4864265</link>
      <description>&lt;P&gt;Each type of object (procedure, table, etc) has an object_id column in the corresponding SYS* table (e.g. sysprocedure, systable, etc).  So to get the object id of an entity you need to query the associated SYS table.&lt;/P&gt;
&lt;P&gt;For example, to find the object id of a procedure, use&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select object id from sysprocedure where proc_name = 'my_procedure';&lt;/LI-CODE&gt;


&lt;P&gt;You can also just use the &lt;A href="http://dcx.sap.com/index.html#sqla170/en/html/81f83e6a6ce210148bb8a319b96e3f9a.html*loio81f83e6a6ce210148bb8a319b96e3f9a"&gt;OBJECT_ID()&lt;/A&gt; function - e.g.&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select object_id( 'myuser.my_procedure' );&lt;/LI-CODE&gt;


&lt;P&gt;FWIW: The OBJECT_ID() function simply queries the sysobject table to get the answer!&lt;/P&gt;
&lt;P&gt;HTH&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2016 15:48:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833422#M4864265</guid>
      <dc:creator>MarkCulp</dc:creator>
      <dc:date>2016-12-07T15:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833423#M4864266</link>
      <description>&lt;P&gt;There is a global variable available called &lt;A href="http://dcx.sap.com/index.html#sa160/en/dbreference/thvagv.html"&gt;@@procid&lt;/A&gt; (as part of the TSQL support) that may help.&lt;/P&gt;
&lt;P&gt;Doing something along the lines of&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt; select proc_name from sys.sysprocedure where proc_id=@@procid;&lt;/LI-CODE&gt;


&lt;P&gt;seems to fix your purpose.&lt;/P&gt;
&lt;P&gt;This was added back in version 5 and is still documented for V16 and still seems to work in V17.  Future versions may or may not continue this tradition.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2016 15:59:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833423#M4864266</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2016-12-07T15:59:01Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833425#M4864268</link>
      <description>&lt;P&gt;Thanks, but my objective was to determine the name of the object - in particular the name of the server code that is executing.  Your suggestion has me know the name that I was looking to calculate.&lt;/P&gt;
&lt;P&gt;My larger objective, if this helps, is to be able to emit debugging code from any trigger or stored procedure that includes the name of the stored procedure - without having to code the name myself.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2016 16:11:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833425#M4864268</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2016-12-07T16:11:30Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833426#M4864269</link>
      <description>&lt;P&gt;Thanks.  I was aware of the @@procid technique.  I was hoping to find a similar technique to determine the id of an executing trigger (@@triggerid does not exist), or better yet, a single way to find the object_id of the executing SP or trigger.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2016 16:13:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833426#M4864269</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2016-12-07T16:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833427#M4864270</link>
      <description>&lt;P&gt;Ah, so then you want to use OBJECT_NAME() function - see &lt;A href="http://dcx.sap.com/index.html#sqla170/en/html/81f83e6a6ce210148bb8a319b96e3f9a.html"&gt;http://dcx.sap.com/index.html#sqla170/en/html/81f83e6a6ce210148bb8a319b96e3f9a.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2016 17:49:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833427#M4864270</guid>
      <dc:creator>MarkCulp</dc:creator>
      <dc:date>2016-12-07T17:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833424#M4864267</link>
      <description>&lt;P&gt;Based on Nick's suggestion, here's test code that relies on the &lt;STRONG&gt;wild guess&lt;/STRONG&gt; that for triggers, the @@procid value is 0x80000000 (= 2147483648) + the trigger_id value from systrigger...&lt;/P&gt;
&lt;P&gt;I don't think that it's documented, but it seems reasonable that code within triggers (and possibly within event handlers) will have a fitting @@procid value, too. (And it somewhat resembles the documented fact that internal connections have particular ranges for their connection numbers.)&lt;/P&gt;
&lt;PRE&gt;create table T_Test
(
    pk_Test int             not null    default autoincrement primary key,
    data    varchar(255)    not null
);

create or replace proc STP_Test()
begin
    declare my_name varchar(128);
    set my_name = (select proc_name from sys.sysprocedure where proc_id = @@procid);
    waitfor delay '00:00:01';
    message 'Procedure "' || my_name || '" has proc_id ' || @@procid;
end;

create or replace trigger TIU_Test
before insert, update on T_Test
for each row
begin
    declare my_name varchar(128);
    declare trigger_proc_id_offset unsigned int = 0x80000000;
    set my_name = (select trigger_name from sys.systrigger
                   where trigger_id = @@procid - trigger_proc_id_offset);
    waitfor delay '00:00:01';
    message 'Trigger "' || my_name || '" has proc_id ' || @@procid;
    call STP_Test();
end;

create or replace trigger TD_Test
before delete on T_Test
for each row
begin
    declare my_name varchar(128);
    declare trigger_proc_id_offset unsigned int = 0x80000000;
    set my_name = (select trigger_name from sys.systrigger
                   where trigger_id = @@procid - trigger_proc_id_offset);
    waitfor delay '00:00:01';
    message 'Trigger "' || my_name || '" has proc_id ' || @@procid;
    call STP_Test();
end;

insert T_Test (data) values ('abc'), ('def'), ('geh');
delete top 1 T_Test order by pk_Test desc;
call sa_server_messages(NULL, -100);
&lt;/PRE&gt;

&lt;P&gt;returns the following in my test on V17.0.4.2100:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Trigger "TIU_Test" has proc_id 2147483662&lt;BR /&gt;
Procedure "STP_Test" has proc_id 527&lt;BR /&gt;
Trigger "TIU_Test" has proc_id 2147483662&lt;BR /&gt;
Procedure "STP_Test" has proc_id 527&lt;BR /&gt;
Trigger "TIU_Test" has proc_id 2147483662&lt;BR /&gt;
Procedure "STP_Test" has proc_id 527&lt;BR /&gt;
Trigger "TD_Test" has proc_id 2147483663&lt;BR /&gt;
Procedure "STP_Test" has proc_id 527&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;HR /&gt;
&lt;P&gt;Here's for &lt;STRONG&gt;events&lt;/STRONG&gt;:&lt;/P&gt;
&lt;PRE&gt;
create or replace event EV_Test
handler
begin
    declare my_name varchar(128);
    declare event_proc_id_offset unsigned int = 0xC0000000;
    set my_name = (select event_name from sys.sysevent
                   where event_id = @@procid - event_proc_id_offset);
    waitfor delay '00:00:01';
    message 'Event "' || my_name || '" has proc_id ' || @@procid;
end;
trigger event EV_Test;
&lt;/PRE&gt;

&lt;P&gt;shows the following message log entry:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Event "EV_Test" has proc_id 3221225473&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 08 Dec 2016 03:49:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833424#M4864267</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-12-08T03:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833428#M4864271</link>
      <description>&lt;P&gt;Indeed - but to use OBJECT_NAME(), I need an object_id value.  In stored procedures I have @@Procid.  Volker, below, provided a detailed solution for obtaining object_id values within Triggers and Events.  He admits the solution is undocumented, and thus could stop working in a future release.  It would be nice if an @@ObjectID were available in any server code which could feed into OBJECT_NAME().  &lt;/P&gt;
&lt;P&gt;Actually, I am wondering whether OBJECT_NAME() is very useful without something like an @@ObjectID global variable.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2016 08:58:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833428#M4864271</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2016-12-08T08:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833429#M4864272</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;He admits the solution is undocumented.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No, I'm just another customer, so I would not even claim that this "solution" is one - it's just a guess that the @@procid has particular values within triggers and events...&lt;/P&gt;
&lt;P&gt;Some SAP engineer would have to acknowledge that "solution" first before it could be claimed "undocumented" - and could then possibly tell if it's reliable or not.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2016 09:06:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833429#M4864272</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-12-08T09:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833430#M4864273</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;It would be nice if an @@ObjectID were available in any server code which could feed into OBJECT_NAME(). &lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hm, it seems naturally that running code has a global "self reference" via @@procid. But what would you expect that requested new "@@objectid" to refer to, as that generally may be relate to tables, columns, procedures, users, ... - all kinds of objects. So within running code, it could also make sense to know what table/column/... the code is working on currently...&lt;/P&gt;
&lt;P&gt;IMHO, in case the @@procid really does have the guessed meaning for triggers/events, it would be more helpful to&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;either document it that way, or&lt;/LI&gt;
&lt;LI&gt;add a builtin function (say, "object_id_to_proc_id()") that would map the internal proc id to the object id of the according procedure/trigger/event/whatever can run code...&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Thu, 08 Dec 2016 09:22:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833430#M4864273</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-12-08T09:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833431#M4864274</link>
      <description>&lt;P&gt;Thank's Volker for the very detailed response.  Indeed this will work.  It would be nice if there was a more direct way to get the object_id of executing server code.  Perhaps an @@ObjectId global variable, as suggested in my comment to Mark, above.&lt;/P&gt;
&lt;P&gt;I am curious why you use a 'waitfor delay' before the 'message' statement?&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2016 09:51:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833431#M4864274</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2016-12-08T09:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: obtain object_id value for executing SQL Anywhere code</title>
      <link>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833432#M4864275</link>
      <description>&lt;P&gt;Just to "delay" the message output, nothing particular and not really meaningful, I guess...&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2016 10:29:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/obtain-object-id-value-for-executing-sql-anywhere-code/qaa-p/13833432#M4864275</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-12-08T10:29:59Z</dc:date>
    </item>
  </channel>
</rss>

