sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Update Top(N) in SQL Server and Azure SQL Database

Antonios Chatzipavlis
Thursday 28 May 2020

Η επιχειρηματική λογική που κάθε οργανισμός έχει οδηγεί πολλές φορές σε ανάγκες που δεν έχουμε σκεφτεί. Όταν συμβαίνει κάτι τέτοιο στην προσπάθεια να καλύψουμε την ανάγκη καταφεύγουμε σε διάφορες λύσεις χωρίς όμως αυτό πάντα να είναι το ιδανικό.

Μια τέτοια περίπτωση είναι να χρειάζεται να γίνει update σε ένα table αλλά για Ν rows.

Σε SQL Server και Azure SQL Database υπάρχει αυτή η δυνατότητα στην σύνταξη της εντολής. Μάλιστα μπορούμε να επιλέξουμε αν θα είναι απόλυτο νούμερο ή ποσοστό.

Simple case

Κάτι τέτοιο για να γίνει είναι αρκετά απλό, όπως φαίνεται και στο παράδειγμα παρακάτω.

T-SQL Sample

-- with number

update top(2) Production.Products
set unitprice *= 1.1;

-- with percent

update top(2) percent  Production.Products
set unitprice *= 1.1;

ATTENTION
Οι παρενθέσεις είναι υποχρεωτικές στο TOP(N) όταν αυτό χρησιμοποιείται σε INSERT, UPDATE, DELETE statements.

ATTENTION
Όταν χρησιμοποιούμε PERSENT, όπως και στο SELECT επιλέγονται τυχαία οι γραμμές που θα ενημερωθούν

Ordering case

Αν θέλουμε αυτά που θα ενημερωθούν με το TOP(N) να είναι ταξινομημένα εύκολα θα υποθέσει κάποιος ότι με ένα ORDER BY λύνεται αυτό. Όμως αυτό δεν μπορεί να γίνει καθώς δεν υποστηρίζεται το ORDER BY στο UPDATE.

T-SQL Sample

update top(2) Production.Products
set unitprice *= 1.1
order by unitprice;

Error Message
Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'order'.

Σε αυτή την περίπτωση θα πρέπει να ακολουθήσουμε κάποιο από τους παρακάτω τρόπους:

T-SQL Sample - 1st method

with U as 
(
    select top(2) * 
    from Production.Products
    order by unitprice
)
update U
set unitprice *=1.1;

T-SQL Sample - 2nd method

update Production.Products
set unitprice *=1.1
from Production.Products as p
    inner join 
    (
        select top(2) * 
        from Production.Products
        order by unitprice
    ) as o on o.productid = p.productid

//Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.