简体   繁体   中英

How create stored procedure with select and update

I have table id, title, showCount .

I need get TOP 10 row from table and set to 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.

You can find how to CREATE PROCEDURE in Books OnLine (F1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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