简体   繁体   English

SQL Server 2005:插入查询返回操作数类型冲突:ntext与smallint不兼容

[英]SQL Server 2005: Insert query returning Operand type clash: ntext is incompatible with smallint

I have what I hope is a quick question. 我有我希望的快速问题。 Haven't found working code for this, and I'm at a little bit of a loss. 没有为此找到工作代码,我有点不知所措。

I'm working with an old database on SQL Server 2005. It has a dynamic questionnaire (or a thousand, actually... long story), and each question can have the answer stored as either a smallint or as ntext. 我正在使用SQL Server 2005上的旧数据库。它有一个动态调查问卷(或一千个,实际上......长篇故事),每个问题都可以将答案存储为smallint或ntext。

The current "answers" table is as follows: 目前的“答案”表如下:

AnswerID (PK, int, not null)
EvalID (int, null)
QuestionID (int, null)
NumericAnswer (smallint, null)
TextAnswer (ntext, null)

(Yes, I know ntext is deprecated. It's an old table.) (是的,我知道ntext已被弃用。这是一张旧桌子。)

This table now has over 300,000 records in it, and is getting really large. 该表现在有超过300,000条记录,并且变得非常大。 So I'm looking for more efficient ways to store the data, and decided to do a little experiment with moving everything to a single sql_variant field (currently, each row can have data in just one of the two answer columns, not both). 所以我正在寻找更有效的方法来存储数据,并决定做一些实验,将所有内容移动到单个sql_variant字段(目前,每行只能在两个答案列中的一个中包含数据,而不是两者都有)。

So I made another table, called AnswersTest : 所以我创建了另一个名为AnswersTest表:

AnswerID (PK, int, not null)
EvalID (int, null)
QuestionID (int, null)
AnswerGiven (sql_variant, null)

... and I'm now trying to get all of the existing data from Answers to AnswersTest , preferably in their original order... so I'm running an insert query, and it's not working. ...而我现在正试图从AnswersAnswersTest获取所有现有数据,最好是按照原始顺序...所以我正在运行一个插入查询,但它不起作用。 I've tried both the following: 我尝试了以下两种方法:

INSERT INTO AnswersTest (EvalID, QuestionID, AnswerGiven)
SELECT EvalID, QuestionID, ISNULL(NumericAnswer,TextAnswer) AS AnswerGiven FROM Answers ORDER BY AnswerID

...and ...和

INSERT INTO AnswersTest (EvalID, QuestionID, AnswerGiven)
SELECT EvalID, QuestionID, CONVERT(ntext,CASE WHEN NumericAnswer IS NULL THEN TextAnswer ELSE NumericAnswer END) AS AnswerGiven FROM Answers ORDER BY AnswerID

In both cases, I get the following: Operand type clash: ntext is incompatible with smallint 在这两种情况下,我得到以下内容: Operand type clash: ntext is incompatible with smallint

I know there must be a simple answer to this... I'm just drawing a blank (it's been a couple of months since I've had to do anything much with TSQL), and I can't find a working answer through searching. 我知道必须有一个简单的答案......我只是画了一个空白(因为我必须用TSQL做很多事情已经过了几个月),我找不到一个可行的答案搜索。 Please help. 请帮忙。 :) :)

EDIT: Found an answer through trial and error... 编辑:通过反复试验找到答案......

The answer may not have been the best one (and it may jump through some unnecessary hoops), but it's a one-time query and it works. 答案可能不是最好的答案(它可能会跳过一些不必要的箍),但它是一次性查询,它可以工作。 :) :)

INSERT INTO AnswersTest (EvalID, QuestionID, AnswerGiven)
SELECT EvalID, QuestionID, CASE WHEN CONVERT(sql_variant,NumericAnswer) IS NULL THEN CONVERT(sql_variant,CONVERT(varchar(8000),TextAnswer)) ELSE CONVERT(sql_variant,NumericAnswer) END AS AnswerGiven FROM Answers ORDER BY AnswerID

(Note: I tried varchar(max) , but that wasn't allowed. The answers weren't all that long, though... nobody writes essays in these surveys... so I just changed it to varchar(8000) , and that worked). (注意:我尝试了varchar(max) ,但这是不允许的。答案并不那么长,但是......没有人在这些调查中写论文......所以我只是把它改成了varchar(8000) ,这工作)。

Thanks for reading, and sorry to take up your time! 感谢阅读,抱歉占用你的时间! :) :)

Faced Similar Error when I accidentally gave string values to column defined as int. 当我意外地将字符串值赋予定义为int的列时面临类似的错误。 Please rearrange the values and Table fields in the insert statement For ex 请重新排列插入语句中的值和表字段

Insert into Table (int,string) values (string,int)

will not work and generate the error as stated above. 将不起作用并产生如上所述的错误。 So re arrange it in respective manner. 所以重新安排各自的方式。

 Insert into Table (int,string) values (int,string)

This link describes the difference between ntext and nvarchar(max). 链接描述了ntext和nvarchar(max)之间的区别。 Is does mention that the ntext is being deprecated. 是否提到ntext已被弃用。 It also talk about the nvarchar(max) being stored within your table making the queries faster. 它还讨论了存储在表中的nvarchar(max),使查询更快。 You might want to try using the variation of separate fields of numericanswer and textanswer and see where it gets you from a performance perspective. 您可能想尝试使用numericanswer和textanswer的单独字段的变体,并从性能角度查看它从何处获取。

暂无
暂无

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

相关问题 SQL Server 错误 - 操作数类型冲突:ntext 与 int 不兼容 -(我什至没有使用“ntext”) - SQL Server error - Operand type clash: ntext is incompatible with int - (I'm not even using "ntext") 操作数类型冲突:日期与 smallint 不兼容 - Operand type clash: date is incompatible with smallint SQL Server:返回的UniqueIdentifier,操作数类型冲突:uniqueidentifier与int不兼容 - SQL Server: Returning UniqueIdentifier, Operand type clash: uniqueidentifier is incompatible with int 存储过程-操作数类型冲突:smallint与uniqueidentifier不兼容 - Stored procedure - Operand type clash: smallint is incompatible with uniqueidentifier SQL 服务器错误“操作数类型冲突:varchar 与 int 不兼容” - SQL Server error "Operand type clash: varchar is incompatible with int " SQL Server-与工会的错误“操作数类型冲突uniqueidentifier与int不兼容” - SQL Server - error 'operand type clash uniqueidentifier is incompatible with int' with Union SQL Server 2012(触发器)操作数类型冲突:int与日期不兼容 - SQL Server 2012 (triggers) Operand type clash: int is incompatible with date SQL-Server - 操作数类型冲突:uniqueidentifier与int不兼容 - SQL-Server - Operand type clash: uniqueidentifier is incompatible with int SQL Server-操作数类型冲突:数字与datetimeoffset不兼容 - SQL Server - Operand type clash: numeric is incompatible with datetimeoffset SQL Server Operand类型冲突:日期与int不兼容 - Sql Server Operand type clash: date is incompatible with int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM