DBI, Placeholders, and a nested query
Edit: Solution found and described below.
Hello all,
I'm attempting to insert/update into an MSSQL database. The source of the data is another database. Sometimes the source database has new records, and other times there are existing records.
So I'm attempting to do this:
IF NOT EXISTS(SELECT * FROM dbTable WHERE library_no=:library_no) BEGIN INSERT INTO dbTable (...library_no1...) VALUES ( ... :library_no2...); END ELSE BEGIN UPDATE dbTable SET=cut fields=end WHERE library_no=:library_no3; END GO
(DBI returns an error if I try to reuse a label, so for this I've had to create different labels with the same content, which isn't a huge problem; this is not related to the problem).
I'm getting the following error:
Can't bind unknown placeholder ':library_no'...
This is in the first line and is supposed to be related to a sub-query used to determine if we're updating or inserting data.
Is DBI unable to bind to nested queries, or am I missing something? I'd prefer to do this in one query instead of checking each record to decide on whether to insert vs. update.
Thanks!
Update: Solution
The issue is that the ODBC connection in Perl does not not work with the : before the named parameter. So while you can have , needs it address as "name", not ":name". This appears to be specific to ODBC, so depending on how you connect to your database, this may or may not apply.
Thanks thread!