Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted Nov 23, 2009

Importing flatfile data to MSSQL table

By DatabaseJournal.com Staff



>>Script Language and Platform: MSSQL
This script is used to read the data from a flat file and then populate it into the database table.

Currently the path and the table names are hardcoded which can be made a parameter variable if the path is not fixed.

Author: Alok Vyas


SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

alter procedure loaddata as
BEGIN
declare @query varchar(7000)
declare @string varchar(1500)
declare @string1 varchar(500)
drop table x
create table x (name varchar(2000))
set @query ='master.dbo.xp_cmdshell "type C:\SAMPLE_FILE.TXT"'
insert x exec (@query)
declare C1 cursor local fast_forward for select name from x;
open c1
fetch next from c1 into @string
while @@FETCH_STATUS = 0
	BEGIN
	fetch next from c1 into @string1
		if (len(@string1)<>0 or @string1<>null)
		  Begin	
			set @string=@string+@string1
		  END
		ELSE if  @string<>''
                    Begin
			insert into filedata values(
			substring(@string,1,16),
			substring(@string,17,35),
			substring(@string,52,25),
			substring(@string,77,25),
			substring(@string,102,25),
			substring(@string,127,25),
			substring(@string,152,25),
			substring(@string,177,20),
			substring(@string,197,15),
			substring(@string,212,15),
			substring(@string,227,50),
			substring(@string,277,15),
			substring(@string,292,17),
			substring(@string,309,17),
			substring(@string,326,17),
			substring(@string,343,17),
			substring(@string,360,17),
			substring(@string,377,17),
			substring(@string,394,3),
			substring(@string,397,6),
			substring(@string,403,4),
			substring(@string,407,6),
			substring(@string,413,40),
			substring(@string,453,15),
			substring(@string,468,1),
			substring(@string,469,30),
			substring(@string,499,30),
			substring(@string,529,30),
			substring(@string,559,30),
			substring(@string,588,30),
			substring(@string,619,30),
			substring(@string,649,30),
			substring(@string,679,30),
			substring(@string,709,30),
			substring(@string,739,30))
			set @string=''
		   End
	END
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Disclaimer: We hope that the information on these script pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, or fitness for a particular purpose... Disclaimer Continued


Back to Database Journal Home



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date