I need to import some MySQL tables into my MSSQL 2019 server and keep getting null errors. "An unexpected NULL value was returned for column deployment_date"
Here is the snip from the stored procedure.
=================================
CREATE TABLE dbo.RAP_project (
id int NOT NULL ,
address text ,
network int DEFAULT '0',
loc int DEFAULT '0',
analyste int DEFAULT '0',
csr_date date NULL,
deployment_date date NULL,
=================================
I've also tried deployment_date VARCHAR(20) NULL,
Thanks
Pete
PS forgot to add the actual insert from a linked server.
SELECT * INTO dbo.RAP_project
from [MYSQL_progmgt]...[dbo.project]
Yes the linked server is [MYSQL_progmgt] and works well with the other tables on the linked server just by switching the table name.[MYSQL_progmgt]...[dbo.loc] etc for 6 others
An unexpected NULL value was returned for column "[MYSQL_progmgt]...[dbo.project].deployment_date" from OLE DB provider "MSDASQL" for linked server "MYSQL_progmgt". This column cannot be NULL. Even when I check the design it shows the field allows NULLS, so I really don't get it unless this is one of those things where so obscure seemingly unreated option whatever is causing this. All other tables work perfectly.
I get the same error.
An unexpected NULL value was returned for column "[MYSQL_progmgt]...[bire.projet].deployment_date" from OLE DB provider "MSDASQL" for linked server "MYSQL_progmgt". This column cannot be NULL.
All other table I try with the same code work perfectly.
MYSQL_progmgt]...[bire.projet].deployment_date" is the field name where the error occurs.
apparently 0000-00-00 is ok for a default value in MySQL but SQL server see the field as NOT NULL but also interprets 0000-00-00 as a NULL, which contradicts the NOT NULL. There doesn't seem to be a work around. the problem is due to the default value of 0000-00-00 being assigned to the field.
declare @booze table(dt varchar(10))
insert into @booze
select '0000-00-00' union
select '2022-02-25'
create table #wine(dt varchar(10), dt2 date)
insert into #wine
select dt as dt,
case
when try_convert(date, dt, 23 ) is null then null
else try_convert(date, dt, 23 )
end as dt2 from @booze
select * from #wine
drop table #wine
WHen I execute select * from [MYSQL_progmgt]...[bire.projet].deployment_date
I get the same error.
An unexpected NULL value was returned for column "[MYSQL_progmgt]...[bire.projet].deployment_date" from OLE DB provider "MSDASQL" for linked server "MYSQL_progmgt". This column cannot be NULL.
If I try IF(csr_date = ''0000-00-00'', ''1900-01-01'', csr_date) AS csr_date
I get Incorrect syntax near '.'.
A colleague suggested I do it like this and it works.
DECLARE @OPENQUERY nvarchar(4000), @TSQL nvarchar(4000), @TSQL_SELECT nvarchar(4000), @LinkedServer nvarchar(4000)
SET @LinkedServer = 'MYSQL_ECHEANCIER'
SET @TSQL_SELECT = 'SELECT * '
SET @OPENQUERY = ' FROM OPENQUERY('+ @LinkedServer + ','''
SET @TSQL = 'SELECT nullif( project.deployment_date, ''''0000-00-00'''') as deployment_date,
nullif( project.csr_date, ''''0000-00-00'''') as csr_date FROM dbo.project '')'
EXEC (@TSQL_SELECT+@OPENQUERY+@TSQL)
I can't see why the quadruple quotes makes it work in this configuration as quad quotes didn't work in my earlier trials.
Yes very sorry about that I should have double checked the code before submitting ,
select deployment_date from [MYSQL_progmgt]...[bire.projet] is the correct code.