SQL Server: use parameter in CREATE DATABASE -


i want specify path data file , log file created in sql script using parameters. here wrote:

declare @datafilepath nvarchar(max)  set @datafilepath = n'c:\programdata\gemcom\'  declare @logfilepath nvarchar(max)  set @datafilepath = n'c:\programdata\gemcom\'   use master go create database testdb on  primary  ( name = n'testdb_data', filename = @datafilepath ) log on  ( name = n'testdb_log', filename = @logfilepath  )  go 

unfortunately, doesn't work. when try run in sql server management studio, got error

incorrect syntax near '@datafilepath'.

i wondering if intended possible?

thanks

you have use dynamic sql

select @sql = 'create database testdb on primary ( name = ''testdb_data'',   filename = ' + quotename(@datafilepath) + ') log on ( name = ''testdb_log'',  filename = ' + quotename(@logfilepath) + ')'  exec (@sql) 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -