我现在的情况是这样的,我的User表中有很多的用户,标志UserID,还有一个Type,可能是1,2,3,4,然后还有一个num,
每天一个用户一个类型只需要统计一次,将num累加起来的。我T_sql不太好。
每天一个用户一个类型只需要统计一次,将num累加起来的。我T_sql不太好。
解决方案 »
- 使用Web Parts后如何区分多个用户?
- gridview 数据多,输出到word后如何设置word的分页?请高手帮忙!
- 链接字符串
- ASP.NET 不能访问局域网共享目录
- 在本地计算机上使用.NET的WEB页面做一个MSMQ的接收程序
- .net DataSet类型转换为List
- 怎样写一个匹配不能为空的正则表达式。
- DataGrid中放置EditCommandColumn 如何让EditCommandColumn 获得焦点
- Response的AddFileDependency方法是什么意思?
- 奇怪~~datagrid中 ButtonColumn到底有没有Commandname属性??
- (100分)江湖救急,急,急,急,IIS配置错误
- 100分献上,江湖救急,急,IIS抛出参数错误。
@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写了一个。貌似又错误
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' 无效。不知道为何???
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:如果不存在,则插入到哪个表里?