I set a variable in MySQL and in the next SELECT query it is NULL.
SET #test = 123;
SELECT #test;
When executing the SET statement, there is no error.
Do I have to enable/activate the usage of variables?
I run MySQL Server 4.1.
Related
I'm trying to import tables from a MYSQL database via a linked server. The MYSQL database has all the date fields set to a default of '0000-00-00'. I can't even list the contents via a stored procedure as I get the following error.
An unexpected NULL value was returned for column "[MYSQL_progmgt]...[dbo.project].date_completed" from OLE DB provider "MSDASQL" for linked server "MYSQL_progmgt". This column cannot be NULL.
If I use any other table that doesn't have dates everything works fine.
I also need to import tables (that contain dates) from another MYSQL database via a linked server as well and have no problems as the default date fields are left as NULL values.
My stored procedure is
USE [TEST_COPY_PETER]
GO
/****** Object: StoredProcedure [dbo].[RAP_weekly] Script Date: 2/25/2022 10:57:03 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[RAP_weekly]
AS
Select * from [MYSQL_progmgr]...[dbo.project]
I've also tried to retrieve a single field for clarity with
Select NULLIF(date_completed,'1901-01-01') as date from [MYSQL_progmgr]...[dbo.project]
Select COALESCE(date_completed,'1901-01-01') as date from [MYSQL_progmgr]...[dbo.project]
Can't figure it out.
My sql server is version 2019 we are using MYSQL 5.3 ODBC driver.
The table on the MySQL side has the date fields set as
deployment_date` date NOT NULL DEFAULT '0000-00-00'
table on the SQL server side is not created yet as I can't even read the MySQL table.
Pete
here is the modified working code.note the quadruple single quotes.
DECLARE #OPENQUERY nvarchar(4000), #TSQL nvarchar(4000), #TSQL_SELECT nvarchar(4000), #LinkedServer nvarchar(4000)
SET #LinkedServer = 'MYSQL_ECHEANCIER'
SET #OPENQUERY = 'Select nullif( project.deployment_date, ''''0000-00-00'''') as deployment_date FROM OPENQUERY('+ #LinkedServer + ','''
SET #TSQL = 'SELECT * from dbo.project'')'
EXEC (#OPENQUERY+#TSQL)
If a column does not accept NULL value; the appreciate solution is two handle the NULL value all the time. In your case use the minimum value of datatime which is 1753-01-01.
ISNULL(date_completed, '1753-01-01')
OR
ISNULL(date_completed, Cast('1753-01-01' as DateTime))
I am moving from SQL Server to mySQL.
MSSQL I used to write below and execute them in one go.
Declare #d datetime
Select #d='2021-02-01'
Select * from tbl where createdt>#d
This can be executed without stored procedure
Mysql:
Declare d datetime
Select d='2021-02-01'
Select * from tbl where createdt>d
How do I do this in mysql work bench? It complains syntax error in DECLARE.
I am confused about the syntax.
You can use # in MySQL too, to use a user-defined variable.
You can do it as following without using any standard procedure or functions.
set #d='2021-02-01';
select * from tbl_name where createdt> #d;
You don't need to use declare clause here.
You can refer here: Syntax reference
I'm using Navicat to manage a MySql database and I want to store a query result into a variable. I tried these two options and none of them is working, but they do work if used directly from MySql console.
First syntax:
SET #var =(SELECT id FROM client where `name` = "gerrard");
SELECT #var;
Second syntax:
SELECT id INTO #var FROM client where `name` = "gerrard";
SELECT #var;
Both of them shows null in the output.
I tried setting a variable in sql as follows:
DECLARE #fromDate VARCHAR(60);
SET #fromDate = '2013-01-01 00:00:00';
SET #toDate = '2013-02-01 00:00:00';
SELECT #fromDate;
but this is not working.
what am I doing incorrectly?
You don't DECLARE variables that start with #.
MySQL has two different types of variables. One is a session variable, with the # prefix. The other type is the local variable inside a trigger or stored proc.
The DECLARE statement is valid only inside of body of stored procedure or function, and this variables don't start by #.
The variables that start with # don't need DECLARE, just use outside of stored procedure inclusive.
First lets take a look at how can we define a variable in mysql
To define a varible in mysql it should start with '#' like #{variable_name} and this '{variable_name}', we can replace it with our variable name.
Now, how to assign a value in a variable in mysql. For this we have many ways to do that
Using keyword 'SET'.
Example :- mysql > SET #a = 1;
Without using keyword 'SET' and using ':='.
Example:- mysql > #a:=1;
By using 'SELECT' statement.
Example:- mysql > select 1 into #a;
Here #a is user defined variable and 1 is going to be assigned in #a.
Now how to get or select the value of #{variable_name}.
we can use select statement like
Example :-
mysql > select #a;
it will show the output and show the value of #a.
Now how to assign a value from a table in a variable.
For this we can use two statement like :-
#a := (select emp_name from employee where emp_id = 1);
select emp_name into #a from employee where emp_id = 1;
Always be careful emp_name must return single value otherwise it will throw you a error in this type statements.
refer this:- http://www.easysolutionweb.com/sql-pl-sql/how-to-assign-a-value-in-a-variable-in-mysql
I'm getting an error in the following OPENQUERY statement that I'm trying to execute against a MySql database from SQL Server.
UPDATE OPENQUERY(MYWPDB, 'SELECT total FROM wp_tt WHERE id = 112121') SET total = 1
The error is "Key column information is insufficient or incorrect. Too many rows were affected by update".
The statement should be updating the 'total' field to the value of '1'. It's an integer field and 'id' is the primary key on the table. I'm using SQL Server 2000.
I had the same issue with an openquery that updates iSeries. My openquery is within a cursor also.
The fix is to include the key columns in the select.
So in your case it would be something like this:
UPDATE OPENQUERY(MYWPDB, 'SELECT key1, key2, total FROM wp_tt WHERE id = 112121') SET total = 1
Turns out there's nothing wrong with the query. I was trying to execute the statement inside a cursor operation inside a stored procedure. I tested it outside the cursor operation and it processed fine.
However, since I still needed it to work within the cursor, I had to keep digging, and finally found the four-part syntax would do the trick. So the query instead became:
UPDATE MYWPDB...wp_tt SET total = 1 WHERE id = 112121