删除在存储过程中不起作用的查询
Delete query not working in stored procedure
我在 sql 服务器中有一个存储过程,它在第一步中将一些数据从一个 table 插入到另一个,然后从第一个 [= 中删除第二个 table 中插入的数据16=];可能描述的比较复杂,看代码:
ALTER PROCEDURE [dbo].[SP_Insert_NotificationUserBulk]
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[NotificationUserBulk]
([NotificationId]
,[UserId]
,[IsNotify]
,[IsShow]
,[NotifyMethod]
,[NotifyDateTime]
,[ShowDateTime]
,[IsDeleted]
,[CreatedDate]
,[ModifiedDate])
(SELECT
UserId,
NotificationId,
IsNotify,
IsShow,
NotifyMethod,
NotifyDateTime,
ShowDateTime,
IsDeleted,
CreatedDate,ModifiedDate
FROM NotificationUsers WHERE IsShow=1 OR IsDeleted=1)
DELETE FROM dbo.NotificationUsers WHERE IsShow=1 OR IsDeleted=1
RETURN
END
Insert 语句执行成功,没有问题,但在 Delete 语句中不起作用,没有任何反应。
那么问题是什么,我该如何解决?
DELETE Query后请不要使用RETURN,尽量简单Query
DELETE FROM dbo.NotificationUsers WHERE IsShow=1 OR IsDeleted=1
如果这是一次性或临时查询,您可以尝试这样的操作。如果它是 oltp 那么你可以添加 TRY/CATCH
和 return 一个 success/failure 变量。
在 XACT_ABORT ON
的显式事务中,查询插入 'NotificationUsers' table,将(插入的)NotificationId 输出到临时 table,并从 'NotificationUsers' 基于温度 table.
的 INNER JOIN
ALTER PROCEDURE [dbo].[SP_Insert_NotificationUserBulk]
AS
SET NOCOUNT ON;
SET xact_abort on;
begin transaction
declare @NotificationId table(NotificationId int primary key);
INSERT INTO [dbo].[NotificationUserBulk]
([NotificationId]
,[UserId]
,[IsNotify]
,[IsShow]
,[NotifyMethod]
,[NotifyDateTime]
,[ShowDateTime]
,[IsDeleted]
,[CreatedDate]
,[ModifiedDate])
output inserted.NotificationId into @NotificationId
SELECT UserId, NotificationId, IsNotify, IsShow, NotifyMethod,
NotifyDateTime, ShowDateTime, IsDeleted, CreatedDate,
ModifiedDate
FROM NotificationUsers
WHERE IsShow=1
OR IsDeleted=1;
DELETE nu
from dbo.NotificationUsers nu
join @NotificationId n on nu.NotificationId=n.NotificationId;
commit
go
我在 sql 服务器中有一个存储过程,它在第一步中将一些数据从一个 table 插入到另一个,然后从第一个 [= 中删除第二个 table 中插入的数据16=];可能描述的比较复杂,看代码:
ALTER PROCEDURE [dbo].[SP_Insert_NotificationUserBulk]
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[NotificationUserBulk]
([NotificationId]
,[UserId]
,[IsNotify]
,[IsShow]
,[NotifyMethod]
,[NotifyDateTime]
,[ShowDateTime]
,[IsDeleted]
,[CreatedDate]
,[ModifiedDate])
(SELECT
UserId,
NotificationId,
IsNotify,
IsShow,
NotifyMethod,
NotifyDateTime,
ShowDateTime,
IsDeleted,
CreatedDate,ModifiedDate
FROM NotificationUsers WHERE IsShow=1 OR IsDeleted=1)
DELETE FROM dbo.NotificationUsers WHERE IsShow=1 OR IsDeleted=1
RETURN
END
Insert 语句执行成功,没有问题,但在 Delete 语句中不起作用,没有任何反应。 那么问题是什么,我该如何解决?
DELETE Query后请不要使用RETURN,尽量简单Query
DELETE FROM dbo.NotificationUsers WHERE IsShow=1 OR IsDeleted=1
如果这是一次性或临时查询,您可以尝试这样的操作。如果它是 oltp 那么你可以添加 TRY/CATCH
和 return 一个 success/failure 变量。
在 XACT_ABORT ON
的显式事务中,查询插入 'NotificationUsers' table,将(插入的)NotificationId 输出到临时 table,并从 'NotificationUsers' 基于温度 table.
INNER JOIN
ALTER PROCEDURE [dbo].[SP_Insert_NotificationUserBulk]
AS
SET NOCOUNT ON;
SET xact_abort on;
begin transaction
declare @NotificationId table(NotificationId int primary key);
INSERT INTO [dbo].[NotificationUserBulk]
([NotificationId]
,[UserId]
,[IsNotify]
,[IsShow]
,[NotifyMethod]
,[NotifyDateTime]
,[ShowDateTime]
,[IsDeleted]
,[CreatedDate]
,[ModifiedDate])
output inserted.NotificationId into @NotificationId
SELECT UserId, NotificationId, IsNotify, IsShow, NotifyMethod,
NotifyDateTime, ShowDateTime, IsDeleted, CreatedDate,
ModifiedDate
FROM NotificationUsers
WHERE IsShow=1
OR IsDeleted=1;
DELETE nu
from dbo.NotificationUsers nu
join @NotificationId n on nu.NotificationId=n.NotificationId;
commit
go