定义变量创建数据库(create database)失败?创建数据库不可以用变量吗,
为什么我这样定义变量,创建数据库失败,
创建数据库不可以用变量吗,代码如下:
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name =N 'Temp ')
DROP DATABASE [Temp]
GO
declare @sFileName nvarchar(200)
declare @sDataName nvarchar(200)
declare @sLogName nvarchar(200)
select @sFileName=filename from master.dbo.sysdatabases where name = N 'master '
select @sFileName=Left(@sFileName,charindex( 'master.mdf ',@sFileName)-1)
set @sLogName = @sFileName+ 'Temp.ldf '
set @sDataName = @sFileName+ 'Temp.mdf '
--select @sLogName,@sDataName
CREATE DATABASE [Temp]
ON (NAME=N 'Cactus_Data ', FILENAME = N @sDataName, SIZE = 102, FILEGROWTH = 10%)
LOG
ON (NAME=N 'Cactus_Log ', FILENAME = N @sLogName, SIZE = 6, FILEGROWTH = 5%)
很急,希望高手帮忙,
GO
------解决方案--------------------declare @sql varchar(1000)
set @sql = 'CREATE DATABASE [Temp]
ON (NAME=N ' 'Cactus_Data ' ', FILENAME = N ' + @sDataName + ', SIZE = 102, FILEGROWTH = 10%)
LOG '
exec (@sql)