Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Obtain a value and insert the increase of this value into the sql server

$
0
0

I Have a table in SQL SERver with name T_ACCOUNT with 2 columns ACCOUNT_ID and CUSTOMER_ID both as int. Now i have to read the last row of that table and insert a new row with the values of last row incrementing by 1. How can I do this?

I tried like this

SELECT ACCOUNT_ID,CUSTOMER_ID FROM T_ACCOUNT order by ACCOUNT_ID desc INSERT T_ACCOUNT(ACCOUNT_ID,CUSTOMER_ID) VALUES(ACCOUNT_ID = ACCOUNT_ID + 1 AND CUSTOMER_ID = CUSTOMER_ID +1)

but it was showing the syntax error.can anyone help me

The below statement picks the last row (record with max account_id) and inserts the next row.

INSERT INTO T_ACCOUNT (ACCOUNT_ID,CUSTOMER_ID) SELECT ACCOUNT_ID + 1, CUSTOMER_ID +1 FROM T_ACCOUNT Where ACCOUNT_ID = (select max(ACCOUNT_ID) from T_ACCOUNT)

The where clause is used in picking the last row.


Viewing all articles
Browse latest Browse all 3160