在SQL中插入新表时出错

在SQL中插入新表时出错,sql,sql-server,sql-insert,Sql,Sql Server,Sql Insert,为什么我会出错 --Q1 CREATE table Aorders ( Id INT IDENTITY NOT NULL, CreatedDate Date NOT NULL, BillingCountry varchar Not Null, MerchId int Not null, OrderStatus varchar Not null, ); --Q2 select * from Aorders --Q3 edited as suggested he

为什么我会出错

--Q1    

CREATE table  Aorders (
 Id  INT IDENTITY NOT NULL,
 CreatedDate Date NOT NULL,
 BillingCountry varchar Not Null,
 MerchId int Not null,
 OrderStatus varchar Not null,
);

--Q2    

select * from Aorders

--Q3 edited as suggested here but still get an error 
插入订单CreatedDate、BillingCountry、MerchId、OrderStatus 值“2001-01-01”、“以色列”、“5”、“批准”

执行插入时,最佳做法是明确列出所有列,并使用标准日期格式:

INSERT INTO Aorders (CreatedDate, BillingCountry, MerchId, OrderStatus)
    VALUES ('2001-01-01', 'Israel', 5 ,'Approved');

您的具体错误是因为“值”列表中有四个值,但表中有五列。

您没有正确执行查询。我想你想让ID自动递增,但你还没有声明。进行如下更改:

CREATE table Aorders ( Id INT IDENTITY NOT NULL
                      ,CreatedDate Date NOT NULL
                      ,BillingCountry varchar Not Null
                      ,MerchId int Not null
                      ,OrderStatus varchar Not null);

现在输入您插入的查询。并确保文档中的日期格式正确。

必须定义varchar列的长度,如:

CREATE table  Aorders (
 Id  INT IDENTITY NOT NULL,
 CreatedDate Date NOT NULL,
 BillingCountry varchar(100) Not Null,
 MerchId int Not null,
 OrderStatus varchar(100) Not null,
); 
发件人:

在数据定义或变量声明中未指定n时 语句,默认长度为1


您用于列CreatedDate的日期格式不正确。Gordon Linoff,您忘记包含CreatedDate列。我是代表你做的。因为第一列是identity,所以不需要在INSERT查询中包含该列。TREATED。。在AOOrders CreatedDate、BillingCountry、MerchId、OrderStatus值“01.01.01”、“Israel”、5、“Approved”中插入;仍然得到相同的错误。。。Msg 8152,级别16,状态14,第12行字符串或二进制数据将被截断。声明已终止。完成时间:2019-08-03T18:57:55.78677+03:00@pnetaly,请参见Gordon的日期格式和yours@AnkitBajpai我尝试了“2001-01-01”,仍然是相同的错误:您正在使用哪个DBMS产品?