我现在的情况是这样的,我的User表中有很多的用户,标志UserID,还有一个Type,可能是1,2,3,4,然后还有一个num,
每天一个用户一个类型只需要统计一次,将num累加起来的。我T_sql不太好。

解决方案 »

  1.   

    就是当有一个用户的数据来的时候先判断是否有这个类型和UserID,而且是当天的,有的话讲Num累加,否则新添加一条数据。不知道怎么写。
      

  2.   

    create proc MoneyLogInsertPro@userID varchar, --UserID
    @type int, --类型
    @num int -- 数量
    asdeclare  @count intbegin --同一天同一个用户的同一个类型只能有一条数据
    select @count = (select count(*) from test where DATEDIFF(d,time,getdate())=0 and userid = @userID and type = @type)if(@count <=0) --没有数据insert into MoneyLog(type,userid,num,logdate)values(@type,@userID,@num,getdate()) --插入一条新的数据elsedeclare  @tmpNum int --临时使用select @tmpNum = select num from MoneyLog where userid = @userID and type = @type --查出原来的num的值update MoneyLog set num = (@tmpNum+ @num) where userID = @userID and type = @type --相加

    endgo
    exec  MoneyLogInsertPro '001',1,14写了一个。貌似又错误
      

  3.   


    alter proc MoneyLogInsertPro@userID varchar, --UserID
    @type int, --类型
    @num int -- 数量
    asdeclare  @count intbegin --同一天同一个用户的同一个类型只能有一条数据
    select @count = (select count(*) from test where DATEDIFF(d,time,getdate())=0 and userid = @userID and type = @type)if(@count <=0) --没有数据insert into MoneyLog(type,userid,num,logdate)values(@type,@userID,@num,getdate()) --插入一条新的数据elsedeclare  @tmpNum int --临时使用 set @tmpNum = (select num from MoneyLog where userid = @userID and type = @type) --查出原来的num的值update MoneyLog set num = (@tmpNum+ @num) where userID = @userID and type = @type --相加

    endgo
    exec  MoneyLogInsertPro '001',1,14
    建的数据表示test
    提示的错误是服务器: 消息 208,级别 16,状态 1,过程 MoneyLogInsertPro,行 13
    对象名 'test' 无效。不知道为何???
      

  4.   

    下面的存储过程是没有错误的,执行的时候就出现test无效,为什么呢?
      

  5.   

    test 表不存在吧 !
      

  6.   


     
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    /*
    * CSDN问题存储过程
    */
    CREATE PROCEDURE P_Common_CheckUser
     @UserID INT,--用户ID
     @Type INT,--用户类型
     @ShowMsg VARCHAR(50) OUTPUT --返回消息
    AS
    BEGIN 
    SET NOCOUNT ON;
        IF NOT EXISTS(SELECT * FROM [USER] WHERE UserID=@UserID AND [Type]=@Type)
        BEGIN
         SET @ShowMsg=N'用户不存在';
         RETURN -1;
        END
        --应该有哪块记录更新时间,应该有个字段吧?
        DECLARE @dtNow DATETIME;
        SELECT @dtNow=[更新时间字段] FROM [USER] WHERE [UserID]=@UserID AND [Type]=@Type;
        IF(DATEDIFF(DAY,@dtNow,GETDATE())=0)
        BEGIN
         UPDATE [USER] SET num=num+1 WHERE [UserID]=@UserID AND [Type]=@Type;
        END
        ELSE
         BEGIN
         INSERT INTO [USER] VALUES(.......)
         END
        IF(@@ROWCOUNT>0)
    BEGIN
         SET @ShowMsg=N'更新成功';
         RETURN 0;
    END
        ELSE
         BEGIN
         SET @ShowMsg=N'更新失败';
         RETURN -2;
         END
    END
    GO你应该少说了几个关键问题描述。
    1:怎么判断是当天。表里的哪个字段?
    2:如果不存在,则插入到哪个表里?