简体   繁体   English

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

[英]How create stored procedure with select and update

I have table id, title, showCount . 我有表id, title, showCount

I need get TOP 10 row from table and set to showCount +1 . 我需要从表中获得TOP 10行并将其设置为showCount +1

How do this? 怎么办

CREATE PROCEDURE YourProceduresNameHere

AS

-- Put the code you want to run here

You may find the documentation interesting. 您可能会发现文档有趣。

I can not understand your purpose, but you can try this 我无法理解您的目的,但是您可以尝试一下

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

Also, you need to understand that in this example TOP 10 rows will be updated in random order. 另外,您需要了解,在此示例中, TOP 10行将以随机顺序进行更新。

You can find how to CREATE PROCEDURE in Books OnLine (F1). 您可以在联机丛书(F1)中找到如何创建过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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