简体   繁体   English

如何识别硬编码的数据库名称 - SQL Server 2008 r2

[英]How to identify hardcoded database name - SQL Server 2008 r2

I need to find a way to determine what object in a database has the original database name hardcoded into it. 我需要找到一种方法来确定数据库中的哪个对象具有硬编码到其中的原始数据库名称。 I've restored a copy of a database under a different name and run homegrown scripts to make all the naming adjustments. 我已经以不同的名称恢复了数据库的副本,并运行自行开发的脚本来进行所有命名调整。 Let's say the original DB was named ABC_Db and the restored copy has been renamed to XYZ_Db. 假设原始数据库名为ABC_Db,恢复的副本已重命名为XYZ_Db。 When I attempt to perform an UPDATE on CoreTable, I get 当我尝试在CoreTable上执行UPDATE时,我得到了

Invalid object name 'ABC_Db.dbo.CoreTable' 无效的对象名称'ABC_Db.dbo.CoreTable'

I've queried against the syscomments and done various manual checks against relations and indexes, etc. with no luck. 我已经查询了syscomments并对关系和索引等进行了各种手动检查,没有运气。 What's next? 下一步是什么?

Thanks, 谢谢,

John 约翰

To find it 找到它

SELECT
   OBJECT_NAME(object_id)
FROM
   sys.sql_modules
WHERE
   definition LIKE '%ABC_Db%'

syscomments is unreliable for long code. 对于长代码,syscomments是不可靠的。

sys.sql_modules covers all code and is safe for all objects sys.sql_modules涵盖所有代码,对所有对象都是安全的

sys.syscomments text is nvarchar(4000) so you can have issues with truncation when a definition splits across multiple rows. sys.syscomments文本是nvarchar(4000),因此当定义分割多行时,您可能会遇到截断问题。 Try this instead. 试试这个。

select quotename(s.name)+'.'+quotename(o.name) as object_name, o.type_desc
    from sys.sql_modules m
        inner join sys.objects o 
            on m.object_id = o.object_id
        inner join sys.schemas s 
            on o.schema_id = s.schema_id
    where m.definition like '%ABC_Db%'

If you want to search through a Microsoft SQL Server database schema, the best tool is RedGate's SQL Search - and it's free. 如果您想搜索Microsoft SQL Server数据库架构,最好的工具是RedGate的SQL搜索 - 它是免费的。 It's awesome. 这很棒。

http://www.red-gate.com/products/sql-development/sql-search/ http://www.red-gate.com/products/sql-development/sql-search/

最好的方法是生成数据库脚本并使用文本编辑器搜索文件工具来查找是否存在任何存储过程或其他代码中的内容。

是否有一个具有硬编码的完整对象名称的触发器?

SQL Prompt 5 has a feature called "Find Invalid Objects", which will identify stored procedures that reference objects that don't exist. SQL Prompt 5具有一个名为“查找无效对象”的功能,该功能将标识引用不存在的对象的存储过程。 It could be worth a try. 值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM