Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CREATE DATABASE Failing When Using the ES6 Tagged Template Literal Approach #1680

Closed
jasel-lewis opened this issue Jul 4, 2024 · 1 comment

Comments

@jasel-lewis
Copy link

jasel-lewis commented Jul 4, 2024

I've noticed an issue when using ES6 Tagged Template Literals with a CREATE DATABASE command.

Expected behavior:

Running the below, I expect a database with the name my_db to be created.

const dbname = 'my_db';
await pool.request().query`CREATE DATABASE ${dbname}`;

Actual behavior:

When running the above, I get a RequestError with the message "Incorrect syntax near '@param1'" and the stacktrace:

RequestError: Incorrect syntax near '@param1'.
    at handleError (/opt/nodejs/node_modules/mssql/lib/tedious/request.js:384:15)
    at Connection.emit (node:events:517:28)
    at Connection.emit (/opt/nodejs/node_modules/tedious/lib/connection.js:957:18)
    at RequestTokenHandler.onErrorMessage (/opt/nodejs/node_modules/tedious/lib/token/handler.js:284:21)
    at Readable.<anonymous> (/opt/nodejs/node_modules/tedious/lib/token/token-stream-parser.js:18:33)
    at Readable.emit (node:events:517:28)
    at addChunk (node:internal/streams/readable:368:12)
    at readableAddChunk (node:internal/streams/readable:341:9)
    at Readable.push (node:internal/streams/readable:278:10)
    at next (node:internal/streams/from:98:31)

Alternate Attempts

Alternate 1

const dbname = 'my_db';
await pool.request().query`CREATE DATABASE [${dbname}]`;

Fail. Running the above, a database is created with the name @param1.

Alternate 2

const dbname = 'my_db';
await pool.request()
    .input('dbname', sql.VarChar, dbname)
    .query('CREATE DATABASE @dbname');

Fail. Running the above, I get a RequestError with message "Incorrect syntax near '@dbname'" and the stacktrace:

RequestError: Incorrect syntax near '@dbname'.
    at handleError (/opt/nodejs/node_modules/mssql/lib/tedious/request.js:384:15)
    at Connection.emit (node:events:517:28)
    at Connection.emit (/opt/nodejs/node_modules/tedious/lib/connection.js:957:18)
    at RequestTokenHandler.onErrorMessage (/opt/nodejs/node_modules/tedious/lib/token/handler.js:284:21)
    at Readable.<anonymous> (/opt/nodejs/node_modules/tedious/lib/token/token-stream-parser.js:18:33)
    at Readable.emit (node:events:517:28)
    at addChunk (node:internal/streams/readable:368:12)
    at readableAddChunk (node:internal/streams/readable:341:9)
    at Readable.push (node:internal/streams/readable:278:10)
    at next (node:internal/streams/from:98:31)

Alternate 3

const dbname = 'my_db';
await pool.request().query(`CREATE DATABASE [${dbname}]`);

Success. Sort of. Running the above, a database is created with the name my_db. The problem with this, however, is we're not parameterizing/sanitizing the database name (as correctly identified by this issue which is a proposal to remove the capability).

Alternate 4

const dbname = 'my_db';
await pool.request().query`
    DECLARE @_dbname VARCHAR(128) = ${dbname};
    EXEC('CREATE DATABASE ' + @_dbname);`;

Success. This approach dynamically generates the SQL and EXECutes it, but at the cost of having to introduce an additional line of SQL to introduce a second level of abstraction (the @_dbname parameter placeholder).

Additional Information

I have encountered what appears to be similar behavior when calling for CREATE USER...FOR LOGIN... and CREATE LOGIN...WITH PASSWORD... SQL actions. I have not exhaustively explored each of these like I have (and wrote out) for CREATE DATABASE above. I'm simply trying to start with getting just one of these figured out, first (the CREATE DATABASE) and then go from there.

Software versions

  • node --version: v20.9.0
  • npm --version: 10.1.0
  • Using mssql NPM package v11.0.0
  • MS SQL Server:
    • Product: Microsoft SQL Server Express (64-bit)
    • Version: 15.0.4375.4
  • OS: Windows Server 2016 Datacenter (10.0)
@jasel-lewis
Copy link
Author

Closing because additional research has led me to believe that parameterization is not generally an option that can be used with SQL queries involving, and similar to, CREATE DATABASE.... It was also realized that this issue had become more of a "which is the best practice" inquiry, which is not appropriate use of a GitHub Issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant