Thursday, March 22, 2012

Bulk Insert Statement

I am trying to perform bulk insert but it is not working.

I created a table

INSERT INTO [demo].[dbo].[test]

([ID] ,[Plates] ,[Driver])

VALUES

(<ID, int,> ,<Plates, varchar(7),> ,<Driver, varchar(7),>)

I am tryin to insert a file.txt

100, 091-184, DOUG798
101, 406-846, DALL152
102, 384-080, TIZE489
103, 064-460, NAMO927
104, 101-366, CETI001
105, 109-366, JESS111

Any help, please

Juvan

hi Juvan,

the syntax you used is not correct for BULK INSERT statement,
have a look at http://msdn2.microsoft.com/en-us/library/ms188365.aspx for the statement's full synopsis and syntax..

/*

[FILE d:\fmt.txt]

9.0

3

1 SQLCHAR 0 10 "," 1 FirstName "Latin1_General_CI_AS"

2 SQLCHAR 0 10 "," 2 LastName "Latin1_General_CI_AS"

3 SQLCHAR 0 8 "\r\n" 3 BDate "Latin1_General_CI_AS"

[/FILE d:\fmt.txt]

[FILE d:\studs.txt]

Juvan, Bonni, 19701015

Andrea, Montanari, 19651030

[/FILE d:\studs.txt]

*/

SET NOCOUNT ON;

USE tempdb;

GO

CREATE TABLE dbo.Students(

FirstName varchar(10) NOT NULL,

LastName varchar(10) NOT NULL,

BDate datetime NOT NULL

);

GO

BULK INSERT dbo.Students

FROM 'd:\studs.txt'

WITH(FORMATFILE = 'd:\fmt.txt') ;

GO

SELECT * FROM dbo.Students;

GO

DROP TABLE dbo.Students;

--<--

FirstName LastName BDate

- - --

Juvan Bonni 1970-10-15 00:00:00.000

Andrea Montanari 1965-10-30 00:00:00.000

regards

No comments:

Post a Comment