我的触发器出错
我正在开发一个使用 SqlSiteMapProvider 的项目。我有两个表 Articles 和 SiteMap
Articles 表:
CREATE TABLE [dbuser].[Articles](
[ArticleId] [uniqueidentifier] NOT NULL,
[Title] [nvarchar](500) NULL,
[Description] [ntext] NULL,
[CategoryId] [int] NULL,
[pubDate] [datetime] NULL,
[Author] [nvarchar](255) NULL,
[Hit] [int] NULL,
[Auth] [bit] NULL
)...
和 SiteMap 表:
CREATE TABLE [dbuser].[SiteMap](
[ID] [int] IDENTITY(0,1) NOT NULL,
[Title] [nvarchar](50) NULL,
[Description] [nvarchar](512) NULL,
[Url] [nvarchar](512) NULL,
[Roles] [nvarchar](512) NULL,
[Parent] [int] NULL,
CONSTRAINT [PK_SiteMap] PRIMARY KEY CLUSTERED
(...
当我插入文章时,我的 ASP.NET 页面也会将该文章 url 和此类信息插入到 SiteMap 表中。我想要做的是,当我从文章表(来自 asp.net 页面)中删除文章时,使用触发器从 SiteMap 表中删除相关行。
我的 asp.net 页面以这种格式将文章信息插入 Sitmap 表中:
Dim SMUrl As String = "~/c.aspx?g=" & ddlCategoryId.SelectedValue & "&k=" & BaslikNo.ToString
我的意思是两个表中没有一列完全匹配。
我的触发器如下:
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON
GO
SET ROWCOUNT 1
GO
ALTER TRIGGER [dbuser].[trig_SiteMaptenSil] ON [dbuser].[Articles]
AFTER DELETE
AS
BEGIN
DECLARE @AID UNIQUEIDENTIFIER, @ttl NVARCHAR,@CID INT
SELECT @AID=ArticleId, @ttl = Title, @CID=CategoryId FROM DELETED
IF EXISTS(SELECT * FROM dbuser.SiteMap WHERE Url = N'~/c.aspx?g=' + CONVERT(nvarchar(5), @CID) + N'&k=' + CONVERT(nvarchar(36), @AID))
BEGIN
DELETE FROM dbuser.SiteMap WHERE Url = N'~/c.aspx?g=' + CONVERT(nvarchar(5), @CID) + N'&k=' + CONVERT(nvarchar(36), @AID)
END
END
GO
我正在使用 SSMSE 2008,我的远程数据库服务器的版本是 8.0
我得到的错误是:
没有删除任何行。 尝试删除第 1 行时出现问题。 错误来源:Microsoft.VisualStudio.DataTools。 错误消息:更新或删除的行值不会使该行唯一,或者会更改多行(2 行)。 更正错误并尝试再次删除该行或按 ESC 取消更改。
您可以帮助我如何使其正常工作吗?我已经搜索了大约几天。无法找到适合我的情况的解决方案...
提前致谢
I am working on a project using SqlSiteMapProvider. I have two tables Articles and SiteMap
Articles table:
CREATE TABLE [dbuser].[Articles](
[ArticleId] [uniqueidentifier] NOT NULL,
[Title] [nvarchar](500) NULL,
[Description] [ntext] NULL,
[CategoryId] [int] NULL,
[pubDate] [datetime] NULL,
[Author] [nvarchar](255) NULL,
[Hit] [int] NULL,
[Auth] [bit] NULL
)...
And SiteMap Table:
CREATE TABLE [dbuser].[SiteMap](
[ID] [int] IDENTITY(0,1) NOT NULL,
[Title] [nvarchar](50) NULL,
[Description] [nvarchar](512) NULL,
[Url] [nvarchar](512) NULL,
[Roles] [nvarchar](512) NULL,
[Parent] [int] NULL,
CONSTRAINT [PK_SiteMap] PRIMARY KEY CLUSTERED
(...
When I insert an Article my asp.net page also inserts that articles url and such information into SiteMap table. What I am trying to do is when I delete an Article from my Articles table (from asp.net page) the related row from SiteMap table with a trigger.
My asp.net page inserts the Article info into Sitmap table in this format:
Dim SMUrl As String = "~/c.aspx?g=" & ddlCategoryId.SelectedValue & "&k=" & BaslikNo.ToString
I mean there is no one column exactly matchin in two tables.
My Trigger is as follows:
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON
GO
SET ROWCOUNT 1
GO
ALTER TRIGGER [dbuser].[trig_SiteMaptenSil] ON [dbuser].[Articles]
AFTER DELETE
AS
BEGIN
DECLARE @AID UNIQUEIDENTIFIER, @ttl NVARCHAR,@CID INT
SELECT @AID=ArticleId, @ttl = Title, @CID=CategoryId FROM DELETED
IF EXISTS(SELECT * FROM dbuser.SiteMap WHERE Url = N'~/c.aspx?g=' + CONVERT(nvarchar(5), @CID) + N'&k=' + CONVERT(nvarchar(36), @AID))
BEGIN
DELETE FROM dbuser.SiteMap WHERE Url = N'~/c.aspx?g=' + CONVERT(nvarchar(5), @CID) + N'&k=' + CONVERT(nvarchar(36), @AID)
END
END
GO
I am using SSMSE 2008 and my remote db server's version is 8.0
The Error I get is:
No rows were deleted.
A problem occurred attempting to delete row 1.
Error Source: Microsoft.VisualStudio.DataTools.
Error Message: The row value(s) updated or deleted either do not make the row unique or they alter multiple rows(2 rows).
Correct the errors and attempt to delete the row again or press ESC to cancel the change(s).
May you help me how to get this working? I have searched for this for about a few days.. Couldn't find a solution for my case...
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了处理原始问题并处理多行删除,您的触发器可以重写如下。
To deal with the original issue and cope with deletions of multiple rows your trigger could be rewritten as follows.