执行时不执行更新DB的过程

时间:2015-01-12 18:21:39

标签: sql sql-server variables stored-procedures procedure

我有一个两个简单的更新语句,可以单独运行。但是我想把它们放在一个程序中,这样我就可以为每个角色定义两个角色和一个生效日期。

更新声明是:

UPDATE DTSMobileDetail
SET DTSMD.MobileID=294
FROM DTSMobileDetail DTSMD
Inner Join DTSDetail DTSD on DTSMD.ShiftID=DTSD.ShiftID
Inner Join LocationDetail LD on DTSD.LocationID=LD.LocationID
Inner Join Accounts Acct on LD.AccountID=Acct.AccountID
Inner Join CenterDetail CD on Acct.CenterID=CD.CenterID
Inner Join IDViewCollectionOp CO on CD.CenterID=CO.CodeID
WHERE CO.CodeID in ('108','110','106','107')
and DTSMD.MobileID=700

-- Update Futre Drives with new Staff Template
UPDATE DSMD
SET DSMD.MobileID=294
FROM DriveMaster DM
Inner Join DriveShiftDetail DSD on DM.DriveID=DSD.DriveID
Inner Join DriveShiftMobileDetail DSMD on DSD.ShiftID=DSMD.ShiftID
Inner Join CenterDetail CD on DM.CenterID=CD.CenterID
Inner Join IDViewCollectionOp CO on CD.CenterID=CO.CodeID
WHERE DSMD.MostRecent=1
And CO.CodeID in ('108','110','106','107')
And DSMD.MobileID=700
And DM.FromDateTime >= '02/19/2015'

我想要完成的是将新的移动ID(@NewRoleName)和旧的移动ID(@OldRoleName)设置为变量,并将第二次更新中的日期设置为@EffectiveDate。

我试图将更新插入到一个过程中:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--CREATE PROCEDURE CA66_Staff_Role_Change
Alter PROCEDURE CA66_Staff_Role_Change
-- Declare new role, old role, effective drive date variables
@OldRoleName int,
@NewRoleName int,
@EffectiveDate datetime
AS
BEGIN
SET NOCOUNT ON;

-- Update Accounts with new Staff Template
UPDATE DTSMD
SET DTSMD.MobileID=@NewRoleName
FROM DTSMobileDetail DTSMD
Inner Join DTSDetail DTSD on DTSMD.ShiftID=DTSD.ShiftID
Inner Join LocationDetail LD on DTSD.LocationID=LD.LocationID
Inner Join Accounts Acct on LD.AccountID=Acct.AccountID
Inner Join CenterDetail CD on Acct.CenterID=CD.CenterID
Inner Join IDViewCollectionOp CO on CD.CenterID=CO.CodeID
WHERE CO.CodeID in ('108','110','106','107')
and DTSMD.MobileID=@OldRoleName

-- Update Futre Drives with new Staff Template
UPDATE DSMD
SET DSMD.MobileID=@NewRoleName
FROM DriveMaster DM
Inner Join DriveShiftDetail DSD on DM.DriveID=DSD.DriveID
Inner Join DriveShiftMobileDetail DSMD on DSD.ShiftID=DSMD.ShiftID
Inner Join CenterDetail CD on DM.CenterID=CD.CenterID
Inner Join IDViewCollectionOp CO on CD.CenterID=CO.CodeID
WHERE DSMD.MostRecent=1
And CO.CodeID in ('108','110','106','107')
And DSMD.MobileID=@OldRoleName
And DM.FromDateTime >= @EffectiveDate

END
GO

当我尝试用:

执行语句时
Exec CA66_Staff_Role_Change 294,700,'02/19/2015'
Go

它告诉我它已成功执行,但是当我检查记录时,没有更新任何内容。

有关我可能遗失的任何建议吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

您正在将294传递给原始更新语句中的@oldRoleName,294应该是新的角色名称

所以正确传递值

Exec CA66_Staff_Role_Change 700,294,'02/19/2015'