我有一個SQL INSERT語句如下所示:SQL Server的INSERT語句自動增量
insert into mytable(ID,Keyindex,KeyValue)
select Id,1,GenreID
from mytable2
原本應該填充我的表像數據:
id GenreID ColumnB
0006342c-47bc-436a-a23a-3b40360d9a30 16 1
0006342c-47bc-436a-a23a-3b40360d9a30 19 1
00109775-f0f8-463e-8134-f842aac8b5df 12 1
001211e3-9bf8-45ad-8297-7a0a94aaf06e 13 1
0025218a-9624-4f5e-86cc-f1cfe862cd2a 16 1
0025218a-9624-4f5e-86cc-f1cfe862cd2a 11 1
0025218a-9624-4f5e-86cc-f1cfe862cd2a 15 1
問題是ID,GenreId和ColumnB是主鍵,我正在插入一個常量值「1」,導致主鍵違規。
如何插入表格,使得ColumnB得到一個值incrementaly如果ID是相同的填充。
例如:
id GenreID ColumnB
0006342c-47bc-436a-a23a-3b40360d9a30 16 1
0006342c-47bc-436a-a23a-3b40360d9a30 19 2
00109775-f0f8-463e-8134-f842aac8b5df 12 1
001211e3-9bf8-45ad-8297-7a0a94aaf06e 13 1
0025218a-9624-4f5e-86cc-f1cfe862cd2a 16 1
0025218a-9624-4f5e-86cc-f1cfe862cd2a 16 2
0025218a-9624-4f5e-86cc-f1cfe862cd2a 16 3
工作,這RDBMS使用的是? – 2012-02-24 20:57:15
第一部分中的列名與第二部分中顯示的名稱不同步。 – 2012-02-24 20:58:57
因此,如果GenreID和ColumnB的組合不會唯一,那麼您只需要ColumnB增量?聽起來就像你需要一個觸發器。 – davidethell 2012-02-24 21:00:25