azure sql DB_ID不起作用

时间:2018-06-19 09:24:21

标签: sql-server azure tsql azure-sql-database azure-sql-server

我已经通过SSMS在 Azure Sql Server中手动创建了一个名为SnapshotsDb的数据库。

尝试使用SnapshotsDb函数获取DB_ID()的ID:

DECLARE @db_id int;  
SET @db_id = DB_ID(N'SnapshotsDb')
PRINT @db_id

不输出

如果我在sql server的本地实例上尝试相同的逻辑,它会返回一个与数据库ID匹配的int。

1 个答案:

答案 0 :(得分:3)

If you need the database_id of a database you need to query the sys.databases system view and filter on the WHERE clause by the name of the database. The database_id filed in sys.databases is consistent and won’t change in the life time of the database.

You cannot rely on DB_ID() since the function may return different values depending on which of the databases you run it as part of a geo-replication setup. You can read more here.