如何使用java检查sql server中已存在的数据库名称? [重复]

时间:2021-09-06 04:26:44

This question already has an answer here:

这个问题在这里已有答案:

I want to check whether the database name is alredy exists in sql server 2008 or not. If it's not means i need to create the database. How can i check this?

我想检查sql server 2008中是否存在数据库名称是否存在。如果不是意味着我需要创建数据库。我怎么检查这个?

6 个解决方案

#1


0  

You can check if it NOT EXISTS:

你可以检查它是否不存在:

IF  NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'YourDBName')
CREATE DATABASE [YourDBName]

Or you can check if it EXISTS then drop it:

或者您可以检查它是否存在然后删除它:

IF  EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'YourDBName')
DROP DATABASE [YourDBName]

#2


2  

Connection connection = DriverManager.getConnection();
ResultSet resultSet = connection.getMetaData().getCatalogs();

while (resultSet.next()) {
    // Get the database name
    String databaseName = resultSet.getString(1);
}
resultSet.close();

#3


0  

You can use show databases; SQL query.

你可以使用show databases; SQL查询。

#4


0  

If you are doing it in a startup query, combine the two actions using create if not exists SQL statement (see your DBMS actual documentation for the exact syntax).

如果您在启动查询中执行此操作,请使用create if not exists SQL语句组合这两个操作(有关确切语法,请参阅DBMS实际文档)。

#5


0  

You can use sp_databases command in sql server query window

您可以在sql server查询窗口中使用sp_databases命令

#6


0  

Possible this be helpful for you -

这可能对你有所帮助 -

CREATE FUNCTION dbo.is_db_exists
(
    @db_name NVARCHAR(100)
)
RETURNS INT
AS BEGIN

    IF EXISTS (
        SELECT 1 
        FROM sys.databases 
        WHERE name = @db_name
    ) RETURN 1

    RETURN 0

END

Or this this query -

或者这个查询 -

IF DB_ID('<db_name>') IS NOT NULL
   PRINT 'DB is exists'

#1


0  

You can check if it NOT EXISTS:

你可以检查它是否不存在:

IF  NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'YourDBName')
CREATE DATABASE [YourDBName]

Or you can check if it EXISTS then drop it:

或者您可以检查它是否存在然后删除它:

IF  EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'YourDBName')
DROP DATABASE [YourDBName]

#2


2  

Connection connection = DriverManager.getConnection();
ResultSet resultSet = connection.getMetaData().getCatalogs();

while (resultSet.next()) {
    // Get the database name
    String databaseName = resultSet.getString(1);
}
resultSet.close();

#3


0  

You can use show databases; SQL query.

你可以使用show databases; SQL查询。

#4


0  

If you are doing it in a startup query, combine the two actions using create if not exists SQL statement (see your DBMS actual documentation for the exact syntax).

如果您在启动查询中执行此操作,请使用create if not exists SQL语句组合这两个操作(有关确切语法,请参阅DBMS实际文档)。

#5


0  

You can use sp_databases command in sql server query window

您可以在sql server查询窗口中使用sp_databases命令

#6


0  

Possible this be helpful for you -

这可能对你有所帮助 -

CREATE FUNCTION dbo.is_db_exists
(
    @db_name NVARCHAR(100)
)
RETURNS INT
AS BEGIN

    IF EXISTS (
        SELECT 1 
        FROM sys.databases 
        WHERE name = @db_name
    ) RETURN 1

    RETURN 0

END

Or this this query -

或者这个查询 -

IF DB_ID('<db_name>') IS NOT NULL
   PRINT 'DB is exists'