简体   繁体   English

正确将行插入MS SQL Server

[英]Correctly insert row into MS SQL Server

I have a SQL Server table created with: 我有一个创建的SQL Server表:

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? 发生这种情况是因为我的日期时间需要为SQL Server格式化,还是为了使SQL Server自动生成此列的值而需要以不同的方式描述主键?

What would be the correct way to do this? 正确的方法是什么?

You are missing ID in your insert statement. 您在插入语句中缺少ID。

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. 如果要自动生成ID,则需要使用IDENTITY(1, 1) ,在其中选择种子和增量步长来代替1。

If you don't want to autogenerate ID, then you need to include it in INSERT statements. 如果您不想自动生成ID,则需要将其包括在INSERT语句中。

Column definition should be: 列定义应为:

Id INT IDENTITY(1, 1) PRIMARY KEY

Although your error clears that 6 values are passing for 7 columns. 尽管您的错误清除了7个列传递了6个值。

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 如果要自动插入Id,则使其与Id int identity(1, 1) not null类似Id int identity(1, 1) not null

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 没有您的ID作为身份,您需要这个

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. 这意味着由您自己来确定ID的下一个值。

Better is to create the Id field as identity like this 更好的方法是像这样将Id字段创建为身份

Id INT IDENTITY(1, 1) PRIMARY KEY

Now you can simply do this (the Id value will be automatic incremented now) 现在,您只需执行此操作(Id值将立即自动递增)

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 在插入中使用列名称的好处还在于,您可以按自己喜欢的顺序排列它们

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM