Database Insert stored procedure to update table contents

Source: Internet
Author: User

Gooliugle inserting a stored procedure that updates the contents of a table

SETAnsi_nulls onGOSETQuoted_identifier onGO-- =============================================--Author: <xxxx>--Create Date: <2011-03-15>--Description: < item system daily report statistics >-- =============================================ALTER PROCEDURE [dbo].[Sp_propsreport_daysync] asBEGIN--iterate through the data in the Tbapps--declaring CursorsDECLARECursor_appCURSOR forSELECTId fromTbapps--Open CursorOPENCursor_appDECLARE @AppId intDECLARE @ReportDay VARCHAR(Ten),@OrderCount INT,@ActAmt INT,@PersonCount INTSELECT @ReportDay=CONVERT(VARCHAR(Ten),DATEADD(DD,-1,GETDATE()), -)--statistics of all charges deducted from status=2 in TbpropsorderFETCH NEXT  fromCursor_app into @AppId while @ @fetch_status=0BEGIN    SET @ActAmt=0    SET @OrderCount=0    SET @PersonCount=0        SELECT @ActAmt=ISNULL(SUM(Actpay),0),@OrderCount=COUNT(1),@PersonCount=COUNT(DISTINCTsndaid) fromTbpropsorder (NOLOCK)WHEREAppId=@AppId  andStatus=2      andLogTime>=@ReportDay  andLogTime<CONVERT(VARCHAR(Ten),GETDATE(), -)        IF EXISTS(SELECT 1  fromTbpropsorderdayreport (NOLOCK)WHEREReportday=@ReportDay  andAppId=@AppId)        UPDATETbpropsorderdayreportSETOrderCount=@OrderCount, Actamt=@ActAmt, Personcount= @PersonCount        WHEREReportday=@ReportDay  andAppId=@AppId    ELSE        INSERTTbpropsorderdayreport (Reportday, OrderCount, Actamt, Appid,personcount)VALUES(@ReportDay,@OrderCount,@ActAmt,@AppId,@PersonCount)            FETCH NEXT  fromCursor_app into @AppIdEND--Close CursorsCLOSECursor_app--Releasing CursorsdeallocateCursor_appENDGOSETAnsi_nullsOFFGOSETQuoted_identifierOFFGO

Database Insert stored procedure to update table contents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.