Avatar of jjliu4492
jjliu4492

asked on 

How to import file information from a file system into a sql server table

I have files of type jpg, tif, and txt located on my computer, and I want to be able to import data from those files into two tables in sql server. Depending on the file type, I want to import to either one or the other table. How would I go about writing a script to do this? Also, how can I store the path of the file into the database as well?
Files.docx
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
jjliu4492
Avatar of Krtyknm
Krtyknm
Flag of India image

You can create a table with the datatype as Image or Varbinary. Then insert the path or file into the table as below.
CREATE TABLE tblData
(
	id INT IDENTITY (1,1),
	FilePath NVARCHAR(MAX),
	FileData VARBINARY(MAX),
	Extn NVARCHAR(20)
)

DECLARE @FilePath NVARCHAR(MAX);
DECLARE @Extn NVARCHAR(20)

SET @FilePath ='C:\Documents and Settings\manik\Desktop\DateFormat.Jpg'
SET @Extn = SUBSTRING(@FilePath,CHARINDEX('.',@FilePath)+1,LEN(@FilePath))

INSERT INTO tblData 
(
	FilePath,
	FileData,
	Extn
)
SELECT @FilePath,@FilePath,@Extn

Open in new window

Avatar of jjliu4492
jjliu4492

ASKER

If all of my files (jpg, tif, and txt) are located within the same folder, how would I determine which table I should be writing to based upon the file extension? How would I write that into the script?
Avatar of jjliu4492
jjliu4492

ASKER

And I want to be able to loop through all the files in the folder, and do the insert for each one, and then move onto the next file. How would I go about doing that in the script?
Avatar of kaminda
kaminda
Flag of Sri Lanka image

You can not do that solely using TSQL. You need the help of a programming language to do this. Anyway Powershell script whould be ideal for you..

This describes how you can iterate through files using powershell

http://technet.microsoft.com/en-us/library/ee176988.aspx

This describes how you can run a sql script from powershell

http://serialseb.blogspot.com/2007/09/executing-sql-scripts-from-powershell.html

If you are new to powershell just google about it. Its awsome scripting language.
Avatar of jjliu4492
jjliu4492

ASKER

Is there a way to loop through the files, and then insert into one of the two tables (depending upon the file type), by using SSIS?
ASKER CERTIFIED SOLUTION
Avatar of kaminda
kaminda
Flag of Sri Lanka image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of lcohan
lcohan
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of jjliu4492
jjliu4492

ASKER

How can I insert the filename from the foreach loop container into my sql server table in an SSIS package? Is there a way to extract other data from the file, such as date, title/name, location, file type, etc?
Avatar of jjliu4492
jjliu4492

ASKER

I am trying to build an Execute SQL task in my dtsx package, with the Expressions Property = SQLStatementSource, and the Expression = INSERT INTO dbo.t_vadoc ([vadoc_batch], [vadoc_docpack], [vadoc_firstimage], [vadoc_lastimage], [vadoc_doctype], [vadoc_firstpage], [vadoc_lastpage], [vadoc_contno])
VALUES (GetDate(), GetDate() , REPLACE( @[User::FileName],'_FIRST', '' ) , REPLACE( @[User::FileName],'_LAST', '' ) , 'GRAPHIC', REPLACE( @[User::FileName],'_FIRST', '' ) , REPLACE( @[User::FileName],'_LAST', ''), GetDate()).

When I try and evaluate the expression, I get the following error message. What am I doing wrong?


ErrorMessage.docx
Microsoft SQL Server 2008
Microsoft SQL Server 2008

Microsoft SQL Server 2008 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. Major improvements include the Always On technologies and support for unstructured data types.

50K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo