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

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;

Related

How Can I Create a MySQL Function to Check JSON Validity?

I'm fairly new to MySQL but I'd like to create a function to validate a JSON objects that are stored in my database tables.
I looked up information on creating a function, but must be missing something as I can't seem to get it to work. It doesn't seem like it would be overly complicated but perhaps I'm not using the appropriate syntax.
Here is my code:
DELIMITER //
CREATE FUNCTION CHECKJSON( DB_NAME varchar(255), TABLE_NAME varchar(255), JSON_COLUMN varchar(255))
RETURNS varchar(300)
BEGIN
DECLARE notNullCount int;
DECLARE validJSONCount int;
DECLARE result varchar(300);
SET notNullCount = (SELECT count(*) FROM DB_NAME.TABLE_NAME WHERE JSON_COLUMN IS NOT NULL);
set validJSONCount = (SELECT count(*) FROM DB_NAME.TABLE_NAME WHERE JSON_VALID(JSON_COLUMN) > 0);
CASE
WHEN (validJSONCount = notNullCount) THEN
SET result = CONCAT('VALID JSON COUNT: ', validJSONCount)
ELSE
SET result = CONCAT('INVALID JSON COUNT: ', (notNullCount - validJSONCount))
END;
RETURN result;
END //
DELIMITER ;
When I try to run this code, I get the following error message:
"Error Code: 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 'ELSE SET result = CONCAT('INVALID JSON COUNT: ', (notNullCount - validJSONC' at line 14"
Any thoughts on how I might improve this code? Thanks!
Since MySQL 5.7 you have a pretty and simple function for this:
JSON_VALID(value)
Returns 0 or 1 to indicate whether a value is valid JSON. Returns NULL if the argument is NULL.
https://dev.mysql.com/doc/refman/5.7/en/json-attribute-functions.html#function_json-valid
You're missing a couple of ; and to end the case it should be END CASE.
DELIMITER //
CREATE FUNCTION CHECKJSON( DB_NAME varchar(255), TABLE_NAME varchar(255), JSON_COLUMN varchar(255))
RETURNS varchar(300)
BEGIN
DECLARE notNullCount int;
DECLARE validJSONCount int;
DECLARE result varchar(300);
SET notNullCount = (SELECT count(*) FROM DB_NAME.TABLE_NAME WHERE JSON_COLUMN IS NOT NULL);
set validJSONCount = (SELECT count(*) FROM DB_NAME.TABLE_NAME WHERE JSON_VALID(JSON_COLUMN) > 0);
CASE
WHEN (validJSONCount = notNullCount) THEN
SET result = CONCAT('VALID JSON COUNT: ', validJSONCount) ;
ELSE
SET result = CONCAT('INVALID JSON COUNT: ', (notNullCount - validJSONCount)) ;
END CASE;
RETURN result;
END //
DELIMITER ;

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.

SQL Server not accepting DECLARE in INTO Statement

I'm somewhat new to SQL Server and am trying to use a while loop inside of an INTO statement, but for some reason SQL Server is not letting me DECLARE my variable inside of the INTO statement... seems to be okay w/ the while loop though, but I need to be able to DECLARE and define my variable to use in the while loop...any ideas?
--Calculations portion of query
SELECT
FNL.*
INTO
#Final_Calc
FROM
(
DECLARE #i INT
SET #i = 0
WHILE (#StartDte + #i <= #EndDte)
BEGIN
SET #i = #i + 1
SELECT
YEAR(ACD.WorkDte) AS WorkDte,
'LOB - ALL ACM' AS RptType,
'Month' AS RptLvl
FROM
#FinalResults ACD
WHERE
ACD.WorkDte BETWEEN #StartDte AND #StartDte + #i
GROUP BY
Year(ACD.WorkDte), ACD.LOB
END
GO) fnl
So, it is not liking DECLARE #i INT portion...

SELECT INTO local variable undeclared?

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.

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).