开发者

Getting string from variable

DECLARE @dbfilepath nvarchar(128)
SET @dbfilepath = 'C:\SqlDataFiles\Cache.mdf'
GO

USE [master]
GO
CREATE DATABASE [Cache] ON  PRIMARY 
( NAME = N'Cache', FILENAME = @dbfilepath, SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
GO

Why doesn't this work?

It gives:

Msg 102, Level 15, State 1, 开发者_JAVA百科Line 3 Incorrect syntax near '@dbfilepath'.


You need to exec it;

USE [master]
GO
DECLARE @dbfilepath nvarchar(128) = 'C:\MSSQL\Cache.mdf'
DECLARE @SQL NVARCHAR(MAX) = N'CREATE DATABASE [Cache] ON PRIMARY (NAME = N''Cache'', FILENAME = ''' + @dbfilepath + ''', SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )'
EXEC(@SQL)


Try this:

DECLARE @dbfilepath nvarchar(128);
SET @dbfilepath = 'C:\\SqlDataFiles\\Cache.mdf';
PRINT @dbfilepath;

To use @dbfilepath in the Create Database statement, dynamic sql needs to be used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜