November 11, 2008 at 9:39 pm
Steve T (11/11/2008)
Sounds like a bank/credit feed file. There's probably no data structure other than 2 columns. 1 for the header record you manually added and the other is probably the actual data, but this is just a guess.Assuming your data is similar to this...
HTEST1
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8
Record9
HTEST2
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8
Record9
HTEST3
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8
Record9
This is what I did:
CREATE TABLE #InputData
(Cnt INT IDENTITY(1,1), Data VARCHAR(1000))
DECLARE MyCursor CURSOR
FOR SELECT Cnt, Data FROM #InputData ORDER BY Cnt
OPEN MyCursor
DECLARE @Cnt INT, @data VARCHAR(1000), @Header VARCHAR(10)
FETCH NEXT FROM MyCursor
INTO @Cnt, @data
WHILE @@FETCH_STATUS = 0
BEGIN
IF SUBSTRING(@Data, 1, 1) = 'H'
BEGIN
SET @Header = SUBSTRING(@Data, 2, 10)
INSERT INTO #Final
SELECT 'H', NULL, @Header
END
ELSE
BEGIN
INSERT INTO #Final
SELECT 'D', @data, @Header
END
FETCH NEXT FROM MyCursor
INTO @Cnt, @data
END
CLOSE MyCursor
DEALLOCATE MyCursor
SELECT * FROM #Final
No... not guaranteed to work because there's nothing to guarantee the order of the data.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 11, 2008 at 9:42 pm
SQL DBA 808 (11/11/2008)
Steve T: Yes. That's very close to what I need to do. Thank you.Garadin: I will read that and see if I can apply it to this situation before my management kills me for not having this process completed sooner.
Heh... been there, done that. If you could take the time to post the table structure and some sample data using the methods found in the link in my signature below, maybe we could keep management from killing you. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
November 12, 2008 at 3:56 am
I don't think you need a cursor & while statement either, but as others have said, we need the table structure before we can accurately help you. Don't assume the SQL given before will actually resolve your problem. Especially if you're just assuming the records following the header happen to be associated with the header.
Remember what they say about assumptions. @=)
November 13, 2008 at 8:10 am
In what format are you getting the data and how is it inserted into Sql Server? Since there is no guaranteed order of the data, the time to assign an "order" field would be when it is inserted into a Sql-Server table.
Steve
November 13, 2008 at 8:15 am
I haven't given up, I am just taking some time to read the posts that were given. I'll give an update when I have one.
Viewing 5 posts - 16 through 19 (of 19 total)
You must be logged in to reply to this topic. Login to reply