I was rolling some queries into a stored procedure and I hit the #1327 - Undeclared variable error ... the odd thing though is that the variable it claims is undeclared is actually a table name.
Working through the problem
So I extracted the bit of the procedure where it was falling over and tried to run it as a normal SQL query directly on the database through PHPMyAdmin... same thing. After much tinkering it seems to be where I'm joining another table.
If I run the query on a single table, it's fine, like this:
SET #i_channel_id = 3;
SET #i_product_id = 90;
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code
FROM `product_status_to_channel`
WHERE `product_status_to_channel`.`channel_id` = #i_channel_id
AND `product_status_to_channel`.`product_id` = #i_product_id
ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC,
`product_status_to_channel`.`date` DESC
LIMIT 0, 1;
SELECT #s_status_code AS status_code;
Which outputs 'LIVE' as the status_code in PHPMyAdmin - which is fine.
However, when I try and JOIN to the message table to find the associated status message, I get the error: #1327 - Undeclared variable: product_status_to_channel_lang ... but product_status_to_channel_lang is a table?!
SET #i_channel_id = 3;
SET #i_language_id = 3;
SET #i_product_id = 90;
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code,
`product_status_to_channel_lang`.`string` INTO #s_status_message
FROM `product_status_to_channel`
LEFT JOIN `product_status_to_channel_lang`
ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
AND `product_status_to_channel_lang`.`language_id` = #i_language_id
WHERE `product_status_to_channel`.`channel_id` = #i_channel_id
AND `product_status_to_channel`.`product_id` = #i_product_id
ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC, `product_status_to_channel`.`date` DESC
LIMIT 0, 1;
SELECT #s_status_code AS status_code, #s_status_message AS status_message;
Is it trying to evaluate product_status_to_channel_lang.product_status_to_channel_id as a variable on the JOIN?
LEFT JOIN `product_status_to_channel_lang`
ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
I assume I'm overlooking something obvious?
I've tried this on both:
a Win7 box running xampp with MySQL 5.5.27 - MySQL Community Server (GPL)
a Debian box running MySQL 5.1.73-1-log - (Debian)
Never mind - it was something obvious:
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code,
`product_status_to_channel_lang`.`string` INTO #s_status_message
Should be:
SELECT
`product_status_to_channel`.`status_code`,
`product_status_to_channel_lang`.`string`
INTO
#s_status_code,
#s_status_message
... it must be Friday, it took literally a couple of hours to see that.
... INTO #s_status_code,
`product_status_to_channel_lang`.`string` ...
^That's where it's trying to assign product_status_to_channel_lang to being a variable into which to put data.
Related
update fare_strategy set sales_date_to = ’07-SEP-22’
where dep_date_to is not null
and market_id in
(select market_id
from fare_strategy_markets
where set_type = ‘STANDARD’
);
In the following query, I'm getting error as SQL command not properly ended.
try to change like this because in your query you used ’ this is not valid so i just change it to ' so it's works fine
update fare_strategy set sales_date_to = '07-SEP-22' where dep_date_to is
not null and market_id in (select market_id from
fare_strategy_markets where set_type = 'STANDARD');
I intend to do:
EXECUTE('UPDATE tableA SET campaignkey = ''20170101'' where storekey = 16
and campaignkey LIKE ''%,%''') at MYLINKEDSERVER
but I get the error that:
You have an error in your SQL syntax; [...] for the right syntax to use near 'where storekey = 16 and campaignkey LIKE '%,%''
Does anyone have any idea what is wrong? To me it seems like I might have one ' too many on my LIKE statement, but I have Always used '' to indicate a non-numeric value. I don't want to fiddle with this to prevent updating far too many values on this server.
campaignkey is non-numeric (I Believe varchar) and storekey is integer.
Edit
I must not use OPENQUERY() because it is not set up correctly, and this is a urgent update.
Edit 2
Seems like it is because of apostrophes 's in the EXECUTE statement.
When I conduct:
select * from openquery(linkedserver,'Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''')
it works, but when using:
EXECUTE('Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''') at linkedserver
I get the error that I need to check the manual by the where clause. From googling it appears however that the correct syntax in fact is:
EXECUTE('UPDATE TableX SET StringVar = ''stringValue'' WHERE intVar = 3
AND stringVar = ''xxxxx''') AT LinkedServer
I don't know why this won't work for me.. I have tried many combinations of '', '" etc.
What about this one?
update openquery(linkedserver,'Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''')
set campaignkey = '20170101'
I tried running this sql
UPDATE table
SET read = 1
WHERE col1_id = 2
AND col3_id = 1;
and it return an error (error in your sql syntax)
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read = 1 WHERE team_notification_id = 2 AND listener_id = 1' at line 1
But when I used thesame WHERE and AND :), in a SELECT statement no error was returned.
SELECT *
FROM read
WHERE col1_id = 2
AND col3_id = 1
please what did I do wrong, I am no seeing it.
After keyword UPDATE there should be a table name. If your table has actually name table (that's a bit strange) you should escape it to let MySQL know it's not a keyword "table" but actual table name.
The same thing is about read.
Both table and read are MySQL keywords (the full list is here http://dev.mysql.com/doc/refman/5.7/en/keywords.html) so if it's your actual table and column names then you should escape it.
Better to escape all table and column names to prevent issues like that.
Escaping in MySQL is done with backtick symbol ` so your query will looks like:
UPDATE `table`
SET `read` = 1
WHERE `col1_id` = 2
AND `col3_id` = 1;
UPDATE tablename
SET `field_name` = 1
WHERE col1_id = 2
AND col3_id = 1;
UPDATE table
SET [read] = 1
WHERE col1_id = 2
AND col3_id = 1;
I am trying to update the Sphinx Search Realtime Index , using values from my Mysql Table.,
I want to add the new value in the old value of the RT index's record like this ,
i want to achieve this
UPDATE RT_index SET col1 = old_val + new_val WHERE id = xx ;
query i am trying is
UPDATE RT_index SET comments_count = comments_count + 3 WHERE id = 1157642
but Sphinx giving me errror
ERROR 1064 (42000): sphinxql: syntax error, unexpected IDENT,
expecting CONST_INT (or 4 other tokens) near 'comments_count + 3 WHERE
id = 1157642'
i have tried query like this
UPDATE RT_index SET comments_count = value(comments_count) + 3 WHERE id = 1157642;
but still sphinx gives error,
ERROR 1064 (42000): sphinxql: syntax error, unexpected IDENT,
expecting CONST_INT (or 4 other tokens) near 'value(comments_count) +
3 WHERE id = 1157642'
How can i add new value in old value using update in sphinx real-time index ?
i am using PHP to do this.
there is not much info about it in http://sphinxsearch.com/docs/current.html#sphinxql-update
You can't. Need to first run a SELECT query to get the current value, and then run the UPDATE.
Not sure if can use a transaction to make the update atomic.
I am trying to use the following query in SQL Server
SELECT [AL].[Subscriptions].Id,
[AL].[Subscriptions].name,
[AL].[Subscriptions].description,
[AL].[Subscriptions].price,
[AL].[Subscriptions].iconFileName,
IIf(a.expiryDate > Now(), 'TRUE', 'FALSE') AS isSubsByUser
FROM [AL].[Subscriptions]
LEFT JOIN (SELECT *
FROM [AL].[UserSubscriptions]
WHERE userId = 13259) AS a
ON Subscriptions.Id = a.itemid;
but always get the error
Error in list of function arguments: '>' not recognized.
Unable to parse query text.
How do I resolve it?
Like Martin Smith said you need to use a case statement. Also it looks like you are only using a couple of fields in the derived table therefor I would suggest not using *. I put a example below.
SELECT [AL].[Subscriptions].Id,
[AL].[Subscriptions].name,
[AL].[Subscriptions].description,
[AL].[Subscriptions].price,
[AL].[Subscriptions].iconFileName,
case when a.expiryDate > GetDate() then 'TRUE' else 'FALSE' end AS isSubsByUser
FROM [AL].[Subscriptions]
LEFT JOIN (SELECT expiryDate, itemid
FROM [AL].[UserSubscriptions]
WHERE userId = 13259) AS a
ON Subscriptions.Id = a.itemid;