Reputation: 2216
I have a SQL Server table created with:
IF OBJECT_ID('dbo.Messages', 'U') IS NOT NULL
DROP TABLE dbo.Messages
GO
CREATE TABLE dbo.Messages
(
Id INT NOT NULL PRIMARY KEY, -- primary key column
Username [NVARCHAR](50) NOT NULL,
MessageType [NVARCHAR](50) NOT NULL,
Recepient [NVARCHAR](50) NOT NULL,
RecepientType [NVARCHAR](50) NOT NULL,
Payload [NVARCHAR](255),
Stamp DATETIME
);
GO
When I try to insert data into the table, an error happens.
Insert statement:
INSERT INTO dbo.Messages
VALUES ('thin', 'message.broadcast', 'channelID', 'channel', 'test', '2019-07-23 15:19:08.960697828 +0300 EEST m=+14.232534538')
Column name or number of supplied values does not match table definition.
Is this happening because my datetime needs to formatted for SQL Server, or do I need to describe the primary key differently in to order to have SQL Server autogenerate values for this column?
What would be the correct way to do this?
Upvotes: 0
Views: 128
Reputation: 12014
You need to add as many columns in your insert statement as there are columns in your table, except for identity fields.
Without your Id as identity, you need this
insert into Messages (Id, Username, MessageType, Recepient, RecepientType, Payload, Stamp)
values (1, 'thin', 'message.broadcast', 'channelID', 'channel', 'test', '2019-07-23 15:19:08.960697828 +0300 EEST m=+14.232534538')
This means its up to you to determine the next value for your Id every time.
Better is to create the Id field as identity like this
Id INT IDENTITY(1, 1) PRIMARY KEY
Now you can simply do this (the Id value will be automatic incremented now)
insert into Messages (Username, MessageType, Recepient, RecepientType, Payload, Stamp)
values ('thin', 'message.broadcast', 'channelID', 'channel', 'test', '2019-07-23 15:19:08.960697828 +0300 EEST m=+14.232534538')
Using column names in your insert also has the benefit that you can put them in any order you like
Upvotes: 2
Reputation: 625
Consider change stamp column to timestamp and id to identity column like this:
IF OBJECT_ID('dbo.Messages', 'U') IS NOT NULL
DROP TABLE dbo.Messages
GO
CREATE TABLE dbo.Messages
(
Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY, -- primary key column
Username [NVARCHAR](50) NOT NULL,
MessageType [NVARCHAR](50) NOT NULL,
Recepient [NVARCHAR](50) NOT NULL,
RecepientType [NVARCHAR](50) NOT NULL,
Payload [NVARCHAR](255),
Stamp [timestamp] NOT NULL -- timestamp column
);
GO
Then, simply insert your data:
INSERT INTO dbo.Messages
(Username, MessageType, Recepient, RecepientType, Payload)
VALUES('thin', 'message.broadcast', 'channelID', 'channel', 'test')
Upvotes: 2
Reputation: 3833
Although your error clears that 6 values are passing for 7 columns.
You need to insert specifically column names.
Insert into Messages ( Username , MessageType , Recepient , RecepientType , Payload , Stamp )
values ('thin', 'message.broadcast', 'channelID', 'channel', 'test', '2019-07-23 15:19:08.960697828 +0300 EEST m=+14.232534538')
After that also you are facing an issue which is corrected by this.
If Id want to be inserted automatic, then make it idenity like Id int identity(1, 1) not null
Upvotes: 2
Reputation: 37367
You are missing ID in your insert statement.
If you want ID to be autogenerated you need to use IDENTITY(1, 1)
where you choose seed and increment step in place of 1's.
If you don't want to autogenerate ID, then you need to include it in INSERT
statements.
Column definition should be:
Id INT IDENTITY(1, 1) PRIMARY KEY
Upvotes: 4