UPDATE with rawSQL plugin and IN operator

Questions and answers regarding general SQL and backend databases

UPDATE with rawSQL plugin and IN operator

Postby studiomiazzo » Tue Nov 20, 2012 5:02 pm

I need to update a column in the table_name using the rawSQL plugin.
Is it possible to use the 'IN' operator using such a code:

Code: Select all
        var paramsArray = [x,y];
   var sqlString = 'UPDATE table_name SET field_1 = 1 WHERE field_y IN ?';
      
   var done = plugins.rawSQL.executeSQL('server_name','table_name',sqlString,paramsArray);   


Thanks in advance.
User avatar
studiomiazzo
 
Posts: 124
Joined: Thu Jun 16, 2011 10:48 am
Location: Novara (IT)

Re: UPDATE with rawSQL plugin and IN operator

Postby ROCLASI » Tue Nov 20, 2012 6:49 pm

Hi,

SQL requires a placeholder per argument you pass so the example code you gave will error.
You can use an IN statement and still use a prepared statement like so:
Code: Select all
var paramsArray = [x,y];
var sqlString = "UPDATE table_name SET field_1 = 1 WHERE field_y IN (" + paramsArray.map(function() {return "?"}).join(",") + ")";
var done = plugins.rawSQL.executeSQL('server_name','table_name',sqlString,paramsArray);   

This will generate the amount of placeholders for the values in your parameter array.

Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: UPDATE with rawSQL plugin and IN operator

Postby studiomiazzo » Wed Nov 21, 2012 10:04 am

I guessed the problem was referred to the parameters but I didn't get how to resolve!
Thank you very much!
User avatar
studiomiazzo
 
Posts: 124
Joined: Thu Jun 16, 2011 10:48 am
Location: Novara (IT)


Return to SQL Databases

Who is online

Users browsing this forum: No registered users and 8 guests