Giter Site home page Giter Site logo

Comments (7)

dhensby avatar dhensby commented on May 29, 2024

Just like you've enclosed the ${table} with braces, the user_id needs to be enclosed.

const table = 'test';
const user_id = 'abab==';
const input_parameter = 'USER_ID';
-const query = `SELECT * FROM [uat].[dbo].[${table}] WITH(NOLOCK) WHERE ${user_id} = @${input_parameter}`;
+const query = `SELECT * FROM [uat].[dbo].[${table}] WITH(NOLOCK) WHERE [${user_id}] = @${input_parameter}`;

from node-mssql.

aminwafi avatar aminwafi commented on May 29, 2024

But now its giving invalid column name error for all queries,

RequestError: Invalid column name 'abab=='
RequestError: Invalid column name '160000007137377767'

from node-mssql.

dhensby avatar dhensby commented on May 29, 2024

I'm afraid that the issues are for bugs with the library and not query writing support.

I can't help you with your query, especially as I don't have any of the actual context.

Here are docs for database identifiers (column names, etc): https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver16

from node-mssql.

aminwafi avatar aminwafi commented on May 29, 2024

Sorry for opening this as an issue, I can't seem to find the workaround for this in docs or any communities :')

I should have write the question better, the variable im passing is in double quote, hence why the solution you gave return that error.

However managed to resolve this, by single quote the user_id variable in query:

const query = `SELECT * FROM [uat].[dbo].[${table}] WITH(NOLOCK) WHERE '${user_id}' = @${input_parameter}`;

Thank you for your help btw! 😄

from node-mssql.

dhensby avatar dhensby commented on May 29, 2024

I'm not sure that is doing what you expect it to do - that's a literal value comparison. A bit like doing SELECT * FROM table WHERE 1 = 1 - it's not referencing a column.

from node-mssql.

aminwafi avatar aminwafi commented on May 29, 2024

Actually you're right, I tried with other string variable yesterday and it still returns true.

I tried using LIKE operator as below, and it seems giving the same error:
SELECT * FROM [uat].[dbo].[${table}] WITH(NOLOCK) WHERE USER_KEY LIKE ${user_id}

RequestError: Incorrect syntax near '='.

Perhaps, is there any other way you can suggest me to try. I tried to remove double quote and assign single quote globally and it still not resolving the issue:

user_id = user_id.replace(/\"/g, "'") // not working
user_id = `'${user_id}'`; // giving literal value comparison

from node-mssql.

aminwafi avatar aminwafi commented on May 29, 2024

I managed to resolve this already, seems like i confused with my own logic to do the query.

It should be:

const table = 'test';
const userId = 'sample==';
const input_parameter = 'USER_ID';
const data = {[input_parameter]: userId }

const query = `SELECT * FROM [uat].[dbo].[${table}] WITH(NOLOCK) WHERE ${input_parameter} = @${input_parameter}`;

let dbRequest = new sql.Request(pool);
for (const key in data) {
      dbRequest.input(key, data[key]);
}

then do the dbRequest.query(query)

now it works as expected, sorry for the confusion :')

from node-mssql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.