Closed jasel-lewis closed 2 weeks ago
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.
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.Actual behavior:
When running the above, I get a
RequestError
with the message "Incorrect syntax near '@param1'" and the stacktrace:Alternate Attempts
Alternate 1
Fail. Running the above, a database is created with the name
@param1
.Alternate 2
Fail. Running the above, I get a
RequestError
with message "Incorrect syntax near '@dbname'" and the stacktrace:Alternate 3
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
Success. This approach dynamically generates the SQL and
EXEC
utes 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...
andCREATE LOGIN...WITH PASSWORD...
SQL actions. I have not exhaustively explored each of these like I have (and wrote out) forCREATE DATABASE
above. I'm simply trying to start with getting just one of these figured out, first (theCREATE DATABASE
) and then go from there.Software versions
node --version
: v20.9.0npm --version
: 10.1.0mssql
NPM package v11.0.0