SP:
CREATE PROC [dbo].[Usp_MenuMgrAddUpdMenupermission]
@MenuID INT,
@PermissionID INT,
@PortalID INT,
@Username nvarchar(256)
AS
BEGIN
IF EXISTS(DELETE [dbo].[PagePermission]
WHERE [PortalID] = @PortalID AND
[MenuID] = @MenuID)
INSERT INTO [dbo].[MenuPermission]
(MenuID,
PermissionID,
AddedOn,
Username)
VALUES (@MenuID,
@PermissionID,
Getdate(),
@UserName)
END
但是我遇到了语法错误。请建议正确的方法。
答案 0 :(得分:2)
您可以使用OUTPUT Clause:
在一个声明中执行此操作DELETE [dbo].[PagePermission]
OUTPUT @MenuID,@PermissionID,Getdate(),@UserName
INTO [dbo].[MenuPermission]
WHERE [PortalID] = @PortalID AND [MenuID] = @MenuID
工作示例:
DECLARE @PagePermission TABLE (PortalID int, MenuID int)
INSERT INTO @PagePermission VALUES (1,1)
INSERT INTO @PagePermission VALUES (2,1)
INSERT INTO @PagePermission VALUES (3,2)
INSERT INTO @PagePermission VALUES (4,2)
DECLARE @MenuPermission TABLE (MenuID int, PermissionID int,YourDate datetime, UserName varchar(10))
DECLARE @MenuID int
,@PermissionID int
,@UserName varchar(10)
,@PortalID int
SELECT @MenuID =1
,@PermissionID =100
,@UserName ='xyz'
,@PortalID =2
DELETE @PagePermission
OUTPUT @MenuID,@PermissionID,Getdate(),@UserName
INTO @MenuPermission
WHERE PortalID = @PortalID AND MenuID = @MenuID
select * from @PagePermission
select * from @MenuPermission
输出:
PortalID MenuID
----------- -----------
1 1
3 2
4 2
(3 row(s) affected)
MenuID PermissionID YourDate UserName
----------- ------------ ----------------------- ----------
1 100 2011-11-09 09:18:52.693 xyz
(1 row(s) affected)
答案 1 :(得分:1)
使用@@ROWCOUNT
变量:
DELETE [dbo].[PagePermission] WHERE [PortalID] = @PortalID AND [MenuID] = @MenuID
IF @@ROWCOUNT > 0
INSERT INTO [dbo].[MenuPermission] ...
答案 2 :(得分:1)
我就是这样做的(为新要求编辑):
IF EXISTS(SELECT TOP 1 1 FROM [dbo].[PagePermission]
WHERE [PortalID] = @PortalID AND [MenuID] = @MenuID)
BEGIN
BEGIN TRANSACTION
DELETE [dbo].[PagePermission]
WHERE [PortalID] = @PortalID AND [MenuID] = @MenuID
INSERT INTO [dbo].[MenuPermission]
(MenuID, PermissionID, AddedOn, Username)
VALUES (@MenuID, @PermissionID, Getdate(), @UserName)
COMMIT TRANSACTION
END
ELSE
BEGIN
INSERT INTO [dbo].[MenuPermission]
(MenuID, PermissionID, AddedOn, Username)
VALUES (@MenuID, @PermissionID, Getdate(), @UserName)
END