SQL ServerʹÓÃBulk Insert°ÑÒ»¸öÎı¾µ¼Èëµ½Êý¾Ý¿â
This is very common request recently – How to import CSV file into SQL Server? How to load CSV file into SQL Server Database Table? How to load comma delimited file into SQL Server? Let us see the solution in quick steps.
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values.
Create TestTable
USE TestData
GO
CREATE TABLE CSVTest
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO
Create CSV file in drive C: with name csvtest.txt with following content. The location of the file is C:\csvtest.txt
1,James,Smith,19750101
2,Meggie,Smith,19790122
3,Robert,Smith,20071101
4,Alex,Smith,20040202
Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
BULK
INSERT CSVTest
from 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
from CSVTest
GO
--Drop the table to clean up database.
SELECT *
from CSVTest
GO
Ïà¹ØÎĵµ£º
ÈçºÎÈÃÄãµÄSQLÔËÐеøü¿ì
---- ÈËÃÇÔÚʹÓÃSQLʱÍùÍù»áÏÝÈëÒ»¸öÎóÇø£¬¼´Ì«¹Ø×¢ÓÚËùµÃµÄ½á¹ûÊÇ·ñÕýÈ·£¬¶øºöÂÔ
Á˲»Í¬µÄʵÏÖ·½·¨Ö®¼ä¿ÉÄÜ´æÔÚµÄÐÔÄܲîÒ죬ÕâÖÖÐÔÄܲîÒìÔÚ´óÐ͵ĻòÊǸ´ÔÓµÄÊý¾Ý¿â
»·¾³ÖУ¨ÈçÁª»úÊ ......
2009-11-18 12:51:46
ÏÞ¶¨´¦Àí¼Ç¼µÄµ¥Î»£¬rowcount=100±íʽÿ´Î´¦Àí100ÌõÊý¾Ý¡£Êµ¼Ê´¦ÀíµÄ¼Ç¼ÊýÓÉrowcountºÍwhere×Ó¾ä¾ö¶¨¡£Èç¹û·ûºÏwhereµÄ¼Ç¼Êý´óÓÚrowcount,ÔòÓÐrowcount¾ö¶¨£¬Èç¹ûСÓÚrowcount£¬ÔòÓÉwhere¾ö¶¨¡£
create table tb(id int identity(1,1),num int)
insert into tb
values(1)
while @@ ......
declare @i int
set @i=1
while @i<30
begin
insert into test (userid) values(@i)
set @i=@i+1
end
---------------
while Ìõ¼þ
begin
Ö´ÐвÙ×÷
set @i=@i+1
end
WHILE
ÉèÖÃÖØ¸´Ö´ÐÐ SQL Óï¾ä»òÓï¾ä¿éµÄÌõ¼þ¡£Ö»ÒªÖ¸¶¨µÄÌõ¼þÎªÕæ£¬¾ÍÖØ¸´Ö´ÐÐÓï¾ä¡£¿ÉÒÔʹÓà BREAK ºÍ CONTINUE ¹Ø¼ü×ÖÔÚÑ»·ÄÚ²¿¿ ......
參¿¼: http://brightsky006.blog.163.com/blog/static/22583668200962195059485/
°ÑSQL 2005µÄ±¸·ÝÎļþµ¼Èëµ½SQL 2000
ѧϰÕ䲨 2009-07-21 09:50 ÔĶÁ616 ÆÀÂÛ0
×ֺţº ´ó´ó ÖÐÖРСС
µçÄÔÉϰ²×°ÁËSQL Server 2005£¬ÏÖÔÚÏë»»»ØSQL2000À´£¬ ......
¡¡1. ²é¿´Êý¾Ý¿âµÄ°æ±¾
¡¡¡¡select @@version
¡¡¡¡³£¼ûµÄ¼¸ÖÖSQL SERVER´ò²¹¶¡ºóµÄ°æ±¾ºÅ:
¡¡¡¡8.00.194 Microsoft SQL Server 2000
¡¡¡¡8.00.384 Microsoft SQL Server 2000 SP1
¡¡¡¡8.00.532 Microsoft SQL Server 2000 SP2
¡¡¡¡8.00.760 Microsoft SQL Server 2000 SP3
¡¡¡¡8.00.818 Microsoft SQL ......