select cast ltrim replace XRecStat as varchar 20 cast ltrim replace NZ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
select '="'+cast(ltrim(replace(d.XRecStat, '"', '""'))as varchar(20))+'"','="'+cast(ltrim(replace(d.NZ, '"', '""'))as varchar(20))+'"','="'+cast(ltrim(replace(a1.NameAn,'"','""'))as varchar(36))+'"','="'+cast(ltrim(replace(d.NumDoc, '"', '""'))as varchar(10))+'"',d.DatOper,'="'+cast(ltrim(replace(a2.NameAn,'"','""'))as varchar(49))+'"',d.Summa,'','','','','','','','','','','','','','','','','','','','','','','',''
from MBDoc d, [KDV].MBDocV dv, MBAnalitSpr a1, MBAnalitSpr a2
where d.XRecID=dv.XRecID and a1.Analit=*d.VidLKart and a2.Analit=*d.MOLMat2
and not exists (select t.NZDoc
from MBDocValR t
where d.NZ=t.NZDoc)
union all
select '="'+cast(ltrim(replace(d.XRecStat, '"', '""'))as varchar(20))+'"','="'+cast(ltrim(replace(d.NZ, '"', '""'))as varchar(20))+'"','="'+cast(ltrim(replace(a1.NameAn,'"','""'))as varchar(36))+'"','="'+cast(ltrim(replace(d.NumDoc, '"', '""'))as varchar(10))+'"',d.DatOper,'="'+cast(ltrim(replace(a2.NameAn,'"','""'))as varchar(49))+'"',d.Summa,'="'+cast(ltrim(replace(s1.Kod,'"','""'))as varchar(10))+'"','="'+cast(ltrim(replace(s2.Kod,'"','""'))as varchar(10))+'"','="'+cast(ltrim(replace(t.VagonT, '"', '""'))as varchar(10))+'"',t.DataT,'="'+cast(ltrim(replace(a3.NameAn,'"','""'))as varchar(18))+'"',t.KolvoVT,t.Kolichestvo1T,t.Kolichestvo2T,'="'+cast(ltrim(replace(a4.NameAn,'"','""'))as varchar(49))+'"','="'+cast(ltrim(replace(a5.NameAn,'"','""'))as varchar(14))+'"','="'+cast(ltrim(replace(a6.Kod,'"','""'))as varchar(10))+'"', case when t.PriznSpisT='С' then 'Списание' when t.PriznSpisT='П' then 'Перемещение и списание' when t.PriznSpisT='з' then 'Перемещение в забаланс' when t.PriznSpisT='т' then 'Перемещение в забаланс транзитом' when t.PriznSpisT='В' then 'Возврат' when t.PriznSpisT='е' then 'Перемещение' when t.PriznSpisT='л' then 'Поступление' when t.PriznSpisT='н' then 'Внутреннее перемещение' when t.PriznSpisT='Р' then 'Расход на сторону' end,'="'+cast(ltrim(replace(t.RekvT, '"', '""'))as varchar(10))+'"','="'+cast(ltrim(replace(a7.NameAn,'"','""'))as varchar(46))+'"','="'+cast(ltrim(replace(t.SoderT, '"', '""'))as varchar(255))+'"','="'+cast(ltrim(replace(t.Stroka2T, '"', '""'))as varchar(40))+'"','="'+cast(ltrim(replace(t.Stroka3T, '"', '""'))as varchar(80))+'"','="'+cast(ltrim(replace(t.Stroka6T, '"', '""'))as varchar(80))+'"','="'+cast(ltrim(replace(t.StrokaT, '"', '""'))as varchar(255))+'"',t.SummaT,'="'+cast(ltrim(replace(a8.NameAn,'"','""'))as varchar(18))+'"',t.CenaT,'="'+cast(ltrim(replace(a9.Kod,'"','""'))as varchar(10))+'"','="'+cast(ltrim(replace(a10.NameAn,'"','""'))as varchar(34))+'"'
from MBDoc d, [KDV].MBDocV dv, MBAnalitSpr a1, MBAnalitSpr a2, MBPlanAcc s1, MBPlanAcc s2, MBAnalitSpr a3, MBAnalitSpr a4, MBAnalitSpr a5, MBAnalitSpr a6, MBAnalitSpr a7, MBAnalitSpr a8, MBAnalitSpr a9, MBAnalitSpr a10, MBDocValR t
where d.XRecID=dv.XRecID and a1.Analit=*d.VidLKart and a2.Analit=*d.MOLMat2 and s1.Acc=*t.BuhAccDT and s2.Acc=*t.BuhAccKT and a3.Analit=*t.EdIzmPT and a4.Analit=*t.MOLMatT and a5.Analit=*t.NaznMCT and a6.Analit=*t.NaimMCT and a7.Analit=*t.SkladMHT and a8.Analit=*t.CenaAT and a9.Analit=*t.SHPZT and a10.Analit=*t.EkonomT and d.NZ=t.NZDoc
order by '="'+cast(ltrim(replace(d.NZ, '"', '""'))as varchar(20))+'"' DESC