其实我也搞不懂为什么要用SQL来创建

ELSE
    
    BEGIN
        SET @sqlstr= ‘CREATE DATABASE’+’ ‘+@database_name+’ ‘
        SET @sqlstr =  @sqlstr +’ON’
        SET @sqlstr =  @sqlstr +'(‘    
        SET @sqlstr =  @sqlstr +’NAME = ‘+’ ‘+@database_name+’_dat,’
        SET @sqlstr =  @sqlstr +’FILENAME =
‘+””+@file_name_d+”+@database_name+’.mdf”,’
        SET @sqlstr =  @sqlstr +’SIZE = 10,’
        SET @sqlstr =  @sqlstr +’MAXSIZE = 50,’
        SET @sqlstr =  @sqlstr +’FILEGROWTH = 5′
        SET @sqlstr =  @sqlstr +’)’
金沙网址,        SET @sqlstr =  @sqlstr +’LOG ON’
        SET @sqlstr =  @sqlstr +'(   NAME = ‘+’
‘+@database_name+’_log,’
        SET @sqlstr =  @sqlstr +’FILENAME =
‘+””+@file_name_l+”+@database_name+’.ldf”,’
        SET @sqlstr =  @sqlstr +’SIZE = 5MB,’
        SET @sqlstr =  @sqlstr +’MAXSIZE = 25MB,’
        SET @sqlstr =  @sqlstr +’FILEGROWTH = 5MB’
        SET @sqlstr =  @sqlstr +’);’
        Print (@sqlstr) —
若是不想一贯实施,使用使用print参数先把命令打字与印刷出来
        –exec (@sqlstr);   — 实施直接上边生成的动态SQL
        –Print ‘Datbaase ‘+@database_name +’ has been created using
default data and log location in the server configuration!!’
        /*打字与印刷已经创设成功的数据库名字*/
        –Print ‘Data file location =
‘+@file_name_d+@database_name+’.mdf’;
        /*打印创立的数据库的数据文件路线*/
        –Print ‘Log file location =
‘+@file_name_l+@database_name+’.ldf’;
        /*打字与印刷成立的数据库的日志文件路线*/
    END

/*事实上笔者也搞不懂为何要用SQL来创设,明明SQL
Server有图形化创造数据库多方便啊!*/
USE master;

​DECLARE @sqlstr nvarchar(max)
/*概念二个变量*/
DECLARE @database_name nvarchar(20) = ‘MyDB’;
/*此间输入须要创建的数目库名*/
DECLARE @file_name_d nvarchar(200) = CONVERT(sysname,
SERVERPROPERTY(‘InstanceDefaultDataPath’))
/*那边是运用SQL
Server的serverproperty这一个函数来博取当前实例的私下认可数据文件地方和日志文件地方*/
DECLARE @file_name_l nvarchar(200) = CONVERT(sysname,
SERVERPROPERTY(‘InstanceDefaultLogPath’))
/*微软官方参数介绍

IF DB_ID(‘MyDB’) IS NOT NULL
    BEGIN
        PRINT ‘Database ‘ +@database_name+’ already exists’; 
–决断MyDB是或不是留存,存在的话就杀掉,小编也不知道为啥要这么写
        DROP DATABASE MyDB; –依旧手动删除吧,不利用脚本删除
    END

相关文章