MS sql server和mysql中update多條資料的例子

來源:互聯網
上載者:User
1.
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)

2.mysql的例子1
語句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;

 
3.mysql例子2
update t_advertiser as a,
    (select uid,sum(amount) as amount from t_trade where status='1' and type='4' group by uid)as b
    set a.spend=(b.amount) ,a.balance=(a.totalAmount-b.amount)    #注意是逗號不是and
    where a.id=b.uid;

 

4.
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.