Good morning,
Suppose I have two databases, A and B, hosted on the same ASE server. While connected to database A, I need to retrieve the value of rowcnt(doampg) for a table in database B.
For example, I have tried the following query:
SELECT rowcnt(doampg)
FROM B..sysindexes
WHERE id = object_id("B..table_name")
AND indid = 0
However, the result is always 0 when I execute this query from database A. If I switch my current database context to B and execute the equivalent query there, I get the expected information.
Is there a way to retrieve the correct value for a table in database B while remaining in database A, without having to switch the current database context? More specifically, is there an alternative approach that does not require querying systabstats after executing flushstats?
I'm having this problem in ASE 12.5.x, but in later versions, "rowcnt" no longer exists and "row_count" is used instead; perhaps my problem was one of the reasons for the change.
Thank you very much for your time and help. This is an important issue for me, so any technical explanation, documentation references, or suggestions for further testing would be extremely valuable.
Regards and thanks in advanced
Request clarification before answering.
Hi, Mark: first of all, thank you for replying and for wanting to help me.
I don’t know how to thank you for each comment individually, so I’m replying with this message. Let me explain: in version 12.5.x, I’ve tried what you suggested and have opted for a compromise: creating a stored procedure in both databases as follows:
CREATE PROC test
@table VARCHAR (255)
AS
SELECT ROWCNT(doampg)
FROM sysindexes
WHERE id = OBJECT_ID(@table)
AND indid = 0
I then simply run `database..test <table name>` and get the result. I don’t like creating objects for this purpose, but given that I cannot run a cross-database query for this, it’s a good workaround.
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah, yes, this solution (local proc in each database) works, too! 🙂
Whenever I see a requirement to run a proc in multiple databases I automatically think global stored proc (a habit formed over a couple decades) so I sometimes forget the KISS principal (eg, separate proc in each database). Having said that ...
I don't currently have access to an ASE 12.5.x instance so I'm curious to know: Did you try creating a global stored proc in ASE 12.5.x and if so, did you have any issues?
Since the rowcnt() function was deprecated with the introduction of ASE 15.0 I'm going to assume OP is running ASE 12.x in which case I'd suggest a global stored procedure (defined in the sybsystemprocs database with a name that starts with 'sp_' - though I'd suggest 'sp__' to distinguish from system stored procedures).
---------------------
For demo purposes I've created MyObject (table in db1; procedure in db2) :
use db1
go
create table MyObject
(a int null
,b int null
)
go
use db2
go
create procedure MyObject
as
print 'hi'
goRunning the following query in both databases to obtain details on MyObject :
select db_name() as 'database',name as 'object name',id,type
from sysobjects
where id = object_id('MyObject')
go
-- results reformatted for readability
database object name id type
-------- ----------- ---------- ----
db1 MyObject 1657105963 U
db2 MyObject 1408005016 PWe'll incorporate this same code in a global procedure (sp__help) we'll place in the sybsystemprocs database :
use sybsystemprocs
go
create procedure sp__help
@objname varchar(255) ='sysobject'
as
select db_name() as 'database',name as 'object name',id,type
from sysobjects
where id = object_id(@objname)
goFor demo purposes we'll make our calls to sp__help from the master database :
use master
go
exec db1..sp__help 'MyObject'
exec db2..sp__help 'dbo.MyObject'
go
database object name id type
-------- ---------------- ---------- ----
db1 MyObject 1657105963 U
database object name id type
-------- ---------------- ---------- ----
db2 MyObject 1408005016 P Notice that we use the database name (db1, db2) to fully qualify the stored proc name (as opposed to fully qualifying the object name as in OP's sample query).
NOTES:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 7 | |
| 7 | |
| 5 | |
| 4 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.