Declare and Set not recognized syntax for my SQL server - mysql

I am running an SQL server version: 5.1.44 using XAMPP. I've been looking through SQL tutorials that use the commands DECLARE and SET.
When I run
DECLARE #iVariable INT, #vVariable VARCHAR(100), #dDateTime DATETIME
SET #iVariable = 1
It responds
#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 'DECLARE #iVariable INT, #vVariable VARCHAR(100), #dDateTime DATETIME SET #iVari' at line 1
Can anyone shed any light on why this isn't working?

I assume you use MySQL as the above error is not related to SQL Server on any levels...I also assume you don't want to create a Function or Stored Procedure but use some variables in a query window (kind of "immediate window").
In this case you don't need the "DECLARE" keyword (as this has to be used inside Stored Procedures -also has to be the very first command after the 'BEGIN' keyword-).
So you should be able to do this:
SET #iVariable = 1
or even this:
SET #iVariable = 15;
SET #iVariable = "foo"
Yes, I know what you think now (about variables and their type)... but it's MySQL, not SQL Server or Sybase ;)

Related

Repeated syntax errors while attempting to create a lookup function that finds players by ID

While I'm attempting to make a mySQL function to find a player by their ID, I'm getting repeated syntax errors no matter what I fix, and I'm unclear as to what I'm doing wrong. I've tried reading through other source materials on here to help, but anything I've tried hasn't been successful.
I've tried reading through Error while using lookup function and "MySQL syntax error" while creating a function which I don't think really pertain to my syntax errors, though I'm really new to coding functions in mysql, so I'm unsure what my problems even are.
delimiter //
DROP FUNCTION IF EXISTS findRaider//
CREATE FUNCTION findRaider(ID INT) RETURNS VARCHAR(20)
BEGIN
DECLARE NAME_FOUND VARCHAR DEFAULT " ";
SELECT name INTO NAME_FOUND FROM players WHERE player_id=ID;
RETURN NAME_FOUND;
END;//
delimiter ;
Expected: SELECT findRaider(1); RETURNS "Seranul"
Actual:
ERROR 1327(42000) Undeclared variable: NAME_FOUND
ERROR 1064(42000): 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 'RETURN NAME_FOUND' at line 1
ERROR 1064(42000): 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 'END' at line 1

MySQL function creation mysterious error [duplicate]

I am attempting to recreate a stored procedure (since I can't edit the body). I called SHOW CREATE PROCEDURE to use the same format as the original stored procedure but when I attempt to recreate it I get the following errors:
ERROR 1064 (42000): 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 '' at line 11
ERROR 1064 (42000): 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 'DECLARE organization_id BIGINT(20) UNSIGNED' at line 1
ERROR 1064 (42000): 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 'DECLARE lobby_pod_id BIGINT(20) UNSIGNED' at line 1
Here's the code:
CREATE DEFINER=`lms`#`10.0.0.%` PROCEDURE `create_organization`(
IN admin_username VARCHAR(255),
IN organization_name VARCHAR(100)
)
BEGIN
DECLARE admin_user_id BIGINT(20) UNSIGNED;
DECLARE organization_id BIGINT(20) UNSIGNED;
DECLARE lobby_pod_id BIGINT(20) UNSIGNED;
SELECT ID, account INTO admin_user_id, organization_id
FROM users
WHERE username = admin_username;
INSERT INTO pods (`title`, `description`, `owner`, `scene`)
VALUES (CONCAT(organization_name, " Village"),
CONCAT("General meeting space and hub for ", organization_name, " students and teachers."),
admin_user_id,
" Village"
);
END
I pasted into SQL Fiddle and got the same result, although pasting into MySQL Syntax Check gave me the thumbs-up. I'm sure it's a simple miss but it isn't that obvious to me.
You are missing the delimiter definition before and after the stored proc definition:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. [...] The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
Since the stored proc definition and body was ok, syntax chack gave you the thumbs up, but the code would not run properly in your client.
Use the following skeleton for defining a stored procedure:
delimiter //
create procedure ...
...
end
//
delimiter ;

Error #1064 when declaring a variable

I am trying to declare and manipulate a variable in MySQL like this:
DECLARE #tim;
//find the difference between current time and time at which the data was stored in the database
SET #tim=(select TIMEDIFF(NOW(),E.reg_date)
FROM `mytable2` E)
But I get the following error:
error:#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 'DECLARE #tim' at line 1
What is causing this error and how can I avoid it? The MySQL server version is 5.6.17.
As the error message says, your syntax is wrong.
tl;dr: SET the variable, do not DECLARE it.
DECLARE is only used when working with variables in routines such as stored procedures, functions and triggers. See the documentation:
DECLARE is permitted only inside a BEGIN ... END compound statement
and must be at its start, before any other statements.
You are trying to set a user-defined variable outside of a routine:
One way to set a user-defined variable is by issuing a SET statement:
SET #var_name = expr [, #var_name = expr] ...
Everything you need to know about this statement, including quite a bit of information about setting session and global variables, is in the documentation for SET.
select TIMEDIFF(NOW(),E.reg_date) into #tim FROM mytable2 E

Error while inserting picture into sql table

I am trying to insert a 'png ' image into an sql table field(called barchart,which is of type blob) with the below query.
INSERT INTO disease_symptom_soc(barchart) Values ((SELECT BULKColumn FROM OPENROWSET(BULK N'/home/barchartC2936861.png', SINGLE_BLOB) AS Image)) where disease_id='C2936861';
I am getting the below error.What could be the reason?
ERROR 1064 (42000): 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 '(BULK N'/home/barchart' at line 1
i guess u can use LOAD_FILE
Example:
INSERT INTO expBLOB(ID,IMAGE) VALUES(1,LOAD_FILE('/some/path/image.png'))
If you can, then change the DataType of the column to 'image'. Depending on the sql server version you are using. Otherwise if it oracle based db , you will have to create a loadfile proc and execute it as follows:
http://arjudba.blogspot.com/2008/06/how-to-insert-blob-dataimage-video-into.html
MySql OpenrowSet isn't used to copy files into the database but to query an external file (csv, xls) with a connection string, for example
OPENROWSET ('MSDASQL','mysql connection string','query')
I suggest you to load the file with a simple program made in any language (I don't know if you are developing in php, c#...) and pass it already loaded in memory to the db with a simple insert query.

MySql MVC3 EF Code First with Stored Procedure

I have a stored procedure created in MySql to return a resultset as
CREATE PROCEDURE GetCount()
BEGIN
SELECT count(*)
FROM mytable;
END$$
I am trying to call this from my MVC3 application using EF CF as:
int count = myContext.Database.SqlQuery<int>("GetCount").First();
It is falling over the call to test in my application with an error ;
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 'GetCount' at line 1
Is this not supported using MySql? Obviously the above works perfectly fine with MS Sql Server 2008. So just wondering if this is a problem with the MySql side.
Thanks
Try
int count = myContext.Database.SqlQuery<int>("CALL GetCount()").First();