SQL Server 2000:奇怪的错误:无法将 VARCHAR 转换为 INT
我在奇怪的情况下发生了一个奇怪的错误。
当且仅当它们尚不存在时,我在光标 curLongSkills
中收到的技能名称列表才会被插入到表 tbl_new_skill_overview
中。所以我像往常一样循环遍历光标,并在插入之前检查它是否已经存在。
奇怪的是,我在 SELECT @iCount = COUNT(ID)< 行上收到错误
将 varchar 值“Some Random Skill”转换为数据类型 int 的列时出现语法错误。
/代码>。
但是,如果我删除该语句中的 WHERE
子句,则不会发生这种情况。因此,如果我注释或删除 WHERE Name = @sSkillName
,它不会给出错误。就好像它认为我将 @sSkillName
分配给 @iCount
只是因为我在 WHERE 中使用
子句。@sSkillName
同一查询的
只要我能判断该技能是否已插入到 tbl_new_skill_overview
中,其他方法就足够了。我不一定非要这样做。
我还尝试了以下操作,但出现了相同的错误:
SET @iCount = (
SELECT COUNT(ID) AS Line_Count
FROM tbl_new_skill_overview
WHERE Name = @sSkillName
);
服务器正在运行 Microsoft SQL Server 2000(我知道,我知道...)。
以下是完整的 SQL 脚本。
DECLARE @sSkillName VARCHAR(200);
DECLARE @iCount INT;
DECLARE curLongSkills CURSOR FOR (
SELECT DISTINCT Name
FROM tbl_new_skill
WHERE Profile = 'long'
AND Parent_ID IS NULL
)
OPEN curLongSkills;
FETCH curLongSkills INTO @sSkillName;
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @iCount = COUNT(ID)
FROM tbl_new_skill_overview
WHERE Name = @sSkillName; -- No error if this line removed.
IF @iCount = 0 BEGIN
PRINT @sSkillName;
-- TODO: Insert skill
END;
FETCH curLongSkills INTO @sSkillName;
END;
CLOSE curLongSkills;
DEALLOCATE curLongSkills;
I'm having a weird error that happens under weird circumstances.
The list of skill names I receive in the cursor curLongSkills
is to be inserted into the table tbl_new_skill_overview
if and only if they don't already exist. So I loop through the cursor as usual, and check whether it already exists before inserting.
The weird thing is that I receive the error Syntax error converting the varchar value 'Some Random Skill' to a column of data type int.
on the line SELECT @iCount = COUNT(ID)
.
However, this does not happen if I remove the WHERE
clause in that statement. So if I comment or remove WHERE Name = @sSkillName
, it won't give the error. It's as if it thinks that I'm assigning @sSkillName
to @iCount
just because I'm using @sSkillName
in the WHERE
clause of the same query.
Other ways of doing this will suffice provided that I can tell whether or not the skill has already been inserted into tbl_new_skill_overview
. I don't necessarily have to do it this way.
I've also tried the following, which gives the same error:
SET @iCount = (
SELECT COUNT(ID) AS Line_Count
FROM tbl_new_skill_overview
WHERE Name = @sSkillName
);
The server is running Microsoft SQL Server 2000 (I know, I know...).
Following is the entire SQL script.
DECLARE @sSkillName VARCHAR(200);
DECLARE @iCount INT;
DECLARE curLongSkills CURSOR FOR (
SELECT DISTINCT Name
FROM tbl_new_skill
WHERE Profile = 'long'
AND Parent_ID IS NULL
)
OPEN curLongSkills;
FETCH curLongSkills INTO @sSkillName;
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @iCount = COUNT(ID)
FROM tbl_new_skill_overview
WHERE Name = @sSkillName; -- No error if this line removed.
IF @iCount = 0 BEGIN
PRINT @sSkillName;
-- TODO: Insert skill
END;
FETCH curLongSkills INTO @sSkillName;
END;
CLOSE curLongSkills;
DEALLOCATE curLongSkills;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我从来不喜欢光标 - 但作为一个厚颜无耻的选择,你应该能够在没有光标的情况下完成你想要的事情。
I've never liked cursors - but as a cheeky alternative, you should be able to accomplish what you want without a cursor.
问题是愚蠢。
tbl_new_skill_overview
中的Name
列被错误地输入为INT
,而不是VARCHAR
。感谢所有回复的人,特别是bobs要求我展示数据库结构,此时我意识到了错误。
The problem was stupidity.
The
Name
column intbl_new_skill_overview
was mistakenly put in as anINT
, not aVARCHAR
.Thanks to all who responded, particularly bobs for asking me to show the database structure, at which point I realized the mistake.
这确实是一个奇怪的现象。我不知道是什么原因造成的,但为了解决它,也许你可以这样做:
假设你以后不使用@iCount做任何其他事情。
That's a strange occurrence for sure. I have no idea what's causing it, but to get around it, perhaps you could do something like this:
That is assuming you don't use @iCount for anything else later.