July 13, 2008 at 10:54 am
You already said that... this is the third time... relax... someone else will answer...
--Jeff Moden
Change is inevitable... Change for the better is not.
July 13, 2008 at 5:48 pm
Here you go:
Create proc spTable_LoadDynamic (@TableName as sysname, @hdr as varchar(max), @data as varchar(max)) as
/*
Dynamically load a table from a reformatted Excel export file.
13-July-2008, RBarryYoung
Test script:
declare @headers as varchar(max), @file as varchar(max)
Set @headers = 'customer_id~customer_name~Address1~Address2~City~State~Pincode~Age'
--Data_String:
Set @file = '1~Name1~Address1~Address2~Chennai~Tamil Nadu~5744856~85#
2~Name2~Address2~Address3~Madurai~Tamil Nadu~57448562~80#
3~Name3~Address3~Address4~Mysore~Karnataka~570007~79#
4~Name4~Address4~Address5~Bangalore~Karnataka~570007~52#
5~Name5~Address5~Address6~Belgum~Karnataka~570006~74#
6~Name6~Address6~Address7~Delhi~Delhi~5700085~64#
7~Name7~Address7~Address8~Ramnagar~Karnataka~570042~41'
exec spTable_LoadDynamic 'Footst1', @headers, @file
*/
--======create the table
declare @sql nvarchar(max)
Set @sql = 'Drop Table '+@TableName
print @sql
Exec (@sql)
Set @sql = 'Create Table '+@TableName+'('+Replace(@hdr, '~', ' varchar(200), ')+' varchar(200))'
print @sql
Exec (@sql)
--Load the values
--INSERT into foost
-- Select case
Declare @prefix nvarchar(max)
Set @prefix = 'Insert into '+@TableName
Set @sql = 'INSERT into '+@TableName+'('+Replace(@hdr, '~', ',')+')
Select '''+Replace( Replace(@data, '~', ''','''), '#
', '''
UNION ALL Select ''' )+''''
print @sql
Exec(@sql)
go
--==--==--==
declare @headers as varchar(max), @file as varchar(max)
Set @headers = 'customer_id~customer_name~Address1~Address2~City~State~Pincode~Age'
--Data_String:
Set @file = '1~Name1~Address1~Address2~Chennai~Tamil Nadu~5744856~85#
2~Name2~Address2~Address3~Madurai~Tamil Nadu~57448562~80#
3~Name3~Address3~Address4~Mysore~Karnataka~570007~79#
4~Name4~Address4~Address5~Bangalore~Karnataka~570007~52#
5~Name5~Address5~Address6~Belgum~Karnataka~570006~74#
6~Name6~Address6~Address7~Delhi~Delhi~5700085~64#
7~Name7~Address7~Address8~Ramnagar~Karnataka~570042~41'
exec spTable_LoadDynamic 'Footst1', @headers, @file
go
Select * from Footst1
Note I have included a demonstration script at the end.
Note also that this approach might need some adjustment if there are embedded apostrophes or also if the data is "ragged-right", but neither of these should be too difficult.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
July 14, 2008 at 12:09 am
Awesome , Its working fine...Thanks for all you supports..
July 15, 2008 at 6:02 am
You can use XML Datatype and put the string that you need into the column as xml, so you don't have ant problems getting it back and translating it to what ever structure that you like/need .
Michael
July 15, 2008 at 7:06 am
I know disk space is relatively cheap... I just can't bring myself to suffer the overhead of storing XML in a database anywhere except a temporary staging table. With respect to the load on the "pipe", I also prefer not to use it for passing parameters, either.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 15, 2008 at 9:15 am
XML is pure evil in the data tier. I don care if XPATH make my job 1000% easier to wade through 500k xml blobs. A realtional engine should not have to deal with markup code. TDS protocol is not optimized for XML. Haphazard use of XML is costing our industry millions of phyiscal dollars.
Stop the insanity :w00t:
sorry for the rant, I am very anti-XML 🙁
July 15, 2008 at 12:08 pm
Heh... I feel the same... but I said "Today, just this one day, I shall not rant." 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
July 15, 2008 at 12:10 pm
🙂 Rants cary over, so you get two tommorrow.
July 15, 2008 at 1:54 pm
Hi,
Basically you need to use Split functiont to break the string.
http://www.dotnetspider.com/resources/1266-Split-e-String-Using-char-separator.aspx
Thanks -- Vj
July 15, 2008 at 11:45 pm
I wouldn't recommend that way... it has a While loop in it and a couple of other performance related problems. Try the following, please... it also explains how it works... split code is near the end...
http://www.sqlservercentral.com/articles/TSQL/62867/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 16 through 24 (of 24 total)
You must be logged in to reply to this topic. Login to reply