Link to home
Start Free TrialLog in
Avatar of adamn123
adamn123

asked on

make bulk insert work from a value

what am I doing wrong?
I am sure it is something simple but I can not find a solution
I am trying to make the following code

--Bulk insert into Test
BULK INSERT Serials_temp
FROM 'C:\test.csv'
WITH
(
FIELDTERMINATOR=',',
ROWTERMINATOR = '\n'
)
DECLARE @my_user_value VARCHAR(50)
SET @users  = ('adam')
 
INSERT INTO Serials (serial, users)
SELECT serial, @users
FROM Serials_temp

but make 'C:\test.csv'  be the value @fileloc






set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[serialupload]
(
     @users VARCHAR(50),
     @fileloc VARCHAR(50)
 )
AS
BEGIN

--Bulk insert into Test
DECLARE @SQL varchar(2000)
SET @SQL = "BULK INSERT Serials_temp FROM '"+@fileloc+"' WITH (FIELDTERMINATOR = '\n') "
EXEC (@SQL)

 
INSERT INTO Serials (serial, users)
SELECT serial, @users
FROM Serials_temp

END

I am getting error


Msg 207, Level 16, State 1, Procedure serialupload, Line 11
Invalid column name 'BULK INSERT Serials_temp FROM ''.
Msg 207, Level 16, State 1, Procedure serialupload, Line 11
Invalid column name '' WITH (FIELDTERMINATOR = '\n') '.
SOLUTION
Avatar of mdagis
mdagis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adamn123
adamn123

ASKER

I am getting error

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'c'.
Msg 319, Level 15, State 1, Line 1
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.

I ran it and if  it helps the fileloc I put as c:\CSVLoad\1\04-23-10-10-39-44-serial.csv  and users was adam
ok got it to work if I make the value

'c:\CSVLoad\1\04-23-10-10-39-44-serial.csv'\
not
c:\CSVLoad\1\04-23-10-10-39-44-serial.csv

need to modify the code to auto put in the ' '
can someone help me so the code puts the ' ' for me
I tried many ways I thought would work but does not
don't want to upload teh value to have them want the insert to have them please
SET @SQL = 'BULK INSERT Serials_temp FROM '''  + @fileloc +  ''' WITH (FIELDTERMINATOR = ''\n'')'

Thanks all for the help :)