从 MYSQL 链接服务器上的表中选择 * 返回列不能为 NULL 错误 MYSQL 将日期字段设置为默认值“0000-00-00”
我正在尝试通过链接服务器从 MYSQL 数据库导入表。 MYSQL 数据库将所有日期字段设置为默认值“0000-00-00”。我什至无法通过存储过程列出内容,因为出现以下错误。 从链接服务器“MYSQL_progmgt”的 OLE DB 提供程序“MSDASQL”中为列“[MYSQL_progmgt]...[dbo.project].date_completed”返回了意外的 NULL 值。该列不能为 NULL。
如果我使用任何其他没有日期的表,一切都会正常。 我还需要通过链接服务器从另一个 MYSQL 数据库导入表(包含日期),并且没有问题,因为默认日期字段保留为 NULL 值。
我的存储过程是
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]
为了清楚起见,我还尝试检索单个字段 选择 NULLIF(date_completed,'1901-01-01') 作为 [MYSQL_progmgr]...[dbo.project]
中的日期 选择 COALESCE(date_completed,'1901-01-01') 作为 [MYSQL_progmgr]...[ 中的日期dbo.project]
无法弄清楚。
我的 sql server 是 2019 版本,我们使用 MYSQL 5.3 ODBC 驱动程序。
MySQL端的表的日期字段设置为 SQL Server 端的deployment_date` date NOT NULL DEFAULT '0000-00-00'
表尚未创建,因为我什至无法读取MySQL 表。
皮特 这是修改后的工作代码。请注意四个单引号。
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果某列不接受 NULL 值;比较好的解决方案是始终处理 NULL 值。在您的情况下,使用数据时间的最小值,即 1753-01-01。
或者
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.
OR