SQL Server 2000:奇怪的错误:无法将 VARCHAR 转换为 INT

发布于 2024-10-21 00:56:10 字数 1488 浏览 2 评论 0原文

我在奇怪的情况下发生了一个奇怪的错误。

当且仅当它们尚不存在时,我在光标 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-10-28 00:56:10

我从来不喜欢光标 - 但作为一个厚颜无耻的选择,你应该能够在没有光标的情况下完成你想要的事情。

insert into tbl_new_skill_overview
select //columnNames
from tbl_new_skill
WHERE Profile = 'long'
AND Parent_ID IS NULL
and name not in
(select name from tbl_new_skill)

I've never liked cursors - but as a cheeky alternative, you should be able to accomplish what you want without a cursor.

insert into tbl_new_skill_overview
select //columnNames
from tbl_new_skill
WHERE Profile = 'long'
AND Parent_ID IS NULL
and name not in
(select name from tbl_new_skill)
德意的啸 2024-10-28 00:56:10

问题是愚蠢。

tbl_new_skill_overview 中的 Name 列被错误地输入为 INT,而不是 VARCHAR

感谢所有回复的人,特别是bobs要求我展示数据库结构,此时我意识到了错误。

The problem was stupidity.

The Name column in tbl_new_skill_overview was mistakenly put in as an INT, not a VARCHAR.

Thanks to all who responded, particularly bobs for asking me to show the database structure, at which point I realized the mistake.

自由如风 2024-10-28 00:56:10

这确实是一个奇怪的现象。我不知道是什么原因造成的,但为了解决它,也许你可以这样做:

if not exists (select * from tbl_new_skill_overview where Name = @sSkillName) begin
    print @sSkillName;
    -- TODO: Insert skill
end

假设你以后不使用@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:

if not exists (select * from tbl_new_skill_overview where Name = @sSkillName) begin
    print @sSkillName;
    -- TODO: Insert skill
end

That is assuming you don't use @iCount for anything else later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文