SQL - 遍历临时表以检查空闲的DB名称

时间:2018-04-18 14:15:40

标签: sql-server

我正在尝试在这里做一个SQL脚本,但面对一些困难,因为我对此没有那么多的知识,这是我的问题:

我需要创建一个带有预定值的临时表(dbrsm01到dbrsm30)并检查迭代这些值,如果其中一个在MSSQL服务器上可用作新数据库,如果其中一个已经是在使用中它需要被忽略,因为我需要使用此值创建一个新的数据库。

这是我到目前为止所做的:

DECLARE @temp TABLE (id int, dbname varchar(10))

INSERT INTO @temp VALUES(1,'dbrsm01');
INSERT INTO @temp VALUES(2,'dbrsm02');
INSERT INTO @temp VALUES(3,'dbrsm03');
...
INSERT INTO @temp VALUES(27,'dbrsm27');
INSERT INTO @temp VALUES(28,'dbrsm28');
INSERT INTO @temp VALUES(29,'dbrsm29');
INSERT INTO @temp VALUES(30,'dbrsm30');

DECLARE @maxid INT, @counter INT, @tempname VARCHAR(10), @nameset VARCHAR(10)

SET @counter = 1
SELECT @maxid = COUNT(*) FROM @temp

WHILE (@counter <= @maxid OR @nameset = @tempname)
BEGIN

SET @tempname = (SELECT dbname FROM @temp WHERE id = @counter)
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = @tempname)
BEGIN
    SET @nameset = @tempname
END

SET @counter = @counter + 1

END

SELECT @nameset as [@dbname]

对此有任何帮助表示赞赏!谢谢大家!

4 个答案:

答案 0 :(得分:3)

这根本不需要迭代。您可以在一个声明中执行此操作:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:ignore="ExtraText">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:layout_marginTop="8dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_constraintBottom_toTopOf="@+id/songTitle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/container"
        app:layout_constraintVertical_bias="0.025">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:elevation="6dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <Button
                android:id="@+id/backToSongList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Song List" />

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/gameBoard"
        android:layout_width="match_parent"
        android:layout_height="132dp"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/songTitle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/app_bar"
        app:layout_constraintVertical_bias="0.0">

        <com.jjoe64.graphview.GraphView
            android:id="@+id/graph"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.design.widget.CoordinatorLayout>


    <ImageButton
        android:id="@+id/playRecord"
        android:layout_width="70dp"
        android:layout_height="75dp"

        android:layout_above="@id/container"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@android:drawable/star_big_on"
        app:layout_constraintVertical_bias="0.0"/>

    <TextView
        android:id="@+id/pitchText"
        android:layout_width="113dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="@+id/playRecord"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.724"
        app:layout_constraintStart_toEndOf="@+id/playRecord"
        app:layout_constraintTop_toBottomOf="@+id/songTitle"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/songTitle"
        android:layout_width="380dp"
        android:layout_height="61dp"
        android:layout_above="@id/container"
        android:layout_marginBottom="12dp"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        android:textColor="@android:color/background_dark"
        android:textSize="24sp"
        android:textStyle="bold|italic"
        app:layout_constraintBottom_toTopOf="@+id/playRecord"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="391dp"
        android:layout_height="569dp"
        android:background="#000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:context="com.example.richard.smarttabs.SongPlayer" />


</android.support.constraint.ConstraintLayout>

编辑:编写SQL时需要改变的思维方式是以编程方式思考的。你不想考虑你将要做什么,你需要考虑你要对一个专栏做什么。使用循环不是数据集方法中的一种思考方式(通常)。

编辑:没关系,这里是如何制作WITH Tally AS ( SELECT 1 AS i UNION ALL SELECT i + 1 FROM Tally WHERE i + 1 <= 30) SELECT 'dbrsm' + CONVERT(varchar(7),T.i) FROM Tally T LEFT JOIN sys.databases d ON 'dbrsm' + CONVERT(varchar(7),T.i) = d.[name] WHERE d.database_id IS NULL; 语句并制作所有数据库:

CREATE

答案 1 :(得分:0)

您可以使用BREAK并按照自己的意愿行事:

DECLARE @temp TABLE (id int, dbname varchar(10))

INSERT INTO @temp VALUES(1,'dbrsm01');
INSERT INTO @temp VALUES(2,'dbrsm02');
INSERT INTO @temp VALUES(3,'dbrsm03');

DECLARE @maxid INT, @counter INT, @tempname VARCHAR(10), @nameset VARCHAR(10)

SET @counter = 1
SELECT @maxid = COUNT(*) FROM @temp

WHILE (@counter <= @maxid /*OR @nameset = @tempname --YOU DON'T NEED THIS */)
BEGIN

    SELECT @tempname = dbname FROM @temp WHERE id = @counter

    PRINT @tempname

    IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = @tempname)
    BEGIN
        SET @nameset = @tempname
        PRINT 'ITS FREE'
        BREAK
    END
    ELSE
        PRINT 'ITS IN USE!'

    SET @counter = @counter + 1

END

SELECT @nameset as [@dbname]

如果您需要创建不存在的所有数据库,请删除BREAK并添加您的逻辑(CREATE DATABASE ...)

答案 2 :(得分:0)

您可以使用动态SQL:

DECLARE @sql      NVARCHAR(MAX)
       ,@maxid    INT = (SELECT COUNT(*) FROM @temp)
       ,@counter  INT = 1
       ,@tempname VARCHAR(10)
       ,@nameset  VARCHAR(10);

WHILE (@counter <= @maxid)
BEGIN
  SET @tempname = (SELECT dbname FROM @temp WHERE id = @counter);
  IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = @tempname)
  BEGIN
    SET @nameset = @tempname;
    SET @sql = 'CREATE DATABASE ' + QUOTENAME(@nameset);
    PRINT @sql;
    EXEC (@sql);
  END

  SET @counter += 1;
END

答案 3 :(得分:0)

SELECT 'CREATE Database '+A.dbname+' 
GO '
FROM
(
  SELECT K.dbname
  FROM @temp K
  LEFT JOIN  sys.databases Y
  ON K.dbName = Y.Name
  WHERE Y.name is NULL
 )A

您可以复制此语句的结果并运行它。