Exporting null values from Matlab table to SQL Server - sql-server

I'm trying to export a Matlab table containing "null" values into a SQL Server DB, without any success. Here are my DB settings in Matlab (setdbprefs command):
NullNumberRead: 'NaN'
NullNumberWrite: 'NaN'
NullStringRead: 'null'
NullStringWrite: 'null'
And two example scripts to create the test case.
To create the DB structure:
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
To insert a row from Matlab into 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);
I get the following error, while it should be allowed by Matlab DB prefs.
Error using database/datainsert (line 301)
Unable to insert element in row 1 column 5, NaN. Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

Related

Auditing SQL Server - who is changing what and when? SOX complaince

In my organization, we are doing very limited logging or any sort to capture who is changing what and when.
I am seeking help here to understand what should be the best practices to capture any logging whatsoever happening in our SQL Server database.
I am thinking of going over the tables based on the important business uses cases that a user can perform with the application and then making an xl file with the following fields so that I keep this file as a reference for myself.
My question: is there any other better way to capture the current change in the database, and is there a way in SQL Server that I use to find out if we are capturing any logging in the database?
We don't have any CDC implementation or C2 audit tracing enables or change tacking enabled.
Management want's to leverage the data captured in the database tables.
I am working on a similar project, you can use below design, i am explaining with student subject example
CREATE TABLE [dbo].[AudRel](
[AudId] [int] IDENTITY(1,1) NOT NULL,
[AudTableName] [varchar](100) NULL,
[AudFieldName] [varchar](100) NULL,
[AudFieldID] [varchar](30) NULL,
CONSTRAINT [PK_AuditRel] PRIMARY KEY CLUSTERED
(
[AudId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Student](
[StudentID] [int] IDENTITY(1,1) NOT NULL,
[StudentName] [varchar](100) NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[StudentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Student_Audit](
[ID] [int] IDENTITY(1,1) NOT NULL,
[StudentID] [int] NOT NULL,
[StudentName] [varchar](100) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[StudentSubject](
[SSID] [int] IDENTITY(1,1) NOT NULL,
[StudentID] [int] NULL,
[SubjectID] [int] NULL,
CONSTRAINT [PK_StudentSubject] PRIMARY KEY CLUSTERED
(
[SSID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[StudentSubject_Audit](
[ID] [int] IDENTITY(1,1) NOT NULL,
[SSID] [int] NOT NULL,
[StudentID] [int] NULL,
[SubjectID] [int] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Subject](
[SubjectID] [int] IDENTITY(1,1) NOT NULL,
[SubjectName] [varchar](50) NULL,
CONSTRAINT [PK_Subject] PRIMARY KEY CLUSTERED
(
[SubjectID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Subject_Audit](
[ID] [int] IDENTITY(1,1) NOT NULL,
[SubjectID] [int] NOT NULL,
[SubjectName] [varchar](50) NULL
) ON [PRIMARY]
SET IDENTITY_INSERT [dbo].[AudRel] ON
INSERT [dbo].[AudRel] ([AudId], [AudTableName], [AudFieldName], [AudFieldID]) VALUES (1, N'Student', N'StudentName', N'StudentID')
INSERT [dbo].[AudRel] ([AudId], [AudTableName], [AudFieldName], [AudFieldID]) VALUES (2, N'Subject', N'SubjectName', N'SubjectID')
SET IDENTITY_INSERT [dbo].[AudRel] OFF
SET IDENTITY_INSERT [dbo].[Student] ON
INSERT [dbo].[Student] ([StudentID], [StudentName]) VALUES (1, N'Alex')
INSERT [dbo].[Student] ([StudentID], [StudentName]) VALUES (2, N'DSouza')
SET IDENTITY_INSERT [dbo].[Student] OFF
SET IDENTITY_INSERT [dbo].[StudentSubject] ON
INSERT [dbo].[StudentSubject] ([SSID], [StudentID], [SubjectID]) VALUES (1, 1, 1)
INSERT [dbo].[StudentSubject] ([SSID], [StudentID], [SubjectID]) VALUES (2, 2, 1)
INSERT [dbo].[StudentSubject] ([SSID], [StudentID], [SubjectID]) VALUES (3, 2, 2)
SET IDENTITY_INSERT [dbo].[StudentSubject] OFF
SET IDENTITY_INSERT [dbo].[Subject] ON
INSERT [dbo].[Subject] ([SubjectID], [SubjectName]) VALUES (1, N'English')
INSERT [dbo].[Subject] ([SubjectID], [SubjectName]) VALUES (2, N'Mathematics')
SET IDENTITY_INSERT [dbo].[Subject] OFF
and then use below query to dynamically fetch fields have been changed. From the UI you need to pass the AudRelID
DECLARE #TableName VARCHAR(100),#FieldName VARCHAR(100),#FieldID VARCHAR(100)
SELECT #TableName = [AudTableName]
, #FieldName=[AudFieldName]
, #FieldID=[AudFieldID]
FROM [dbo].[AudRel] WHERE [AudId] = 1 -- (Ex : StudentHistory)
DECLARE #SQL NVARCHAR(MAX) = N'
SELECT ID,' + #FieldID +
',' + #FieldName + ' FROM ' + #TableName + '_Audit ' + ' WHERE ' + #FieldID + ' = '
+ Convert(varchar(20),#FieldID)
print #SQL
EXECUTE sp_executesql #SQL

How to create a table from a schema

I have a table called tbRep in my database
I have 3 schemas on the the SQL Server database
X, Y, Z
now X & Y have tables called X.tbRep & Y.tbRep
However, when I try use the CREATE To script from e.g. X.tbRep and try to create one for the new schema Z, it throws an error saying,
Msg 2714, Level 16, State 6, Line 2
There is already an object named 'tbRep' in the database.
What am I doing wrong here?
I'm sure that there are Z.tbRep doesn't exist
CREATE to script
USE [Info]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [Z].[tbRep](
[ReplicaGroup] [varchar](50) NOT NULL,
[RunFrequencyUnit] [char](1) NOT NULL,
[Enabled] [bit] NOT NULL,
[LastRun] [datetime] NOT NULL,
[RunFrequency] [int] NOT NULL,
[ReplicationWindowType] [char](1) NOT NULL,
[ReplicationWindowSize] [tinyint] NOT NULL,
CONSTRAINT [PK_tbReplicaGroups] PRIMARY KEY CLUSTERED
(
[ReplicaGroup] 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
ALTER TABLE [Z].[tbReplicaGroups] ADD CONSTRAINT [DF_tbReplicaGroups_ReplicationWindowType] DEFAULT ('D') FOR [ReplicationWindowType]
GO
ALTER TABLE [Z].[tbReplicaGroups] ADD CONSTRAINT [DF_tbReplicaGroups_ReplicationWindowSize] DEFAULT ((1)) FOR [ReplicationWindowSize]
GO
--Checks to see if the table doesn't already exist, if not add your creation script
if not exists (select name from sys.tables where name = 'tpRep')
BEGIN
--Table Creation code goes here
END
Can you execute this before CREATE TABLE statement and let us know if you are getting anything?
select *
from sys.all_objects
where sys.all_objects.name = 'tbRep' and
sys.all_objects.type = 'U' and
sys.all_objects.schema_id = (select schema_id from sys.schemas where sys.schemas.name = 'Z')

SQL Server strange duplicate key error

In SQL Server 2008 R2, I have a simple table with the following columns definitions:
Id (PK ,int , not null)
MeterId (FK , int ,not null)
InstallDate(DateTime, not null)
Image(NVarCharMax, null)
Number (int , not null)
Comments(NVarChar(300), null)
And Id column is set as Identity .
When I run:
insert into Transmitters (MeterId, Number, InstallDate)
values (952, 777 , '2013-02-21')
I get the duplicate key error.
There is no other transmitter with id 777 ,
There is a meter with id 952.
This started happening in more than 1 table of my DB.
Any suggestions would be most appriceated.
The entire table script is:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Transmitters](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MeterId] [int] NOT NULL,
[InstallDate] [datetime] NOT NULL,
[Image] [nvarchar](max) NULL,
[Number] [int] NOT NULL,
[Comments] [nvarchar](300) NULL,
CONSTRAINT [PK_Transmitters] PRIMARY KEY CLUSTERED
(
[Id] 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
ALTER TABLE [dbo].[Transmitters] WITH CHECK ADD CONSTRAINT [FK_Transmitter_Meter] FOREIGN KEY([MeterId])
REFERENCES [dbo].[Meter] ([Id])
GO
ALTER TABLE [dbo].[Transmitters] CHECK CONSTRAINT [FK_Transmitter_Meter]
GO
Ok , thanks guys you've been most helpful.
I've checked my identity current value using:
USE Name_Of_The_DB
GO
DBCC CHECKIDENT (‘Name_Of_The_Table’)
GO
It was indeed the cause of the problem that some rows had higher Id values than
the current identity.
Thand I inserted a row to update the current identity value:
set identity_insert YourTable ON
--Insert my row
set identity_insert YourTable OFF
Thanks :)

debug foreign key constraint error

I have a Sql Server db that was generated by Nhibernate's schema tool and hitting the error below when I try to enter a known PartyId, and I just do not get the error. (Even tho the log output I'm showing is from a test run using NHibernate, I can replicate the error manually using the SQL Server db that NHib generated)
I think the FK constraint is saying the PartyId must exist first, but like I say - it does.
I have SQL Server 2008 management studio but I rarely use it, preferring to access it through Visual Studio on those rare times I need to. So I have two basic questions
how do I see the ddl in sql server from visual studio?
how do I debug and fix my FK constraint problem?
DDL generated by NHib
create table PartyNames (
PartyNameId INTEGER not null,
PartyId INTEGER not null,
RequiredName TEXT not null,
EverythingElse TEXT,
ContextUsed TEXT,
Salutation TEXT,
EffectiveStart DATETIME,
EffectiveEnd DATETIME,
primary key (PartyNameId)
)
create table Parties (
PartyId INTEGER not null,
Type TEXT not null,
primary key (PartyId)
)
The ERROR
NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('PERSON', #p0);#p0 = 98304 [Type: Int32 (0)]
NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('PERSON', #p0);#p0 = 98305 [Type: Int32 (0)]
NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('PERSON', #p0);#p0 = 98306 [Type: Int32 (0)]
NHibernate: select next_hi from hibernate_unique_key with (updlock, rowlock)
NHibernate: update hibernate_unique_key set next_hi = #p0 where next_hi = #p1;#p0 = 5 [Type: Int32 (0)], #p1 = 4 [Type: Int32 (0)]
NHibernate: INSERT INTO
PartyNames (PartyId, RequiredName, EverythingElse, ContextUsed, Salutation, EffectiveStart, EffectiveEnd, PartyNameId)
VALUES (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7);
#p0 = 98304 [Type: Int32 (0)],
#p1 = 'Hesh' [Type: String (50)],
#p2 = 'Berryl;;;' [Type: String (4000)],
#p3 = 'Stack Overflow Profile' [Type: String (50)],
#p4 = 'Fellow Geek' [Type: String (20)],
#p5 = 7/29/2011 4:55:19 PM [Type: DateTime (0)],
#p6 = 12/31/9999 11:59:59 PM [Type: DateTime (0)],
#p7 = 131072 [Type: Int32 (0)]
Test 'Parties.Data.Impl.NHib.Tests.TestFixtures.BaseDataTestFixtures.SqlServerCommandExecutor.GenerateTestData' failed: NHibernate.Exceptions.GenericADOException : could not insert: [Parties.Domain.Names.PartyName#131072][SQL: INSERT INTO PartyNames (PartyId, RequiredName, EverythingElse, ContextUsed, Salutation, EffectiveStart, EffectiveEnd, PartyNameId) VALUES (?, ?, ?, ?, ?, ?, ?, ?)]
----> System.Data.SqlClient.SqlException :
The INSERT statement conflicted with the FOREIGN KEY constraint "Party_PartyName_FK".
The conflict occurred in database "PartyDomainDb", table "dbo.Parties", column 'PartyId'.
Create Scripts
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PartyNames](
[PartyNameId] [int] NOT NULL,
[PartyId] [int] NOT NULL,
[RequiredName] [nvarchar](50) NOT NULL,
[EverythingElse] [nvarchar](255) NULL,
[ContextUsed] [nvarchar](50) NULL,
[Salutation] [nvarchar](20) NULL,
[EffectiveStart] [datetime] NULL,
[EffectiveEnd] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[PartyNameId] 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
ALTER TABLE [dbo].[PartyNames] WITH CHECK ADD CONSTRAINT [Party_FK] FOREIGN KEY([PartyId])
REFERENCES [dbo].[Parties] ([PartyId])
GO
ALTER TABLE [dbo].[PartyNames] CHECK CONSTRAINT [Party_FK]
GO
ALTER TABLE [dbo].[PartyNames] WITH CHECK ADD CONSTRAINT [Party_PartyName_FK] FOREIGN KEY([PartyNameId])
REFERENCES [dbo].[Parties] ([PartyId])
GO
ALTER TABLE [dbo].[PartyNames] CHECK CONSTRAINT [Party_PartyName_FK]
GO
USE [PartyDomainDb]
GO
/****** Object: Table [dbo].[Parties] Script Date: 07/29/2011 18:22:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Parties](
[PartyId] [int] NOT NULL,
[Type] [nvarchar](255) NOT NULL,
PRIMARY KEY CLUSTERED
(
[PartyId] 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
The foreign key is probably not defined correctly. This is the current definition.
ALTER TABLE [dbo].[PartyNames] WITH CHECK ADD CONSTRAINT [Party_PartyName_FK] FOREIGN KEY([PartyNameId])REFERENCES [dbo].[Parties] ([PartyId])
It should probably be as follows.
ALTER TABLE [dbo].[PartyNames] WITH CHECK ADD CONSTRAINT [Party_PartyName_FK] FOREIGN KEY([PartyId])REFERENCES [dbo].[Parties] ([PartyId])
Note that I have swapped PartyNameId for PartyId.

DDL not executing properly inside of an IF NOT EXISTS statement on SQL Server 2005

I have a fairly complex database that needs to be deployed to a variety of servers which may or may not potentially have existing parts of the DB already implemented on it. To work around this contingency I have setup the following test:
USE [testDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[wcSites]') AND type in (N'U'))
begin
CREATE TABLE [dbo].[wcSites](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[siteCSS] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[masterTemplate] [int] NULL,
[errorPage] [int] NULL,
[homePage] [int] NULL,
[addressProduction] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[addressTest] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[routeHandler] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[publish] [bit] NOT NULL CONSTRAINT [DF_wcSites_publish] DEFAULT ((0)),
[publicAccess] [bit] NOT NULL CONSTRAINT [DF_wcSites_publicAccess] DEFAULT ((1)),
[siteAccessPermission] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[contentOwner] [int] NULL,
[navStyle] [int] NULL,
[incScripts] [int] NULL,
[boxW] [int] NULL,
[boxH] [int] NULL,
[columns] [int] NULL,
[rows] [int] NULL,
CONSTRAINT [PK_wcSites] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_wcSites_Unique_Address] UNIQUE NONCLUSTERED
(
[addressProduction] ASC,
[addressTest] 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
EXEC sys.sp_addextendedproperty #name=N'MS_Description', #value=N'Ensure unique addresses in the addressProduction, addressTest fields' , #level0type=N'SCHEMA',#level0name=N'dbo', #level1type=N'TABLE',#level1name=N'wcSites', #level2type=N'CONSTRAINT',#level2name=N'IX_wcSites_Unique_Address'
end
And the end result is always:
Msg 102, Level 15, State 1, Line 32
Incorrect syntax near 'PRIMARY'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'end'.
If I test the IF statement, it works correctly:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[wcSites]') AND type in (N'U'))
begin
PRINT 'Table does not exist'
end
And likewise when I test the CREATE TABLE script. But when I put the CREATE TABLE script inside of the begin..end block it fails every time. And I have the problem across the board, almost every table that uses this method fails.
Try removing the 'go' statements within the begin..end block, see if that helps.
I'm a bit hazy on the SQL Server syntax but doesn't the GO statement try to execute the Block? What happens if you replace the go with a semi-colon?
Also, the Set Ansi_padding off being in the If block while the set Ansi_padding on is outside looks odd to me, are you sure thats correct?

Resources