T-SQL query: CTE, T-SQL: CTE

Source: Internet
Author: User

T-SQL query: CTE, T-SQL: CTE

I have seen a simple example of the with as Clause in two books. I have not found any relevant information on the Internet.

Summary:

[Batch update of large tables]

[Batch Delete large tables]

[Keep only one row for completely repeated rows]


-- Create a test TABLE -- drop table [tabName] SELECT * INTO [tabName] FROM sys. objectsSELECT * FROM [databaseName]. [dbo]. [tabName] order by name desc

Batch [batch update of large tables] When the webpage needs to be upgraded, the database needs to add or update the field value, which will be blocked for a long time. Generally, you can add a field to allow null values, update the default values in the table, and add constraints. For example, to update the principal_id of the test table to 0, use the simplest and feasible method to update the table :; with tab as (select top 10 principal_id FROM [dbo]. [tabName] where principal_id is null) update TAB set principal_id = 0 to update the field principal_id, take only one. Each time you select the first 10 rows for update, the value of null is 0. You can create a scheduled job update. Batch [batch deletion of large tables] a large number of data needs to be deleted for some data maintenance, and the table is large and many users are still using it. Generally, you can create a job to delete it at night or delete it in segments based on a field. You can also delete the first N rows based on the selected conditions. with tab as (select top 10 principal_id FROM [dbo]. [tabName] where principal_id is null) delete from tab distinct [retain only one row for completely repeated rows] -- insert into [tabName] select top 50 PERCENT * FROM [databaseName]. [dbo]. [tabName] SELECT * FROM [databaseName]. [dbo]. [tabName] ORD In the cases where er by name desc is the most widely used on the Internet, it must be unique to specify a column to use a statement to delete other duplicates. All identical rows indicate creating a temporary table for the transition operation. The following uses the "with clause and ROW_NUMBER () function" to delete completely duplicated rows. When grouping partition, you can SELECT a column for group sorting. with tab as (SELECT ROW_NUMBER () over (partition by object_id order by (select 0) idFROM [dbo]. [tabName]) delete from tab where id> 1 partition ------------------------------------------------------------------------------------------------------------------------------






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.