开发者

Getting string from variable

开发者 https://www.devze.com 2023-03-12 16:30 出处:网络
DECLARE @dbfilepath nvarchar(128) SET @dbfilepath = \'C:\\SqlDataFiles\\Cache.mdf\' GO USE [master] GO CREATE DATABASE [Cache] ONPRIMARY
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

精彩评论

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