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