CREATE PROCEDURE PicViewUpdate 
@UId int, 
@VProId varchar (50) 
AS 
IF  (SELECT LEN(PoductViewed) FROM tb_user  WHERE UserId=@UId)  > 0 
begin 
UPDATE tb_user 
          SET ProductViewed =ProductViewed + ',' + @VProId 
WHERE UserId=@UId 
end 
else 
begin 
UPDATE tb_user 
SET 
ProductViewed = @VProId 
WHERE UserId=@UId 
end 
GO

解决方案 »

  1.   

    if object_id('tb_user') is not null
    drop table tb_user
    go
    create table tb_user(UserId int,ProductViewed varchar(50))
    insert into tb_user select 1,''
    insert into tb_user select 2,null
    insert into tb_user select 3,'呵呵'
    go
    if object_id('PicViewUpdate') is not null
    drop PROCEDURE PicViewUpdate
    go
    CREATE PROCEDURE PicViewUpdate
    @UId int,
    @VProId varchar (50)
    AS
    UPDATE tb_user
    SET ProductViewed =case when ProductViewed='' or ProductViewed is null then '' else ProductViewed+',' end + @VProId
    WHERE UserId=@UId
    go
    exec PicViewUpdate 1,'嘿嘿'
    exec PicViewUpdate 2,'嘿嘿'
    exec PicViewUpdate 3,'嘿嘿'select * from tb_userUserId ProductViewed
    1 嘿嘿
    2 嘿嘿
    3 呵呵,嘿嘿