火曜日, 10月 02, 2007

SQL SERVER MODE: Inserting Multiple Records Using One Insert Statement - Use of UNION ALL

My current CRF is to add 134 entries in a table. Initially, I have thought my query to be like this one:

Use Database
Insert INTO Table (Col1, Col2)
VALUES ('Hello','World');
Insert INTO Table (Col1, Col2)
VALUES ('Hello','Philippines');
Insert INTO Table (Col1, Col2)
VALUES ('Hello','Quezon City');
Insert INTO Table (Col1, Col2)
VALUES ('Hello','Gilmore');
Insert INTO Table (Col1, Col2)
VALUES ('Hello','Sykes');

kahit copy-paste lang ang gagawin ko, ang fanet na paulit-ulit si INSERT. So... I asked for google's help for this. Gusto ko na isang INSERT lang ang gagamitin ko. Fortunately, pwede ngang ISA LANG! hahaha. An alternative to repeated INSERT is the use of INSERT INTO...SELECT...UNION ALL statement.

Use Database
INSERT INTO Table (Col1, Col2)
SELECT 'Hello','World'
UNION ALL
SELECT 'Hello','Philippines'
UNION ALL
SELECT 'Hello','Quezon City'
UNION ALL
SELECT 'Hello','Gilmore'
UNION ALL
SELECT 'Hello','Sykes'
UNION ALL

hehe. kahit copy-paste sia, kung sa performance basis, the alternative way is much more efficient. the difference can be seen pag mas malaking entries ang ilalagay sa table. ayus! new learning for today. :D

Thanks to http://blog.sqlauthority.com/2007/06/08/sql-server-insert-multiple-records-using-one-insert-statement-use-of-union-all/ for this helpful article. ^__^

---
WOOOT PROGRAMMER KUNO ANG POST KO! hahaha. JUST SHARING SOMETHING VALUABLE. :D

2 comments:

Mylene さんのコメント...

wow! ok 'to ah. may ganyan din ako karaming updates dati. pinagtyagahan kong isa-isahin huhu.

now i know may mas madaling way pala hehe. matsala!

M A K R E さんのコメント...

may isshare na ko sa new learning! hehehehe. :D