Click here to Skip to main content
15,123,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sir

I have to import flat file into mssql 2019
But I have created stored procedure to import .txt file using asp.net page
I have fixed path and file name. Its working fine.
I need advice how to do ,user select folder and file name dynamically

Pls advice me
thank you
Maideen

Here is my working code.
Currently folder: C:\textfiles
File name : test.txt

What I have tried:

insert into [dbo].[ZZ_Flat_Data]
(CoCode, CoName, CoAddress1, CoAddress2, CoAddress, CoPost, SPeron, Transporter, DocDate, DocNo, 
Category, Amount, Type)
SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0', 'Text;Database=C:\textfiles; ',
'SELECT CoCode, CoName, CoAddress1, CoAddress2, CoAddress, CoPost, SPeron, Transporter, DocDate, DocNo, 
Category, Amount, Type FROM test.txt')
Posted
Updated 15-Sep-21 17:59pm

1 solution

You can first build the statement into a string and then use EXECUTE (Transact-SQL) - SQL Server | Microsoft Docs[^] to run it.

Consider the following example
SQL
declare @location as varchar(100)
declare @tablename as varchar(100)
declare @query varchar(5000)

set @location = 'C:\textfiles';
set @tablename = 'test.txt';

set @query = 'insert into [dbo].[ZZ_Flat_Data]
(CoCode, CoName, CoAddress1, CoAddress2, CoAddress, CoPost, SPeron, Transporter, DocDate, DocNo, 
Category, Amount, Type)
SELECT * FROM OPENROWSET (''Microsoft.ACE.OLEDB.12.0'', ''' + @location + ''',
''SELECT * FROM ' + @tablename +''')';

print @query
execute(@query)

This would build the string and print it as follows
insert into [dbo].[ZZ_Flat_Data]
(CoCode, CoName, CoAddress1, CoAddress2, CoAddress, CoPost, SPeron, Transporter, DocDate, DocNo, 
Category, Amount, Type)
SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0', 'C:\textfiles',
'SELECT * FROM test.txt')

and the run it using execute
   
Comments
Maideen Abdul Kader 16-Sep-21 0:37am
   
hi.
thank you for your prompt reply. Once run this below is error

Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)"

Pls advise me
thank you
Maideen Abdul Kader 16-Sep-21 1:05am
   
Hi I have added "Text;Database=", Now is working fine.

Thank you very much

Below is final Code

declare @query varchar(5000)
declare @FileName varchar(100)
declare @FilePath varchar(100)
set @FilePath = 'C:\textfiles';
set @FileName = 'test.txt';

set @query = 'insert into [dbo].[ZZ_Flat_Data]
(CoCode, CoName, CoAddress1, CoAddress2, CoAddress, CoPost, SPeron, Transporter, DocDate, DocNo,
Category, Amount, Type)
SELECT * FROM OPENROWSET (''Microsoft.ACE.OLEDB.12.0'',''Text;Database=' + @FilePath + ''',
''SELECT CoCode, CoName, CoAddress1, CoAddress2, CoAddress, CoPost, SPeron, Transporter, DocDate, DocNo,
Category, Amount, Type FROM ' + @FileName +''')';

print @query
execute(@query)
Wendelius 16-Sep-21 14:33pm
   
Sorry, that was a copy paste type I had. adding the Text... is needed, just like in your original example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900