With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
The Grade of the Solution
The Zone Rank of the Expert Providing the Solution
The Number of Author and Expert Comments
The Number of Experts Contributing
The Feedback of the Community
Your Input Matters Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.
If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.
Hello. Using the solutions found on EE, I've created a stored proc that takes each row of a fixed width text file and bulk inserts it as a single string in a row in a temp table. The proc then parses through each row (string) in the temp table and inserts the specified values into my permanent table:
ALTER PROC [Pharmacy].[BulkInsertDDPSFile] @FilePath nvarchar(MAX), @LastRow int AS CREATE TABLE #tmpDDPS ( [strRecord] [nvarchar](MAX) COLLATE SQL_Latin1_General_CP1_CI_AS)
declare @sql varchar(2000)
set @sql = 'BULK INSERT #tmpDDPS FROM ''' + @FilePath + ''' with (FIRSTROW=3, FIELDTERMINATOR=''~'', ROWTERMINATOR=''\n'', LASTROW=''' + @LastRow + ''')' exec (@sql)
I'm having a problem with the @LastRow variable I have in my variable BULK INSERT SQL statement. Because I do not want to import the first 2 rows and the last 2 rows of the text file and because the file length is variable, I want the user to supply the last row number to the sp. Is this possible with the way I have it above, or is there another way to do this?
BTW, this is the error code I get when trying to execute the above sp: Msg 245, Level 16, State 1, Procedure BulkInsertDDPSFile, Line 12 Conversion failed when converting the nvarchar value 'BULK INSERT #tmpDDPS FROM 'C:\DDPS_TRANS_VALIDATI.txt' with (FIRSTROW=3, FIELDTERMINATOR='~', ROWTERMINATOR='\n', LASTROW='' to data type int.
Ignore Lastrow. Instead, add an ID field to your table, and delete the last two rows by ID. This will, of course, require you to use a format file, but you will save thousands of days of headaches.
Alternately, BULK INSERT, Count the rows, then set the LASTROW based on the number of rows inserted and BULK INSERT again.
I found a solution to my problem...Each of the rows has a different starting strind based on what type of record it is - HDR for the header record, TLR for the trailing record, DET for the detail record, etc. I want to only import the DET records so I created a temp table for my rows and the deleted any records that were LIKE %HDR% or %TLR%, thus deleting the last 2 rows and keeping all of the DET records. It works - too bad I can't award myself some points!
I gave bhess1 half the points becuase his solution led me on the path to finding my own workaround. Thx!