操作数类型冲突:uniqueidentifier 与 int 不兼容

发布于 2024-12-04 02:13:32 字数 452 浏览 0 评论 0原文

当我尝试创建下面的存储过程时,出现以下错误:

操作数类型冲突:uniqueidentifier 与 int 不兼容

我不清楚是什么导致了此错误。 UserID 实际上是我所有表中的一个 int 。有人可以告诉我我做错了什么吗?

create procedure dbo.DeleteUser(@UserID int)
as

    delete from [aspnet_Membership] where UserId = @UserID
    delete from [Subscription] where UserID = @UserID
    delete from [Address] where UserID = @UserID
    delete from [User] where UserID = @UserID

go

When I attempt to create the stored procedure below I get the following error:

Operand type clash: uniqueidentifier is incompatible with int

It's not clear to me what is causing this error. UserID is in fact an int in all of my tables. Can someone tell me what I've done wrong?

create procedure dbo.DeleteUser(@UserID int)
as

    delete from [aspnet_Membership] where UserId = @UserID
    delete from [Subscription] where UserID = @UserID
    delete from [Address] where UserID = @UserID
    delete from [User] where UserID = @UserID

go

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

罪#恶を代价 2024-12-11 02:13:32

在我看来,至少其中一个表已将 UserID 定义为 uniqueidentifier,而不是 int。你检查过每个表中的数据吗? SELECT TOP 1 UserID FROM 每个表会产生什么结果? int 还是 GUID

编辑

我认为您已经基于包含名为 UserID 的列的所有表构建了一个过程。我认为您不应该在脚本中包含 aspnet_Membership 表,因为它实际上并不是“您的”表之一。

如果您打算围绕 aspnet_Membership 数据库设计表,那么当该表明确使用 uniqueidentifier 时,为什么其余列是 int UserID 列?

Sounds to me like at least one of those tables has defined UserID as a uniqueidentifier, not an int. Did you check the data in each table? What does SELECT TOP 1 UserID FROM each table yield? An int or a GUID?

EDIT

I think you have built a procedure based on all tables that contain a column named UserID. I think you should not have included the aspnet_Membership table in your script, since it's not really one of "your" tables.

If you meant to design your tables around the aspnet_Membership database, then why are the rest of the columns int when that table clearly uses a uniqueidentifier for the UserID column?

离旧人 2024-12-11 02:13:32

如果您通过视图访问此内容,则尝试sp_recompile 或刷新视图。

sp_recompile

导致存储过程、触发器和用户定义函数在下次运行时重新编译。它通过从过程缓存中删除现有计划来强制在下次运行过程或触发器时创建新计划来实现此目的。在 SQL Server Profiler 集合中,记录事件 SP:CacheInsert,而不是事件 SP:Recompile。

参数

[ @objname= ] 'object'

当前数据库中存储过程、触发器、表、视图或用户定义函数的限定或非限定名称。对象是 nvarchar(776),没有默认值。如果 object 是存储过程、触发器或用户定义函数的名称,则存储过程、触发器或函数将在下次运行时重新编译。如果 object 是表或视图的名称,则引用该表或视图的所有存储过程、触发器或用户定义函数将在下次运行时重新编译。

返回代码值

0(成功)或非零数字(失败)

备注

sp_recompile 仅在当前数据库中查找对象。

存储过程、触发器和用户定义函数使用的查询仅在编译时才会优化。当对数据库进行索引或其他影响统计信息的更改时,编译的存储过程、触发器和用户定义的函数可能会降低效率。通过重新编译作用于表的存储过程和触发器,您可以重新优化查询。

If you're accessing this via a View then try sp_recompile or refreshing views.

sp_recompile:

Causes stored procedures, triggers, and user-defined functions to be recompiled the next time that they are run. It does this by dropping the existing plan from the procedure cache forcing a new plan to be created the next time that the procedure or trigger is run. In a SQL Server Profiler collection, the event SP:CacheInsert is logged instead of the event SP:Recompile.

Arguments

[ @objname= ] 'object'

The qualified or unqualified name of a stored procedure, trigger, table, view, or user-defined function in the current database. object is nvarchar(776), with no default. If object is the name of a stored procedure, trigger, or user-defined function, the stored procedure, trigger, or function will be recompiled the next time that it is run. If object is the name of a table or view, all the stored procedures, triggers, or user-defined functions that reference the table or view will be recompiled the next time that they are run.

Return Code Values

0 (success) or a nonzero number (failure)

Remarks

sp_recompile looks for an object in the current database only.

The queries used by stored procedures, or triggers, and user-defined functions are optimized only when they are compiled. As indexes or other changes that affect statistics are made to the database, compiled stored procedures, triggers, and user-defined functions may lose efficiency. By recompiling stored procedures and triggers that act on a table, you can reoptimize the queries.

一腔孤↑勇 2024-12-11 02:13:32

原因是数据与数据类型不匹配。
我遇到了同样的问题,我忘记使字段匹配。虽然我的情况和你的不一样,但它显示了类似的错误消息。

情况是我复制了一张表,但不小心拼错了一个字段,所以我在创建数据库后使用ALTER更改了它。并且两个表中字段的顺序并不相同。因此,当我使用 INSERT INTO TableName SELECT * FROM TableName 时,结果显示类似的错误: Operand type crash: datetime is incomplete with uniqueidentifier

这是一个简单的示例

use example
go
create table Test1 (
    id int primary key,
    item uniqueidentifier,
    inserted_at datetime
    )
go
create table Test2 (
    id int primary key,
    inserted_at datetime
    )
go
alter table Test2 add item uniqueidentifier;
go

--insert into Test1 (id, item, inserted_at) values (1, newid(), getdate()), (2, newid(), getdate());


insert into Test2 select * from Test1;

select * from Test1;
select * from Test2;


:错误信息是:

Msg 206, Level 16, State 2, Line 24
Operand type clash: uniqueidentifier is incompatible with datetime

The reason is that the data doesn't match the datatype.
I have come across the same issues that I forgot to make the fields match. Though my case is not same as yours, but it shows the similar error message.

The situation is that I copy a table, but accidently I misspell one field, so I change it using the ALTER after creating the database. And the order of fields in both table is not identical. so when I use the INSERT INTO TableName SELECT * FROM TableName, the result showed the similar errors: Operand type clash: datetime is incompatible with uniqueidentifier

This is a simiple example:

use example
go
create table Test1 (
    id int primary key,
    item uniqueidentifier,
    inserted_at datetime
    )
go
create table Test2 (
    id int primary key,
    inserted_at datetime
    )
go
alter table Test2 add item uniqueidentifier;
go

--insert into Test1 (id, item, inserted_at) values (1, newid(), getdate()), (2, newid(), getdate());


insert into Test2 select * from Test1;

select * from Test1;
select * from Test2;


The error message is:

Msg 206, Level 16, State 2, Line 24
Operand type clash: uniqueidentifier is incompatible with datetime
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文