且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL创建数据库(如果不存在),异常行为

更新时间:2022-12-05 22:34:12

尝试使用

 If(db_id(N'DBNAME') IS NULL)

如果那行不通,可能是权限。这将解释为什么您没有收到错误消息。

If that does not work, it could be the permissions. That would explain why you are not getting an error message.


...查看相应行所需的最低权限为
ALTER ANY DATABASE或VIEW ANY DATABASE服务器级权限,或
master数据库中的CREATE DATABASE权限。始终可以在sys.databases中查看与呼叫者连接的
数据库。

...minimum permissions required to see the corresponding row are ALTER ANY DATABASE or VIEW ANY DATABASE server-level permission, or CREATE DATABASE permission in the master database. The database to which the caller is connected can always be viewed in sys.databases

(来自 MS文档上的sys.databases

正在运行的用户具有什么权限?

What permissions does the user under which you are running has?

尝试更改代码以仅返回sys.databases的内容,以便查看。

Try changing your code to just return the contents of sys.databases so you can see it.