在UPDATE语句中,LIMIT等效于SQL Server 2012

时间:2016-10-12 08:49:15

标签: sql sql-server-2012

我有下面的表格,我正在尝试通过查询更新带有用户ID的第一个可用行,但我需要将其限制为仅更新一行而不是多行。

ID     Model     UserID
1      X12T5     1
2      X13T5     2
3      X14T5     NULL
4      X15T5     NULL

第一个可用行是ID为3.我将使用以下查询更新它:

UPDATE Table SET UserID = '3' WHERE UserID IS NULL

但是我想确保它只影响1行而不是多个可用的,SQL Server中不存在LIMIT。

实现这一目标的最佳方法是什么?

4 个答案:

答案 0 :(得分:3)

您可以使用SELECT TOP执行此操作。它等同于TOP,但用于更新;而LIMIT是SQL Server相当于MySQL的{{1}}。

请参阅further info

答案 1 :(得分:1)

UPDATE Table SET UserID = '3'
WHERE UserID IS NULL
 AND Id IN (SELECT top 1 ID FROM table where UserId IS NULL)

答案 2 :(得分:0)

您还可以使用以下任何方法(假设列ID是整数数据类型)

使用UPDATE a SET a.UserID='3' FROM YourTable a WHERE ID =( SELECT MIN(ID) FROM YourTable WHERE UserID is NULL)

JOIN

使用UPDATE a SET a.UserID='3' FROM YourTable a JOIN ( SELECT MIN(ID) MinId FROM YourTable WHERE UserID is NULL) b ON a.ID=b.MinId

CTE

使用WITH cte_a AS (SELECT MIN(ID) MinId FROM YourTable WHERE UserID is NULL) UPDATE a SET a.UserID='3' FROM YourTable a JOIN cte_a b a.ID=b.MinId

My code index.html:
           <div class="container" ng-controller="myCrudController">
    <div class="row">
        <div class="col s12">
            <h4>Products</h4>
            <form id="modal-product-form" class="modal" >
                <div class="modal-content">
                    <h5 id="modal-product-title"></h5>
                    <div class="row">
                        <div class="input-field col s12">
                            <input  type="text" class="validate" id="form-name" placeholder="Type name here..." />
                            <label for="name">Name</label>
                        </div>

                        <div class="input-field col s12">
                            <textarea  type="text" class="validate materialize-textarea" placeholder="Type description here..."></textarea>
                            <label for="description">Description</label>
                        </div>


                        <div class="input-field col s12">
                            <input  type="text" class="validate" id="form-price" placeholder="Type price here..." />
                            <label for="price">Price</label>
                        </div>


                        <div class="input-field col s12">
                            <a id="btn-create-product" class=" btn margin-bottom-1em"  ng-click="createProduct()"><i class="material-icons left">add</i>Create</a>

                            <a id="btn-update-product" class=" btn margin-bottom-1em" ><i class="material-icons left">edit</i>Save Changes</a>

                            <a class="modal-action modal-close  btn margin-bottom-1em"><i class="material-icons left">close</i>Close</a>
                        </div>
                    </div>
                </div>
            </form>
            <!-- floating button for creating product -->
            <div class="fixed-action-btn" style="bottom:45px; right:24px;">
                <a class=" btn btn-floating btn-large red modal-open" href="#modal-product-form" ng-click="showCreateForm()"><i class="large material-icons">add</i></a
            </div>
        </div>
    </div>

</div>

答案 3 :(得分:0)

您还可以使用CTE&amp;执行SELECT\UPDATE\DELETE操作,

; WITH CTE AS (
SELECT TOP 10 * FROM TABLE
)
UPDATE CTE 
SET ...

DELETE\UPDATE\SELECT与CTE合作......很酷!!!