Home >Database >Mysql Tutorial >How to Create a SQL Database with Dynamically Parameterized File Paths?

How to Create a SQL Database with Dynamically Parameterized File Paths?

Barbara Streisand
Barbara StreisandOriginal
2024-12-25 03:56:17898browse

How to Create a SQL Database with Dynamically Parameterized File Paths?

Creating Database with Parameterized File Paths

In the realm of SQL scripting, the need to dynamically specify database file paths using parameters often arises. To achieve this, the use of dynamic SQL techniques is necessary.

Your initial attempt encountered syntax errors due to the incorrect placement of @DataFilePath and @LogFilePath in the CREATE DATABASE statement. To rectify this, dynamic SQL can be employed as follows:

DECLARE @DataFilePath AS NVARCHAR(MAX)
SET @DataFilePath = N'C:\ProgramData\Gemcom\'

DECLARE @LogFilePath AS NVARCHAR(MAX)
SET @DataFilePath = N'C:\ProgramData\Gemcom\'

USE master
Go

DECLARE @sql AS NVARCHAR(MAX)

SET @sql = 'CREATE DATABASE TestDB ON PRIMARY ( NAME = ''TestDB_Data'', FILENAME = ' + quotename(@DataFilePath) + ') LOG ON ( NAME = ''TestDB_Log'', FILENAME = ' + quotename(@LogFilePath) + ')'

EXEC (@sql)

By using the EXEC command with the dynamically generated SQL statement, you can create the TestDB database with the desired file paths specified through the @DataFilePath and @LogFilePath parameters.

The above is the detailed content of How to Create a SQL Database with Dynamically Parameterized File Paths?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn