Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 BULK INSERT

Author  Topic 

mwendel2
Starting Member

3 Posts

Posted - 2010-05-17 : 15:04:11
I have been reading on this all day and it would appear that when I do @pathfile in the FROM portion of the BULK INSERT statement the entire file's contents are pulling into @pathfile?

Is this correct? (see query below) If yes, is there a way to make what I am doing work as just a passed file name? If not, any suggestions as to why the query is returning:

Msg 4860, Level 16, State 1, Line 8
Cannot bulk load. The file "@pathfile" does not exist.

DECLARE @counter Int
DECLARE @pathfile varchar(500)
SET @counter = 3
WHILE @counter < 32
BEGIN
SET @pathfile = 'C:\clientname\OrderReport\Processed\2009-09\OrderSummary_2009090' + RTRIM(CAST(@counter AS CHAR)) + '.txt'
PRINT 'The file is ' + @pathfile
BULK INSERT clientname.dbo.OrderSummary
FROM '@pathfile'
WITH
(
FIRSTROW = 2,
BATCHSIZE = 1,
MAXERRORS = 100,
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)
SET @counter = @counter + 1
END
GO

Thanks in advance for any assistance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-17 : 15:12:07
Remove the single quotes around @pathfile. Do these files and path exist on the database server or on a client machine?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mwendel2
Starting Member

3 Posts

Posted - 2010-05-17 : 15:16:19
I am running everything locally on my laptop.

When I removed the quotes around @pathfile, I then get:

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '@pathfile'.
Msg 319, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-17 : 15:28:10
Looks like you'll need to use dynamic SQL then for this. Build your BULK INSERT statement into a variable (@sql or similar) and then use EXEC (@sql) to execute it.

BULK INSERT runs from the perspective of the database server, so if SQL Server is on a remote machine, then you'll need to either use UNC or copy the files to the database server.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mwendel2
Starting Member

3 Posts

Posted - 2010-05-18 : 09:47:26
Thank you. That worked. Now I have to deal with other issues as a result of getting past that point. :)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-18 : 13:18:49


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -