Using temporary tables to cycle data during [SQL Server] Storage

Source: Internet
Author: User

This article is original article, reprint please indicate the source! My blog address: http://www.cnblogs.com/txwd

As a result of the work, for more than a year now for this has not written SQL Server storage process, has been a little rusty. Recently work has a table of data needs to be updated regularly, turn over the previously written storage process, in this record.

The requirement is this:

There are two tables 1, bo Main table: blogger , 2, Bo master corresponding article table: Blogforblogger

The data of the article table is obtained by the service-side timing, the main table has a field to save the total number of bloggers, so this field should be updated regularly.

implementation: Create a storage process, and then execute the stored procedure in the database with a scheduled job.

-- =============================================--Author:li--Create date:2017-08-29--Description: Update the total number of Bo master articles-- =============================================CREATE PROCEDURESp_update_blogger_blog_articlecount asBEGIN        Declare @account varchar( -);--Bo Master Account    Declare @count int;--bo Master Number    Declare @i int;--Cyclic identification    Declare @articleCount int;--Number of articles    --Save all blogger information to a temporary table    Select *  into#Temp  from(SelectAccount,ranking,row_number () Over(Order  byRanking) asRow fromBlogger) bSelect @count = COUNT(1) from#Temp; Set @i = 1; --Circular Staging Table     while(@count >= @i)       begin          Select @account =Account from#Temp whereRow= @i;--get the blogger account for the current line          Select @articleCount = Count(1) fromBlogforbloggerwhereAccount= @account;--get the number of blog posts          UpdateBloggerSetArticlecount= @articleCount;--Update Blogger's number of articles          Set @i = @i + 1; End        Drop Table#Temp;--Delete temporary tableENDGO

This article is original article, reprint please indicate the source! My blog address: http://www.cnblogs.com/txwd

Using temporary tables to cycle data during [SQL Server] Storage

Related Article

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.