返回原始调用数据库的名称 [英] Returning the name of the original calling database

查看:63
本文介绍了返回原始调用数据库的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有三个数据库 - dbOne,dbTwo和dbThree。如果我在DbThree中有一个存储过程,如何让它返回执行原始命令的数据库的名称?



使用以下内容仅给出程序所在的数据库:



Assume that I have three databases - dbOne, dbTwo and dbThree. If I have a stored procedure in DbThree, how can I get it to return the name of the database where the original command was executed?

Using the following only gives the database where the procedure is held:

CREATE PROCEDURE GetDatabaseName
AS
SET NOCOUNT ON
BEGIN
SELECT DB_Name()  -- SELECT DB_Name(dbid) returns the same result.
END





所以从任何数据库调用它都会返回dbThree,而我需要它来返回dbOne,dbTwo等。



So calling it from any database returns dbThree whereas I need it to return dbOne, dbTwo etc.

推荐答案

那是因为SP在dbThree中运行 - 当你使用时

Thats because the SP is running in dbThree - when you use
EXEC [dbThree].[dbo].GetDatabaseName

您在<持续时间内有效地发出 USE dbThree 命令code> EXEC

如果你想要原始数据库名称,那么你需要在所有数据库中添加相同的程序,并删除任何资格 EXEC it。

You are effectively issuing a USE dbThree command for the duration of the EXEC
If you want the "original" database name, then you need to add the same procedure in all the DBs, and remove any qualification when you EXEC it.


这篇关于返回原始调用数据库的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆