日期:2014-05-18  浏览次数:20608 次

高手帮忙解答下这个存储过程,有些语句看不懂
SQL code

CREATE proc [dbo].[spUpdClerkPwd]
 @ClerkId varchar(20),        --被重置操作员号            
 @OPassWord varchar(20),    --旧密码
 @NPassWord varchar(20),    --新密码
 @OperateId varchar(20)        --操作者员号(谁操作的)     
as
begin
set nocount on
declare @field int
declare @err int
set @err=0
declare @rowClerk int
declare @rowInfo int
declare @rowRelation int
select @rowClerk=COUNT(1) from Clerk where ClerkId = @ClerkId

--select @rowClerk as rowClerk , @rowInfo as rowInfo, @rowRelation as rowRelation
declare @Clerkrowcount int

declare @oldClerk table(
    Id int,
    [ClerkId] [varchar](20)  NULL,
    [PassWord] [varchar](20)  NULL
)

begin tran
begin try
--这段下面的看不懂,请解释下 
update a set 
a.PassWord=(case when isnull(@OPassWord, '~NULL~')=ISNULL(@NPassWord, '~NULL~') then a.PassWord else @NPassWord end),
UpdateDT=GETDATE()
output deleted.Id,deleted.ClerkId,deleted.PassWord  
into @oldClerk
from Clerk as a  
where  a.ClerkId =@ClerkId
and isnull(a.PassWord, '~NULL~')=isnull(@OPassWord, '~NULL~')
and a.Flag=0 
set @Clerkrowcount=@@ROWCOUNT 


--PRINT  @ClerkRoleRelationrowcount
if @Clerkrowcount=@rowClerk  
commit
else
begin
rollback 
if @rowClerk=(select COUNT(1) from @oldClerk as a  where a.ClerkId = @ClerkId  )
set @err=50010
else
set @err=50013

goto exit_bk
end



------解决方案--------------------
case when isnull(@OPassWord, '~NULL~')=ISNULL(@NPassWord, '~NULL~')

两个变量相等,或者两个变量都为null
------解决方案--------------------
update a set 
a.PassWord=(case when isnull(@OPassWord, '~NULL~')=ISNULL(@NPassWord, '~NULL~') then a.PassWord else @NPassWord end)

@OPassWord和@NPassWord都为空或者都不为空且相等......
------解决方案--------------------
是输出这几个列,并且赋值到@oldClerk表里面
------解决方案--------------------
SQL code

--这段下面的看不懂,请解释下 
--更新这个表a 设置Password字段等于@NPassWord 变量
--如果新密码和旧密码相等,更新PassWord等于PassWord,和更新1=1是一样,就是没变化
UPDATE  a
SET     a.PassWord = ( CASE WHEN ISNULL(@OPassWord, '~NULL~') = ISNULL(@NPassWord,
                                                              '~NULL~')
                            THEN a.PassWord
                            ELSE @NPassWord
                       END ) ,
        UpdateDT = GETDATE()
        --更新的同时用output子句输入被更新掉的结果进入到@oldClerk表变量
OUTPUT  deleted.Id ,
        deleted.ClerkId ,
        deleted.PassWord
        INTO @oldClerk
FROM    Clerk AS a
--这就是个where条件
WHERE   a.ClerkId = @ClerkId
        AND ISNULL(a.PassWord, '~NULL~') = ISNULL(@OPassWord, '~NULL~')
        AND a.Flag = 0 
SET @Clerkrowcount = @@ROWCOUNT

------解决方案--------------------
探讨

引用:
SQL code

--这段下面的看不懂,请解释下
--更新这个表a 设置Password字段等于@NPassWord 变量
--如果新密码和旧密码相等,更新PassWord等于PassWord,和更新1=1是一样,就是没变化
UPDATE a
SET a.PassWord = ( CASE WHEN ISNULL(@OPas……