最近在一个网站上,我们有许多丢失的图像,我不得不恢复。该网站是一家医院,我能够恢复所有图像并将所有图像放入临时表中,其中包括受影响的:遭遇,医疗记录编号,文档和副标题。我现在正在尝试创建一个长文本文件,显示每个受影响的遭遇,如果有一个遭遇的多个文档,它将显示该遭遇的所有文档。每个受影响的帐户将以文件中的%% START开头,并以%% END结尾。我想要完成这个,最好做一个游标来对这个表中的遭遇进行排序。那个....光标通过那次遭遇的所有文件来包括遭遇。我的脚本看起来像这样:
declare
@name varchar(40),
@mrn varchar(20),
@encounter varchar(20),
@document varchar(30),
@subtitle varchar(30),
@cmd varchar(255),
@count int
declare find_affected cursor for
select distinct p.name, m.mrn, m.encounter from mck_hpf..missing_documents m
join his..patients p on m.mrn=p.mrn
open find_affected
fetch next from find_affected into @mrn, @name, @encounter while
@@FETCH_STATUS=0
begin
select @mrn = mrn from mck_hpf.dbo.missing_documents where encounter = @encounter
select @name=name from his.dbo.patients where mrn=@mrn
select @encounter = encounter from mck_hpf.dbo.missing_documents where encounter = @encounter
Print '%%START'
Print ''
Print 'Name: ' + @name
Print 'MRN: ' + @mrn
Print 'Encounter: ' + @encounter
Print 'Possible affected documents in this chart:'
declare find_document cursor for
select distinct(document), subtitle from mck_hpf..missing_documents m
join his..encounters e on e.encounter=m.encounter where @mrn=m.mrn and @encounter=m.encounter
open find_document
fetch next from find_document into @document, @subtitle
while @@FETCH_STATUS=0
begin
select @document = document from mck_hpf.dbo.missing_documents where encounter = @encounter
select @subtitle = subtitle from mck_hpf.dbo.missing_documents where encounter = @encounter and document=@document
Print @document + '-' + @subtitle
fetch next from find_document into @document, @subtitle
end
close find_document
deallocate find_document
Print ''
Print ''
Print '%%END'
fetch next from find_affected into @mrn, @name, @encounter
end
close find_affected
deallocate find_affected
go
脚本运行良好并且正确地遍历遇到的问题.....但由于某种原因,它不能处理文档。示例....遇到123在missing_documents表中有2个文件,比如叫做UB04和1500 ....当脚本完成时,而不是说:
Name: Mickey Mouse
MRN: 11111111
Encounter: 123456789
此图表中可能受影响的文档:
UB04
1500
它会说:
Name: Mickey Mouse
MRN: 11111111
Encounter: 123456789
此图表中可能受影响的文档:
UB04
UB04
就像光标知道有2个丢失的文档,但它重复了它一遍又一遍找到的第一个文档。 IT会为所有遭遇做到这一点......如果有6个缺失的文档,它会选择第一个并继续重复。
我花了一些时间来达到这一点,并让它工作,我只是不知道从哪里去,因为我认为我的代码看起来正确。我在这里搜索但没有找到任何可以帮助我的东西。任何帮助,将不胜感激。谢谢你好心的
答案 0 :(得分:1)
您正在重置print语句正上方的@subtitle。 。
select @document = document from mck_hpf.dbo.missing_documents where encounter = @encounter
select @subtitle = subtitle from mck_hpf.dbo.missing_documents where encounter = @encounter and document=@document
Print @document + '-' + @subtitle
如果要查看第二个光标的值,请在选择之前执行print :
Print @document + '-' + @subtitle;
select @document = document from mck_hpf.dbo.missing_documents where encounter = @encounter ;
select @subtitle = subtitle from mck_hpf.dbo.missing_documents where encounter = @encounter and document=@document;