问题
2 tables 1 with clients other with accno information
table 1:
file, accno <- nvarchar(50)
t1, 123a
t1, 456a
t1, 789a
t2, 012b
t3, 345c
t3, 678c
i would like to transfer table 1 to table 2
table 2:
file, accno <- nvarchar(max)
t1, 123a
456a
789a
t2, 012b
t3, 345c
678c
i can do this is delphi very easy, but itll take forever almost 6 hrs because im filtering close to 300000 records on table 1
is there an sql query that can make this go super fast, even if it takes 5min
回答1:
SELECT
DISTINCT [File]
,STUFF(
(SELECT ',' + accno
FROM
TableName it
WHERE it.[File] = ot.[File]
FOR XML PATH(''))
,1,1,'') AS [Memo]
FROM
TableName ot
You can adapt this but this will create a comma delimited list of your accno which I think is kind of what you are looking for? You can modify to do it with line endings in your Memo Field rather than commas.
回答2:
k so it turns out that the funny gizmozs on the ends was carriage returns, i managed to fix that by modifying query with the below
SELECT
DISTINCT fileref, stuff(
(
Select '; ' + IsNull( D2.viaccno, '' )
From vi_accno As D2
Where D2.vifileref = A.FileRef
For Xml Path(''), type
).value('.', 'nvarchar(max)')
, 1, 2, '') As SelectedComments
FROM
vtindex a
Thanks to @Matt for the help and response
来源:https://stackoverflow.com/questions/38087645/combine-multiple-rows-into-one-memo-nvarcharmax