繁体   English   中英

如何使用选择和更新创建存储过程

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM