[英]How to create a select, and then an update stored procedure in SQL Server 2012
[英]How create stored procedure with select and update
我有表id, title, showCount
。
我需要從表中獲得TOP 10
行並將其設置為showCount +1
。
怎么辦
CREATE PROCEDURE YourProceduresNameHere
AS
-- Put the code you want to run here
您可能會發現文檔有趣。
我無法理解您的目的,但是您可以嘗試一下
CREATE TABLE #tbl (id int identity(1,1), title varchar(50), showCount int)
INSERT INTO #tbl (title, showCount)
VALUES ('q',1),('qw',2),('qe',3),('qr',4),('qt',5),('qy',6),('qu',7),('qh',8),('qx',9),('qs',10), ('qs',100)
UPDATE T1
SET T1.showCount=T1.showCount+1
FROM #tbl T1
JOIN (SELECT TOP 10 id, showCount
FROM #tbl) T2 ON T1.id=T2.id
SELECT *
FROM #tbl
DROP TABLE #tbl
另外,您需要了解,在此示例中, TOP 10
行將以隨機順序進行更新。
您可以在聯機叢書(F1)中找到如何創建過程 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.