Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bulk Insert FROM Issue 1

Status
Not open for further replies.

mjjks

Programmer
Jun 22, 2005
138
US
Hi All,

I'm using SQL 2005 Express and pass path\filename to stored procedure to import file into table, but get an error in BULK INSERT statement as it doesn't like having a variable as a FROM parameter.



Code:
Create Procedure dbo.spImportAreas(@ProjectPlanKey int, @FilePath varchar(300))

as

[COLOR=green]-- create temp table to store imported data[/color]
create table #temp(ID int IDENTITY not null,
				AreaName varchar(50),MaterialName varchar(50),
				ActualMatlQty decimal(15,3),MatlUOM varchar(10),
				AreaMeasQty decimal(15,3),MatlDescription varchar(100),
				AreaType varchar(30))

[COLOR=green]-- dump content of import file into temp table[/color]
BULK INSERT #temp
   FROM [highlight]@FilePath[/highlight]
   WITH 
      (
         FIELDTERMINATOR =',',
         ROWTERMINATOR ='\n'
      )

I get this error:
[COLOR=#ff0000]
Msg 102, Level 15, State 1, Procedure spImportAreas, Line 15
Incorrect syntax near '@FilePath'.
Msg 319, Level 15, State 1, Procedure spImportAreas, Line 16
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.
[/color]

I tried adding single quotes around @FilePath like this:
''+ @FilePath + '' , but no luck.

Anyone has and idea?

Thanks much
 
use dynamic sql:

exec('
BULK INSERT #temp
FROM '+@FilePath+'
WITH
(
FIELDTERMINATOR ='','',
ROWTERMINATOR =''\n''
)
')

Known is handfull, Unknown is worldfull
 
Thanks for the tip, vbkris.
However, I still get pretty much the same error after using your sample.

There's nothing weird in the file path.
exec spImportAreas 2,'C:\Inetpub\wwwroot\PS\Import\Floorright.txt'


Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'C'.
Msg 319, Level 15, State 1, Line 4
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.
 
Thanks Denis, that worked great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top