question about update record

  • hi.i have a table that contains id,header,content,image fileds. id is primary key. Other fields are string fields(varchar). i keep image's path on the image field. update stored proc of my is like this:

    create procedure Upd_Info

    @hea varchar(max)=null,

    @cont varchar(max)=null,

    @im varchar(max)=null

    as

    begin

    update Info set header=@hea,content=@cont,image=@im

    end

    if i don't give value a field on update proc running,it gives null value that field.(this is normal for this proc)

    what i want to do is this: if i don't give value to the filed, that fied will be protect old value. Or else it takes new value.

    for example, a row has value of (aa,qqq,img/p.jpg)

    if i runs update proc parameters like (ww,cc) values, i want image filed doesn't change. row wil be remain with ww,cc,img/p.jpg values.

    i hope, i can express my problem properly. Thanks.

  • update Info set header=ISNULL(@hea,header),content=ISNULL(@cont,content),image=(@im,image)

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • don't forget the where clause or all of your records will be the same 😉

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Mike01 (3/6/2011)


    don't forget the where clause or all of your records will be the same 😉

    Thanks for the catch, Mike. Shamefully, I only fixed what I saw. :blush:

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply