|
I've inherited a horrible stored proc to load an XSL file from a text file into one TEXT column in one row. It BULK INSERTs into a temporary table and then cursors through that somehow appending the lines together into a TEXT variable using TEXTPTR and UPDATETEXT. The whole thing is in a transaction. What could go wrong? Well, it's suddenly started occasionally not loading some of the start of the file. No errors, it just sometimes misses off the first n bytes (always the same number of bytes). I have a recipe like this that works: Is there a cleaner way? Maybe one that doesn't use xp_cmdshell?
(comments are locked)
|
|
Here is the sample showing how to insert the guts of the file to the table without using the xp_cmdshell: The results display (because this what was in the file): Of course it goes without saying that the file path is relative to the server. Oleg That works like a charm. Thanks, Oleg.
Jul 26 '10 at 09:07 AM
David Wimbush
@David Wimbush Thank you. In my answer, the path to the file is hardcoded because openrowset will not accept a path as a parameter. This means that should the file path be a parameter, the insert into select from should be spelled out into a varchar variable which can be fed to the execute in order to bypass this limitation of the openrowset. For example,
Jul 26 '10 at 09:15 AM
Oleg
So I discovered! But you put me on the right track. Thanks.
Jul 26 '10 at 02:41 PM
David Wimbush
(comments are locked)
|
|
If you can create a CLR assembly with EXTERNAL_ACCESS, then I would go that route. Given that you have a nvarchar(4000) column, you will want to split larger text files into smaller chunks. You will also want it not to epic fail on Unicode. A CLR table function could be a neat way to achieve the above two without causing the massive hole that xp_cmdshell does. Thanks Matt. +1 because I think this is probably the technically superior solution. But this is a live problem a quick fix is needed so I'm going with Oleg's answer in this case.
Jul 26 '10 at 09:06 AM
David Wimbush
@David Wimbush - fair play - but do remember that if you need Unicode support (which the nvarchar suggests) then you will need to do it properly...
Jul 26 '10 at 09:13 AM
Matt Whitfield ♦♦
@Matt Whitfield +1 This is a very sound solution! As a matter of fact, you gave me an excellent idea on the task I am working right now. Thank you.
Jul 26 '10 at 09:33 AM
Oleg
(comments are locked)
|


Do you have SSIS? or Filestream?