如何 - 在SQL Server中创建后直接创建和使用数据库? [英] How - create and use database directly after creation in SQL Server?

查看:464
本文介绍了如何 - 在SQL Server中创建后直接创建和使用数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个sql脚本来检查数据库是否已经存在,如果它已经存在,则删除并重新创建。
之后我想在创建后直接连接它以创建表格。

I created a sql script to check if a database already exist, if it already exists it deletes and re-creates. After I would like to connect it directly after its creation for creating tables ..

这是我的代码,但它不是工作。
他宣布错误消息

Here is my code but it does not work. He announces an error message

消息911,级别16,状态1,行10
数据库'Arms2'不存在。确保输入的名称正确。

Msg 911, Level 16, State 1, Line 10 Database 'Arms2' does not exist. Make sure that the name is entered correctly.

我的脚本

IF EXISTS (select * from sys.databases where name = 'Arms2')
BEGIN 
    DROP DATABASE Arms2
    PRINT 'DROP DATABASE Arms2'
END
    CREATE DATABASE Arms2;
    PRINT 'CREATE DATABASE Arms2'

USE Arms2

CREATE TABLE .....


推荐答案

在CREATE ...之后放置GO语句...

Put a GO statement after the CREATE...

...
CREATE DATABASE Arms2;
PRINT 'CREATE DATABASE Arms2'
GO
USE Arms2

这篇关于如何 - 在SQL Server中创建后直接创建和使用数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆