将空值从Matlab表导出到SQL Server

时间:2016-02-11 09:09:22

标签: sql-server database matlab null

我试图导出一个包含" null"的Matlab表。将值赋入SQL Server数据库,但没有任何成功。以下是我在Matlab中的数据库设置(setdbprefs命令):

          NullNumberRead: 'NaN'
         NullNumberWrite: 'NaN'
          NullStringRead: 'null'
         NullStringWrite: 'null'

用于创建测试用例的两个示例脚本。 要创建数据库结构:

USE [master]
GO

CREATE DATABASE [TestDB]
GO

USE [TestDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[TestTable](
    [Field1] [int] IDENTITY(1,1) NOT NULL,
    [Field2] [smallint] NULL,
    [Field3] [int] NULL,
    [Field4] [varchar](10) NULL,
    [Field5] [datetime] NULL,
    [Field6] [datetime] NULL,
    [Field7] [datetime] NULL CONSTRAINT [CreatedOn_DF]  DEFAULT     (getdate()),
 CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED 
(
    [Field1] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =     OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

从Matlab向DB中插入一行:

% Connect to MS SQL Server Database
conn = database('TestDB','','','Vendor','Microsoft SQL Server',...
    'Server','localhost','AuthType','Windows',...
    'PortNumber',1433);

% Build record to Export (note that Field1 and Field7 are missing because
% are already generated by destination server)
R = struct('Field2', 1,...
    'Field3', 10,...
    'Field4', 'abc',...
    'Field5', '2016-01-01 00:00:00',...
    'Field6', NaN);

% Transform to Table
R = struct2table(R);

% Export to Database
datainsert(conn, '[TestDB].[dbo].[TestTable]',     R.Properties.VariableNames, R);

我收到以下错误,而Matlab DB prefs应该允许它。

  

使用database / datainsert时出错(第301行)
      无法在第1行第5列NaN中插入元素。时间戳格式必须为yyyy-mm-dd hh:mm:ss [.fffffffff]

0 个答案:

没有答案