---
# System prepended metadata

title: 使用CURSOR更新TABLE資料
tags: [MSSQL]

---

# 使用CURSOR更新TABLE資料
###### tags: `MSSQL`

```sql=


DECLARE @TppPlatformID  AS INT;
DECLARE @TppCode AS Varchar(30);
 

DECLARE cursor_tb CURSOR FOR
  SELECT CommonTppPlatformID
  FROM   CommonTppChannelPlatform

OPEN cursor_tb;

FETCH NEXT FROM cursor_tb INTO @TppPlatformID

WHILE @@FETCH_STATUS = 0
  BEGIN 
	  select @TppCode = code from CommonTppPlatform where ID = @TppPlatformID

      UPDATE CommonTppChannelPlatform
      SET  CommonTppPlatformCode = @TppCode
      WHERE  CURRENT OF cursor_tb;
	   
      FETCH NEXT FROM cursor_tb INTO @TppPlatformID
  END;

CLOSE cursor_tb;
DEALLOCATE cursor_tb;  

```