国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 数据库 > Sqlserver > 实例:Sql server和MYsql中update多条数据

实例:Sql server和MYsql中update多条数据

来源:程序员人生   发布时间:2014-01-19 07:10:52 阅读次数:2581次

建站学院文档 mssql server 中 使用动态表名 MY sql server存储过程完整例子 大家直接看代码:

MS sql server中使用动态的表名:
declare @tableName nvarchar(160)
set @tableName = 't_stat_all'
declare @sql nvarchar(160)
print @tableName
set @sql='select count(*) from '+@tableName
exec(@sql)
MYsql语句
update (select sc,tos,sum(click) as click,product,adpid from log_sc_click group by sc,tos,product,adpid) as a,
t_stat_sc_h_tmp as b
set b.sc_click=a.click
where b.stat_date=str_date and b.hour=str_hour and b.sc=a.sc
and b.tos=a.tos and b.product=a.product and a.adpid=b.adpid;

MY sql server存储过程完整例子

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

--ALTER procedure [dbo].[ad_stat]
ALTER procedure [dbo].[ad_stat]
@day varchar(20) = null

as
BEGIN try

if(@day is null)
set @day = convert(varchar(10),dateadd(day,-1,getdate()),121)

declare @theDay datetime
set @theDay = cast(@day as datetime)


declare @yesterday varchar(10)
set @yesterday = convert(varchar(10),@theDay,121)

declare @tableName nvarchar(160)
set @tableName = 'log_adlist_'+ left(@yesterday,4)+'_'+substring(@yesterday,6,2)+'_'+substring(@yesterday,9,2)

declare @sql nvarchar(500)
set @sql=' update t_stat_all '+
' set cl=b.click from '+
' ( '+
' select AllType as ad_id ,posid as posid,count(*) as click '+
' from '+@tableName+
' where datediff(d,VisitTime,'+@theDay+')=0'+
' group by AllType,posid '+
' ) b ,t_stat_all a '+
' where datediff(d,a.stat_date,'+@theDay+')=0 and a.posid=b.posid '+
' and a.ad_id=b.ad_id ';
exec(@sql)

END try
begin catch
INSERT INTO actionLogs
([createTime]
,[actionName]
,[type]
,[infor])
VALUES
(getdate(),
'ad_stat',
'error', --error,info
ERROR_MESSAGE())
end catch

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生