SQLServer2008中,同一个存储过程执行需要有时间隔吗?为什么程序中先后执行两次,会有一次执行没效,但也不报错?
求高手解答

解决方案 »

  1.   

    执行无效是什么意思把内容贴上来看看,跟sql 没关系。有问题是你的代码或者数据。
      

  2.   

    看你存储过程的逻辑是否有问题。SQL没有这方面限制
      

  3.   

    代码如下:ALTER PROCEDURE [dbo].[P_DataUpdate]
    @UserId int,
    @DataType int,
    @DataValue int
    AS
    begin
    SET NOCOUNT ON
    if exists( select UserId from T_UserData where UserId = @UserId and DataType = @DataType )
    begin
    update T_UserData  set DataValue = @DataValue where UserId = @UserId and DataType = @DataType 
    end
    else
    begin
    insert into UserId ( UserId , DataType, DataValue ) 
    values ( @UserId , @DataType, @DataValue )
    end
    return @@ERROR
    end
      

  4.   

    刚打错了一句,"insert into UserId ( UserId , DataType, DataValue )" -->"insert into T_UserData  ( UserId , DataType, DataValue )" 代码中是没错的,所以逻辑、语法之类的,也没问题
      

  5.   

    你这更新后的结果应该和插入后的一样的值,所以你看不出来效果
    你执行第二次,数据是不会有变化的。
    改成下面的肯定就能看出来效果了ALTER PROCEDURE [dbo].[P_DataUpdate]
    @UserId int,
    @DataType int,
    @DataValue int
    AS
    begin
    SET NOCOUNT ON
        if exists( select UserId from T_UserData where UserId = @UserId and DataType = @DataType )
        begin
            update T_UserData  set DataValue = 'xxoo' where UserId = @UserId and DataType = @DataType 
        end
        else
        begin
            insert into UserId ( UserId , DataType, DataValue ) 
            values ( @UserId , @DataType, @DataValue )
        end
        return @@ERROR
    end
      

  6.   

    改成这样你就可以看到它干了什么ALTER PROCEDURE [dbo].[P_DataUpdate]
    @UserId int,
    @DataType int,
    @DataValue int
    AS
    begin
    SET NOCOUNT ON
        if exists( select UserId from T_UserData where UserId = @UserId and DataType = @DataType )
        begin
            update T_UserData  set DataValue =  @DataType where UserId = @UserId and DataType = @DataType 
    print 'update'
        end
        else
        begin
            insert into UserId ( UserId , DataType, DataValue ) 
            values ( @UserId , @DataType, @DataValue )
    print 'insert'
        end
        return @@ERROR
    end