Yes it is possible. First you need to load the data into a table with an IDENTITY column:
-- drop table
The result will be:
id Country City ----------- -------------------- -------------------- 1 US New York 2 Washington 3 Baltimore 4 Canada Toronto 5 Vancouver
And now with a little recursive CTE magic, you can fill in the missing details:
;WITH a as( SELECT Country ,City ,ID FROM
Result:
Country City ID -------------------- -------------------- ----------- US New York 1 US Washington 2 US Baltimore 3 Canada Toronto 4 Canada Vancouver 5
Update:
As Tab Alleman suggests below, the same result can be achieved without a recursive query:
SELECT ID , COALESCE(NULLIF(LTrim(a.Country), ''), (SELECT TOP 1 Country FROM
By the way, the format file for your input is this (if you want to try the scripts, save the input as c: \ import.txt and the format file below as c: \ format.fmt):
9.0 2 1 SQLCHAR 0 11 "" 1 Country SQL_Latin1_General_CP1_CI_AS 2 SQLCHAR 0 100 "\r\n" 2 City SQL_Latin1_General_CP1_CI_AS
source share