SELECT INTO local variable undeclared? - mysql

I have the following in an sql file I am executing...
DECLARE rowcount INT;
SELECT COUNT(*) INTO rowcount
FROM VRG_PROBLEM_ACCOUNT PA
WHERE NEW.CustName = PA.CustNAME
AND NEW.AreaCode = PA.AreaCode
AND NEW.PhoneNumber = PA.PhoneNumber;
And yet I'm getting
ERROR 1327 (42000): Undeclared variable: rowcount
In another file I am doing the same type of SELECT...INTO localvariable and it works.

Local variables can only be declared inside of stored routines. You can use #-variables instead, which don't need to be declared at all:
SELECT COUNT(*) INTO #rowcount
FROM ...

How about re-writing your query a bit?
DECLARE rowcount INT;
SET rowcount = (
SELECT COUNT(*)
FROM VRG_PROBLEM_ACCOUNT PA
WHERE PA.CustNAME = NEW.CustName
AND PA.AreaCode = NEW.AreaCode
AND PA.PhoneNumber = NEW.PhoneNumber);
UPDATE 1*
I do not know your full code, but please keep in mind that DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

Related

Can I use subquery in a user-defined function

I try to use subquery in mysql custom user-defined function I get an error so could u help me with one example.
Here is my code:
CREATE DEFINER=`root`#`localhost` FUNCTION `findsubName`(counts INT)
RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE result VARCHAR(500) DEFAULT NULL;
DECLARE v_name VARCHAR(200);
DECLARE finished INT(1) DEFAULT 0;
DECLARE my_cursor CURSOR FOR
SELECT id, (SELECT t_name FROM ajctb_titles b WHERE a.jt_id=b.t_id)
as tableName FROM ajctb_vacancies a limit counts;
DECLARE CONTINUE HANDLER
FOR NOT FOUND
SET finished = 1;
OPEN my_cursor;
calc_change: LOOP
FETCH my_cursor INTO v_name;
IF finished THEN
LEAVE calc_change;
END IF;
IF result<>'' THEN
SET result = CONCAT_WS(',',result,v_name);
ELSE
SET result = v_name;
END IF;
END LOOP calc_change;
CLOSE my_cursor;
RETURN result;
END
Error message:
Error Code: 1328. Incorrect number of FETCH variables
Error message: Error Code: 1328. Incorrect number of FETCH variables
Error messages attempt to tell you what the problem is. It is in the FETCH. Looking at the documentation:
13.6.6.3 Cursor FETCH Syntax
FETCH [[NEXT] FROM] cursor_name INTO var_name [, var_name] ...
This statement fetches the next row for the
SELECT statement associated with the specified cursor (which must be
open), and advances the cursor pointer. If a row exists, the fetched
columns are stored in the named variables. The number of columns
retrieved by the SELECT statement must match the number of output
variables specified in the FETCH statement.
https://dev.mysql.com/doc/refman/8.0/en/fetch.html
2 columns in your query:
SELECT
id
, (
SELECT
t_name
FROM ajctb_titles b
WHERE a.jt_id = b.t_id
)
AS tableName
means 2 variables are needed the FETCH
It hasn't even attempted the subquery yet.
Regarding that correlated subquery it could be a problem. When you use a subquery in the select clause like this it MUST return no more then one value. So you should use limit 1 if you continue with that subquery.
That subquery can be replaced with a join. e.g.
SELECT
id
, b.t_name AS tableName
FROM ajctb_vacancies a
LEFT JOIN ajctb_titles b ON a.jt_id = b.t_id
You may want to use an INNER JOIN if you must always have a non-null tablename returned.

use local variables in mysql IF statement

I modified the statements as below, but still errors at the line with the DECLARE statement.
BEGIN
DECLARE searchresult int(11);
SET searchresult=(Select count(*) from wbsimsynuqsql where SimBase='a cappella');
IF searchresult >0 THEN
Select * from simsyn1sql where BaseID = (Select Distinct BaseID from wbsimsynuqsql where SimBase='a cappella')
ELSE
Select * from simsyn1sql where BaseID = (Select Distinct BaseID from wbsimsynuqsql where SimSyn='a cappella')
END IF;
END
I am using the expression below in PHPMYADMIN with the intent to use it later in a PHP/MySQL application. It gives an error relating to the DECLARE statement in line 1.
I've looked at example declarations in MySQL and I don't see an error, but I'm doing something wrong and would appreciate a correction/suggestion.
DECLARE searchresult int(11);
SET searchresult=(Select count(*) from wbsimsynuqsql where SimBase='a cappella');
IF searchresult >0
{Select * from simsyn1sql where BaseID = (Select Distinct BaseID from wbsimsynuqsql where SimBase='a cappella')}
[ ELSE
{Select * from simsyn1sql where BaseID = (Select Distinct BaseID from wbsimsynuqsql where SimSyn='a cappella')} ]
END IF;
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
like inside a CREATE PROCEDURE you can use DECLARE
Your IF statement is missing a THEN
and remove the square brackets ([]) and the curly braces ({})
If your subquery returns more than one BaseID you might have to use IN instead of =
sqlfiddle

MySQL select from dynamic database and table query

Stored Proc Definition:
DECLARE dbName varchar(255);
DECLARE tableName varchar(255);
DECLARE fullPath varchar(255);
DECLARE conditions varchar(255);
SET dbName = idbname;
SET tableName = itablename;
SET fullPath = CONCAT("'",dbName,"'",'.',"'",tableName,"'");
SET checkExists = 0;
I am creating a stored proc where the dbname and tablename are dynamic, however I am stuck on the select aspect of this query.
I am trying to repalce the _test.user with values passed into the stored proc.
SELECT count(*) INTO checkExists FROM `_test`.`user` WHERE id = 1;
However this line throws an error
SELECT count(*) INTO checkExists FROM fullPath WHERE id = 1;
Error:
Procedure execution failed
1146 - Table 'dbname.fullpath' doesn't exist
I have also tried CONCAT() like this
set conditions = CONCAT('SELECT count(*) INTO ',checkExists, ' FROM ', fullPath, ' WHERE id=', 1);
However I can't figure out even how to use this in a select? Help is appreciated.
I like to do these modifications using replace(). Something like this:
replace(replace('SELECT count(*) INTO checkExists FROM `<dbname>`.`<tname>` WHERE id = 1',
'<tname>', v_tablename
), '<dbname>', v_databasename
)
You may also want to use v_fullpath somewhere. I'm not really sure what query you actually want to create.
I'm not sure why you have a variable called checkExists, when it seems to be the destination file. However, I would suggest that you prepend all your local variables with something to distinguish them from column names.

Using #variable in mysql used as command in vb.net

I've used this type of query under VB.NET 2008, but everytime I run it, it always gives me a fatal error and it's telling me to declare the #variable I've used.
Below is the sample code:
select js.year, js.week, js.rem_balance,
case when js.rem_balance = 0
then #prev_rem_balance
else js.rem_balance
end as rem_balance_zero_or_prev,
#prev_rem_balance := js.rem_balance
from test_jos_stock js
inner join (SELECT #prev_rem_balance := 0) as t
order by year,week;
You need to declare the parameter before your select statement:
DECLARE #prev_rem_balance INT; --or whatever datatype it is
Then set it to be a specific value:
SET #prev_rem_balance = 1234;

mysql declaring and setting variables. syntax errors

I thought this would be a simple task but i cannot figure out why this gives me an error in my syntax. Any help is appreciated.
DECLARE #usernameid VARCHAR(20);
declare #UserIDParam VARCHAR(20);
SET #usernameid = 'myid';
SET #UserIDParam =
(SELECT userid
FROM tblusers
WHERE unid = usernameid);
SELECT *
FROM tblusers
WHERE tblusers.userID = #useridparam
One doesn't DECLARE user variables: one just uses them. (You DECLARE local variables e.g. in a procedure).