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
相关文档:
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 @@ ......
使用整数数据的精确数字数据类型。
bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字)。
存储大小为 8 个字节。
int 从 -2^31 (-2,147,483,648) 到 2^31 - 1 (2,147,483,64 ......
sql server2000中使用convert来取得datetime数据类型样式(全)
日期数据格式的处理,两个示例:
CONVERT(varchar(16), 时间一, 20) 结果:2007-02-01 08:02/*时间一般为getdate()函数或数据表里的字段*/
CONVERT(varchar(10), 时间一, 23) 结果:2007-02-01 /*varchar(10)表示日期输出的格式,如果不够长会发生截取*/ ......
[C#]
public void RunSqlTransaction(string myConnString)
{
SqlConnection myConnection = new SqlConnection(myConnString);
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
SqlTransaction m ......
參考: http://brightsky006.blog.163.com/blog/static/22583668200962195059485/
把SQL 2005的备份文件导入到SQL 2000
学习珍藏 2009-07-21 09:50 阅读616 评论0
字号: 大大 中中 小小
电脑上安装了SQL Server 2005,现在想换回SQL2000来, ......