查看代碼中的評論。如何獲取調用存儲過程的數據庫名稱?
use master
go
create database db1
create database db2
go
-------------------------------
use db2
go
create procedure proc2
as begin
-- how I can figure out that this proc2 is called
-- 1. from proc1 which is contained in the db1 (case N1)
-- 2. or from context of db1 (case N2)
select DB_NAME()
end
go
-------------------------------
use db1
go
create procedure proc1
as begin
exec db2.dbo.proc2
end
go
select DB_NAME();
-- case N1
exec dbo.proc1;
-- case N2
exec db2.dbo.proc2;
-------------------------------
use master
go
drop database db1
drop database db2
輸出
db1
db2
db2
'@@ nestlevel'和'original_db_name()'可能會給你一些信息,但不是太多 –