sql server programming:
Try: UPDATE User SET name = @name, pwd = COALESCE(@pwd, pwd) WHERE id=@id -- Hope this helps. Dan Guzman SQL Server MVP [quoted text, click to view] "L.Peter" <peter@nospam.local> wrote in message news:%23kDzbxZxEHA.1396@tk2msftngp13.phx.gbl... > Dear Group, > I have this sp: > create procedure UpdateUser > ( > @id char(10), > @name char(10), > @pwd char(10) > ) > as update User set name = @name , pwd = @pwd where id=@id > return > > I have a row > insert into User values('test','peter','pwd') > Question : How can I NOT update field pwd if the @pwd is null (if it is > not then it should be updated)? > something like: > exec UpdateUser 'test','Rob',null --> the field pwd remains 'pwd' > exec UpdateUser 'test','Rob','newpwd' --> the field pwd is changed to > 'newpwd' > > TIA > > Peter >
update User set name = @name , pwd = Case when @pwd IS NULL Then pwd ELSE @pwd END where id=@id -- Roji. P. Thomas Net Asset Management https://www.netassetmanagement.com [quoted text, click to view] "L.Peter" <peter@nospam.local> wrote in message news:%23kDzbxZxEHA.1396@tk2msftngp13.phx.gbl... > Dear Group, > I have this sp: > create procedure UpdateUser > ( > @id char(10), > @name char(10), > @pwd char(10) > ) > as update User set name = @name , pwd = @pwd where id=@id > return > > I have a row > insert into User values('test','peter','pwd') > Question : How can I NOT update field pwd if the @pwd is null (if it is > not then it should be updated)? > something like: > exec UpdateUser 'test','Rob',null --> the field pwd remains 'pwd' > exec UpdateUser 'test','Rob','newpwd' --> the field pwd is changed to > 'newpwd' > > TIA > > Peter >
Dear Group, I have this sp: create procedure UpdateUser ( @id char(10), @name char(10), @pwd char(10) ) as update User set name = @name , pwd = @pwd where id=@id return I have a row insert into User values('test','peter','pwd') Question : How can I NOT update field pwd if the @pwd is null (if it is not then it should be updated)? something like: exec UpdateUser 'test','Rob',null --> the field pwd remains 'pwd' exec UpdateUser 'test','Rob','newpwd' --> the field pwd is changed to 'newpwd' TIA Peter
Well, I am shocked, never get replies so fast. Thank you guys Peter [quoted text, click to view] "Dan Guzman" <guzmanda@nospam-online.sbcglobal.net> wrote in message news:ea78YzZxEHA.1988@TK2MSFTNGP12.phx.gbl... > Try: > > UPDATE User > SET name = @name, > pwd = COALESCE(@pwd, pwd) > WHERE id=@id > > -- > Hope this helps. > > Dan Guzman > SQL Server MVP > > "L.Peter" <peter@nospam.local> wrote in message > news:%23kDzbxZxEHA.1396@tk2msftngp13.phx.gbl... >> Dear Group, >> I have this sp: >> create procedure UpdateUser >> ( >> @id char(10), >> @name char(10), >> @pwd char(10) >> ) >> as update User set name = @name , pwd = @pwd where id=@id >> return >> >> I have a row >> insert into User values('test','peter','pwd') >> Question : How can I NOT update field pwd if the @pwd is null (if it is >> not then it should be updated)? >> something like: >> exec UpdateUser 'test','Rob',null --> the field pwd remains 'pwd' >> exec UpdateUser 'test','Rob','newpwd' --> the field pwd is changed to >> 'newpwd' >> >> TIA >> >> Peter >> > >
Don't see what you're looking for? Try a search.
|