I got a strange error running a Sybase stored procedure these days.

I will try explain the context and wish some comments and explanations.

I also would like to advise that some aspects about the situation couldn't be changed, because I'm in an environment where I have limited privileges (I can only change the stored procedure, nothing else).

1 - I'm using Sybase 12.5 (Adaptive Server Enterprise/12.5.4/EBF 15434 ESD#8/P/RS6000/AIX 5.1/ase1254/2105/64-bit/FBO/Sat Mar 22 19:39:46 2008)6

2 - I wrote a full stored procedure which simplified version follows below (It's a simplified version but could be used to simulate the error):

-- The procedure must be created in db1
use db1
if object_id('tempdb..tb1') is not null
drop table tempdb..tb1

drop proc stproc1

create proc stproc1 as
create table tempdb..tb1
(c1 int)

if @@error = 0
insert into tempdb..tb1 select 11
insert into tempdb..tb1 select 21
return 1 -- This just exits the procedure as if "there
was an error"

select top 1 * from tempdb..tb1
delete top 1 from tempdb..tb1
select top 1 * from tempdb..tb1
delete top 1 from tempdb..tb1

drop table tempdb..tb1

3 - The procedure is compiled in production environment using a login called user1 (through a in-house tool). This login is aliased to dbo on databases tempdb and db1 (I can't change the user permissions nor the user).

4 - To run the stored procedure, I'm using a login called user2 (this couldn't be changed). This login is aliased to dbo on database db1 and is guest on

5 - In my environment there is a very high probability to occur a deadlock (it's a very intensive OLTP system). When I ran my procedure in production environment, a deadlock occurred. When I reran that, I got this error:

Msg 267, Level 16, State 2
Object 'tempdb..tb1' cannot be found.

What has made me confused is that the table was still there (I opened another session on database and queried the table) and when we don't reference the user in object name it assumes the current user (in the case, login user2 points to user guest on tempdb).

6 - To correct this error, I changed the stored procedure replacing all tempdb..tb1 occurrences by tempdb.guest.tb1. With this modification, when I ran the procedure first time again another deadlock occured. When I reran it (cause of a deadlock), it ended OK as I've expected.

7 - To simulate this situation, I wrote the simplified procedure above.
When I ran the simplified procedured first time (using the login user2), it exited with return status 1 (there is an explicit return inside it, to simulate that an error occurred and it exits gracefully).
When I reran the procedure above again, it ended OK (very strange...).

8 - To simulate an "unhandled" error in an development environment, I just replaced "return 1" by "waitfor delay 00:00:10". After, I compiled the procedure using the login "user1" and ran it again with login "user2". While it was waiting for 10 seconds, I stopped its execution (I'm using SQL Advantage).
When I restarted it again, got the error 267 as occurred in my production environment.

I wish someone could explain why this behavior is happening, if it has some relationship with Sybase procedure compilation proccess, user permissions or if it's a Sybase bug.