错误:
消息 257,级别 16,状态 3,第 22 行
不允许从数据类型 datetime 到 int 的隐式转换。 使用 CONVERT 函数运行此查询。
当我尝试执行以下操作时(在 SSMS 中)出现。 注意:这不在存储过程中。
/******The original table elements ******/
CREATE TABLE [dbo].[UserAccounts]
(
[Id] int IDENTITY(1,1) NOT NULL,
[Username] nvarchar(max) NOT NULL,
[FullName] nvarchar(max) NOT NULL,
[Email] nvarchar(max) NOT NULL,
[Password] nvarchar(max) NOT NULL,
[IsLoggedIn] bit NOT NULL,
[LastLogin] datetime NOT NULL,
[LoginCount] int NOT NULL,
[VerifyPending] bit NOT NULL,
[FilterPreference] int NOT NULL,
[LikesCommitted] int NOT NULL,
[CommentsCommitted] int NOT NULL,
[LoginType] int NOT NULL,
[BlogContainerBlogId] int NOT NULL,
[ValidationKey] nvarchar(max) NULL,
[ValidationStartTime] datetime NULL,
[Validated] bit NULL
);
/* I then copy the data into a backup table */
select *
into UserAcctBackup
from UserAccounts
/* (verified content to be correct) */
/* Added an element to the table (drop, recreate) */
CREATE TABLE [dbo].[UserAccounts]
(
[Id] int IDENTITY(1,1) NOT NULL,
[Username] nvarchar(max) NOT NULL,
[FullName] nvarchar(max) NOT NULL,
[Email] nvarchar(max) NOT NULL,
[Password] nvarchar(max) NOT NULL,
[IsLoggedIn] bit NOT NULL,
[LastLogin] datetime NOT NULL,
[LoginCount] int NOT NULL,
[VerifyPending] bit NOT NULL,
[FilterPreference] int NOT NULL,
[LikesCommitted] int NOT NULL,
[CommentsCommitted] int NOT NULL,
[LoginType] int NOT NULL,
[BlogContainerBlogId] int NOT NULL,
[ValidationKey] nvarchar(max) NULL,
[ValidationStartTime] datetime NULL,
[Validated] bit NULL,
[AllowPromotions] bit NOT NULL <--- Added this
);
/* Then, attempt to restore the table content from backup */
insert into UserAccounts
select *
from UserAcctBackup
我得到这个错误:
消息 257,级别 16,状态 3,第 22 行
不允许从数据类型 datetime 到 int 的隐式转换。使用 CONVERT 函数运行此查询。
如果/当我向表中简单添加标量数据项时(没有 FK mucky-muck 等),我只是希望将此作为备份/恢复一个表的一种快速而肮脏的方式。我不明白这个错误的原因,也不明白如何确定两个日期时间元素中的哪一个 might 有问题。
感谢任何见解!
你在表中添加了一列,现在它的结构与备份表不同,所以你不能只
select *
进去。
您通常需要枚举列(无论如何这是一个很好的做法)来阐明您的意图。此外,如果您添加的列不可为空且没有默认值,则需要为其赋值。
例如,对于包含列
(id, username, fullname, email)
的表,您可以在其中添加一个名为 bit
的不可空 allowPromotions
列:
insert into UserAccounts (id, username, fullname, email, allowPromotions)
select id, username, fullname, email, 1
from UserAcctBackup
如果新列可以为空并且我们满足于保留它
null
,我们可以逃脱:
insert into UserAccounts (id, username, fullname, email)
select id, username, fullname, email
from UserAcctBackup