Scenario:-
We have database on a vendor supported application in which a Table TestIdt has a integer column col1 which is not specified as identity as below,
--Create Table
CREATE TABLE [dbo].[TestIdt]
(
[col1] int NOT NULL,
[col2] [varchar](60) NULL,
[col3] [varchar](60) NULL,
[col4] [varchar](50) NULL,
CONSTRAINT [PK_TestIdt] PRIMARY KEY CLUSTERED
(
[col1] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS =ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
--Insert Values to the Table
INSERT INTO TestIdt VALUES (1,'Britan','London','Football')
INSERT INTO TestIdt VALUES (2,'Wales','Cardiff','Football')
INSERT INTO TestIdt VALUES (3,'Scotland','Edinburgh','Football')
INSERT INTO TestIdt VALUES (4,'France','Paris','Football')
INSERT INTO TestIdt VALUES (5,'Ireland','Dublin','Football')
Now we need to insert values of another table(subsetTbl as below) to TestIdt with col1 on TestIdt need to be increaed by 1 with each row in the subset, In short Col1 should be populated like an identity column.
CREATE TABLE [dbo].[subsetTbl]
(
[subcol2] [varchar](60) NULL,
[subcol3] [varchar](60) NULL,
[subcol4] [varchar](50) NULL,
) ON [PRIMARY]
--Insert Values to the Table
INSERT INTO subsetTbl VALUES ('Germany','Berlin','Tennis')
INSERT INTO subsetTbl VALUES ('Swiss','Bern','Tennis')
INSERT INTO subsetTbl VALUES ('Italy','Rome','Tennis')
INSERT INTO subsetTbl VALUES ('Belgium','Brussels','Tennis')
INSERT INTO subsetTbl VALUES ('Spain','Madrid','Tennis')
Let us see how we can Accomplish this task, One way of doing it is using SQL cursors which looks fine till we have small tables but with large tables and inserts cursor has its own disadvantages of using more resources and
more locking on the table. With the introduction of OVER clause on SQL 2005 we can accomplish this task pretty easily using rownumber function as below,
DECLARE @cnt int
SELECT @cnt = MAX(col1) from TestIdt
INSERT INTO TestIdt
select @cnt + (row_number() OVER (ORDER BY subcol2 ASC)) As Col1,subcol2,subcol3,subcol4 from subsetTbl
col1 | col2 | col3 | col4 |
1 | Britan | London | Football |
2 | Wales | Cardiff | Football |
3 | Scotland | Edinburgh | Football |
4 | France | Paris | Football |
5 | Ireland | Dublin | Football |
6 | Belgium | Brussels | Tennis |
7 | Germany | Berlin | Tennis |
8 | Italy | Rome | Tennis |
9 | Spain | Madrid | Tennis |
10 | Swiss | Bern | Tennis |
Now that we have inserted subsetTbl values to TestIdt with Col1 values incremented by 1, We can specify the start value as we wish, Let say I want to start with 100 for next insert We can do it as follows,
DECLARE @cnt int
SET @cnt = 99
INSERT INTO TestIdt
select @cnt + (row_number() OVER (ORDER BY subcol2 ASC)) As Col1,subcol2,subcol3,subcol4 from subsetTbl
col1 | col2 | col3 | col4 |
1 | Britan | London | Football |
2 | Wales | Cardiff | Football |
3 | Scotland | Edinburgh | Football |
4 | France | Paris | Football |
5 | Ireland | Dublin | Football |
6 | Belgium | Brussels | Tennis |
7 | Germany | Berlin | Tennis |
8 | Italy | Rome | Tennis |
9 | Spain | Madrid | Tennis |
10 | Swiss | Bern | Tennis |
100 | Belgium | Brussels | Tennis |
101 | Germany | Berlin | Tennis |
102 | Italy | Rome | Tennis |
103 | Spain | Madrid | Tennis |
104 | Swiss | Bern | Tennis |
We have eventually created a Identity Insert for a Non-Identity Column.
Copyright © 2012 Vinoth N Manoharan.The information provided in this post is provided "as is" with no implied warranties or guarantees.
GREAT THANKS
ReplyDeletehttps://bayanlarsitesi.com/
ReplyDeleteGöktürk
Yenidoğan
Şemsipaşa
Çağlayan
O4572
görüntülü show
ReplyDeleteücretlishow
DSN
Çorlu Lojistik
ReplyDeleteManisa Lojistik
Eskişehir Lojistik
Afyon Lojistik
Konya Lojistik
EK5ZB
5E043
ReplyDeleteBinance Referans Kodu
Ankara Parke Ustası
Tekirdağ Evden Eve Nakliyat
Konya Şehir İçi Nakliyat
Artvin Şehir İçi Nakliyat
Bursa Şehirler Arası Nakliyat
Amasya Lojistik
Antalya Şehirler Arası Nakliyat
Bolu Evden Eve Nakliyat
B2A5F
ReplyDeleteKripto Para Nasıl Çıkarılır
Kripto Para Nasıl Oynanır
Kripto Para Madenciliği Siteleri
Clubhouse Takipçi Satın Al
Referans Kimliği Nedir
Referans Kimliği Nedir
Görüntülü Sohbet
Onlyfans Beğeni Satın Al
Binance Hesap Açma
85D16
ReplyDeleteweb3 phantom
dcent web
ledger
wallet trust
web arculus
safepal wallet
onekey wallet
ledger live
ronin chain
69467
ReplyDeleteFuckelon Coin Hangi Borsada
Binance Referans Kodu
Parasız Görüntülü Sohbet
Alya Coin Hangi Borsada
Coin Üretme
Youtube Beğeni Satın Al
Referans Kimliği Nedir
Linkedin Beğeni Hilesi
Coin Kazanma Siteleri
FD062
ReplyDeletebinance
bingx
copy trade nedir
huobi
bingx
btcturk
binance
gate io
binance
230FF
ReplyDeleteReseller Hosting
Lisans Satışı
Boştaki Domainler
Tiktok SEO
silkroad pvp serverler
pvp
SEO Ajansı
Instagram Reklam Verme
knight online pvp serverler
7C77D
ReplyDeleteGal Coin Yorum
Grt Coin Yorum
Stg Coin Yorum
BTC Yorum
Oxt Coin Yorum
Xlm Coin Yorum
Req Coin Yorum
Vite Coin Yorum
BTC Forum
109FE
ReplyDeletesanal ücretli show
B28E0
ReplyDeletegörüntülü show
3B94D3C5A3
ReplyDeletetelegram show
skype şov
bufalo çikolata
ücretli show
görüntülü şov
whatsapp ücretli show
görüntülü şov whatsapp numarası
novagra
bufalo içecek
5EBE65E389
ReplyDeletetelegram show
novagra
whatsapp görüntülü show güvenilir
cialis
kaldırıcı
yapay kızlık zarı
viga
görüntülü şov
degra
2FD1FFFC22
ReplyDeletesinegra
whatsapp görüntülü şov
whatsapp ücretli show
green temptation
skype şov
maxman
fx15
viagra
delay
D72C5BB8E9
ReplyDeletewhatsapp görüntülü şov
DA5F4F5616
ReplyDeletetwitter ucuz beğeni
B7E7B61F8F
ReplyDeletebayan beğeni
3D074D6028
ReplyDeletesteroid satın al
görüntülü show
cialis
www.ijuntaxmedikal.store
steroid satın al
şov