我一直在向通过SSIS从SQL查询生成的供应商发送平面文件。供应商声明何时将文件导入其主机NULL或低值导致问题看到无效字符。有没有人遇到过这个?我的查询如下。我是否需要将“空间”替换为不同的非填充字段?
--CREATE HEADER RECORD TABLE UH01
declare @uh01 table
( UH01 char(10), --required field
custcode char(9), --required field
processdatetime char(14), --required field
version char(4), --required field
filler char(475)) --required field
--Create table to store UA01 data
declare @ua01 table (
recid int IDENTITY(1,1),
UA01 char(10), --required field
reporttype char(1), --required field
inscompanycode char(9), --required field
policynum char(30), --required field
policytype char(4), --required field
policyinceptiondate char(8),
policyexpate char(8),
policyrenewalind char(1),
riskpolicyind char(1),
claimno char(30), --required field
dateofloss char(8), --required field
timeofloss char(4),
CatIndicator char(1),
cat char(3),
companyrecdate char(8),
lossdesc char(50),
loclossaddr1 char(50),
loclossaddr2 char(50),
loclosscity char(25),
loclossst char(2), --required field
loclosspostal char(9),
loclosscountry char(3), --required field
filler char(4),
agencynotifiedofloss char(35),
policefirereportnum char(15),
routingmisinfo char(20),
nosearchind char(1),
datefirstclaimpayment char(8),
fundclaim char(1),
vesselcallnum char(50),
apdfastcheckindicator char(1),
legacyclaimconversionind char(1),
requestclaimconversionind char(1),
policereportinoccurenceind char(1),
singlevehicleaccidentindicator char(1),
phantomvehaccind char(1),
wasaccwitnessed char(1),
molddamage char(1),
filler2 char(1),
filler3 char(2),
hitandrun char(1),
reqrecallind char(1),
masstortind char(1), --required field
selfinsuredind char(1),
cobcassignedsectionID char(9),
tin char(9),
siteid char(9),
nmvtisoprepid char(7),
filler4 char(9), --required field
filler5 char(5)) --required field
--CREATE UO01 TABLE PARTIES TO THE LOSS
declare @uo01 table (
recid int IDENTITY(1,1),
UO01 char(10), --required field
roleinclaim char(2), --required field
individualbusinessind char(1), --required field
businessname char(70), --required field
lastname char(30), --required field
firstname char(20), --required field
middlename char(20),
dob char(8),
gender char(1),
ssn char(9),
tin char(9),
dln char(20),
dlstate char(2),
occupationrating char(50),
medproflicense char(15),
addr1 char(50), --required field
addr2 char(50),
city char(25), --required field
statecode char(2), --required field
postalcode char(9),
countrycode char(3),
homephone char(10),
busphone char(10),
cellphone char(10),
pagernum char(10),
pagerpin char(7),
vehicreltoown char(2),
filler char(2), --required field
nosearchind char(1),
passportnum char(9),
routinginfo char(5),
vinofoccupant char(20),
datereportedloss char(8),
faxnum char(10),
searchchildsupportind char(1),
filler1 char(1), --required field
filler2 char(70)) --required field
declare @uc01 table
(
recid int IDENTITY(1,1),
UC01 char(10), --required field
filler char(9),
adjnamelast char(30),
adjnamefirst char(20),
adjnamemid char(20),
adjphone char(10),
losstype char(4), --required field
coveragetype char(4), --required field
injurysdamage char(50), --required field
bodypart char(2),
filler1 char(3), --required field
claimstatus char(3),
tortthresholdtype char(2),
tortthresholdstate char(2),
suitindicator char(1),
estlossamt char(11),
reserveamt char(11),
settlementamt char(11),
dateclaimclosed char(8),
routingmiscinfo char(20),
losstimestartdate char(8),
losstimeenddate char(8),
totallostdays char(5),
courtfiledtype char(10),
courtfileddate char(8),
physicaldamage char(1),
icd9code char(6),
icd9code1 char(6),
icd9code2 char(6),
icd9code3 char(6),
icd9code4 char(6),
deathresultofinjury char(1),
dateattorneyinvolv char(8),
noticedateemployer char(8),
cptcode char(5),
benefittypecode char(3),
employmentstatcode char(2),
dateofretirement char(8),
dateofstrike char(8),
preexistingdiscode char(2),
personinsattimeofloss char(1),
empdateofhire char(8),
datetermlaid char(8),
ambulance char(1),
disabled char(1),
pdmedbills char(11),
pdindemnity char(11),
totalpaid char(11),
courtcounty char(25),
courtstate char(2),
docketnum char(25),
adjustemailadd char(50),
erisaclaim char(1),
filler2 char(15)) --required field
--Populate UH01
--POPULATE THE HEADER RECORD TABLE UH01
insert into @uh01
select '000000UH01', + --filetype
'S73200001', + --custcode
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19), CONVERT(DATETIME, getdate(), 112), 126), '-', ''), 'T', ''), ':', ''), --processdatetime
'US01', --version
space(475) --filler
--POPULATE THE UA01 table
insert into @ua01
select
--ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
'000000UA01', --UA01
'I', --reporttype
'S73200001', --inscompanycode
isnull(rtrim(convert(char(30), po.policyid)), space(30)), --policynum
'PAPP', --policytype
isnull(left(convert(char(8), po.inceptdt, 112), 8), space(8)), --policyinceptiondate
isnull(left(convert(char(8), po.expiratdt, 112), 8), space(8)), --policyexpate
space(1), --policyrenewalind
space(1), --riskpolicyind
isnull(convert(char(30), c1.agencynumb), space(30)), --Claim Number
isnull(left(convert(char(8), c.doi, 112), 8), space(8)), --dateofloss
isnull(left(convert(char(4), c.accdtime, 114), 4), space(4)), --timeofloss
space(1), --CatIndicator
space(3), --cat@
isnull(left(convert(char(8), c.lworkdate, 112), 8), space(8)), --companyrecdate
space(50), --lossdesc
space(50), --loclossaddr1
space(50), --loclossaddr2
space(25), --loclosscity
convert(char(2),c1.jurst), --loclossst
space(9), --loclosspostal
'USA', --loclosscountry
space(4), --filler
space(35), --agencynotifiedofloss
space(15), --policefirereportnum
space(20), --routingmisinfo
space(1), --nosearchind
space(8), --datefirstclaimpayment
space(1), --fundclaim
space(50), --vesselcallnum
space(1), --apdfastcheckindicator
space(1), --legacyclaimconversionind
space(1), --requestclaimconversionind
space(1), --policereportinoccurenceind
space(1), --singlevehicleaccidentindicator
space(1), --phantomvehaccind
space(1), --wasaccwitnessed
space(1), --molddamage
space(1), --filler2
space(2), --filler3
space(1), --hitandrun
space(1), --reqrecallind
space(1), --masstortind
case
when ca.invmsg like 'SELF%' then 'Y'
else 'N'
end, --selfinsuredind
space(9), --cobcassignedsectionID
space(9), --tin
space(9), --siteid
space(7), --nmvtisoprepid
space(9), --filler4
space(5) --filler5
from claim c
join v_initial_bi v on c.claimno = v.claimno
join claim1 c1 on c.claimno = c1.lnkclaimno
join claim2 c2 on c.claimno = c2.link2claim
join person p on c.patient = p.personid
join cmpolicy cm on c.claimno = cm.claimno
join policy po on cm.policyid = po.policyid
join carrier ca on c.carrier = ca.code
where cast (v.INITBI as date) = cast (getdate()-1 as date)
--POPULATE THE WC CLAIM DETAIL TABLE UO01
insert into @uo01
select
--ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
'000000UO01', --UO01
'CL', --roleinclaim
'B', --individualbusinessind
isnull(rtrim(convert(char(70), ph.name)), space(70)), --businessname
isnull(rtrim(convert(char(30), p.lname)), space(30)), --lastname
isnull(rtrim(convert(char(20), p.fname)), space(20)), --firstname
isnull(rtrim(convert(char(20), p.mname)), space(20)), --middlename
space(8), --dob
case
when p.sex = 1 then 'M'
when p.sex = 2 then 'F'
else ' '
end,
space(9), --SSN
space(9), --tin
space(20), --dln
space(2), --dlstate
space(50), --occupationrating
space(15), --medproflicense
isnull(rtrim(convert(char(50), p.addr1)), space(50)), --Addr1
isnull(rtrim(convert(char(50), p.addr2)), space(50)), --Addr2
isnull(rtrim(convert(char(25), p.city)), space(25)), --Employee City
isnull(rtrim(convert(char(2), p.state)), space(2)), --Employee State
isnull(rtrim(convert(char(9), p.zip)), space(9)), --Employee Zip
space(3), --countrycode
isnull(rtrim(convert(char(10), p.phone)), space(10)), --Employee phone
space(10), --busphone
space(10), --cellphone
space(10), --pagernum
space(7), --pagerpin
space(2), --vehicreltoown
space(2), --filler
space(1), --nosearchind
space(9), --passportnum
space(5), --routinginfo
space(20), --vinofoccupant
space(8), --datereportedloss
space(10), --faxnum
space(1), --searchchildsupportind
space(1), --filler1
space(70) --filer2
from claim c
join v_initial_bi v on c.claimno = v.claimno
join claim1 c1 on c.claimno = c1.lnkclaimno
join claim2 c2 on c.claimno = c2.link2claim
join person p on c.patient = p.personid
join cmpolicy cm on c.claimno = cm.claimno
join policy po on cm.policyid = po.policyid
join carrier ca on c.carrier = ca.code
join masttype m on c.casetype = m.mastid
left join employer e on c.empcode = e.code
left join employer ph on e.polholder = ph.code
left join dictdata d7 on c2.losstype = d7.datumid --Type Of Loss Code
where cast (v.INITBI as date) = cast (getdate()-1 as date)
--POPULATE THE UC01 TABLE PARTIES TO THE LOSS
insert into @uc01
select
--ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
'000000UC01', --UC01
space(9), --filler
space(30), --adjnamelast
space(20), --adjnamefirst
space(20), --adjnamemid
space(10), --adjphone
isnull(convert(char(4), d7.code), '0001'), --losstype
left(m.mastdesc, 4),--coveragetype
isnull(rtrim(convert(char(50), c.accdesc)), space(50)), --injurysdamage
space(2), --bodypart
space(3), --filler
convert(char(3), c.status), --Claim Status
space(2), --tortthresholdtype
space(2),--tortthresholdstate
space(1), --suitindicator
space(11), --estlossamt
space(11), --reserveamt
space(11), --settlementamt
space(8), --dateclaimclosed
space(20), --routingmiscinfo
space(8), --losstimestartdate
space(8), --losstimeenddate
space(5), --totallostdays
space(10), --courtfiledtype
space(8), --courtfileddate
space(1), --physicaldamage
space(6), --icd9code
space(6), --icd9code1
space(6), --icd9code2
space(6), --icd9code3
space(6), --icd9code4
space(1), --deathresultofinjury
space(8), --dateattorneyinvolv
space(8), --noticedateemployer
space(5), --cptcode
space(3), --benefittypecode
space(2), --employmentstatcode
space(8), --dateofretirement
space(8), --dateofstrike
space(2), --preexistingdiscode
space(1), --personinsattimeofloss
space(8), --empdateofhire
space(8), --datetermlaid
space(1), --ambulance
space(1), --disabled
space(11), --pdmedbills
space(11), --pdindemnity
space(11), --totalpaid
space(25), --courtcounty
space(2), --courtstate
space(25), --docketnum
space(50), --adjustemailadd
space(1), --erisaclaim
space(15) --filler2
from claim c
join v_initial_bi v on c.claimno = v.claimno
join claim1 c1 on c.claimno = c1.lnkclaimno
join claim2 c2 on c.claimno = c2.link2claim
join person p on c.patient = p.personid
join cmpolicy cm on c.claimno = cm.claimno
join policy po on cm.policyid = po.policyid
join carrier ca on c.carrier = ca.code
join masttype m on c.casetype = m.mastid
left join employer e on c.empcode = e.code
left join employer ph on e.polholder = ph.code
left join dictdata d7 on c2.losstype = d7.datumid --Type Of Loss Code
---where INITBI = GETDATE() //TO UNCOMMENT, COMMENTED FOR TESTING ONLY
where cast (v.INITBI as date) = cast (getdate()-1 as date)
--------------------------------------------------------------------------------------------------
declare @outputtable table
(
id int identity(1,1),
recid int,
rectype varchar(10),
result varchar(max)
)
declare @TotalCount int
declare @ProcessedCount int = 2
declare @LastProcessedType varchar(10) = 'X'
declare @uocount int = 0;
declare @seqcode varchar(10)
set @TotalCount = ( (select count(1) from @uc01) + (select count(1) from @ua01) + (select count(1) from @uo01))
print @TotalCount
--Declare and populate UZ01 segment.
declare @uz01 table
(UZ01 char(10), --required field
totalrectrans char(6), --required field
filler char(480)) --required field
--INSERT HEADER RECORD
INSERT INTO @outputtable
SELECT
0,
'HEADER',
'000001UH01' + --filetype
'S73200001' + --custcode
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19), CONVERT(DATETIME, getdate(), 112), 126), '-', ''), 'T', ''), ':', '')+ --processdatetime
'US01' + --version
space(475) --filler
--UA, UO, UO, UC
WHILE( (SELECT COUNT(1) FROM @outputtable) < @TotalCount)
BEGIN
--INITIAL ATTEMPT AND UA
IF (@LastProcessedType = 'X' OR @LastProcessedType = 'UA')
BEGIN
print 'UA'
set @seqcode = (concat ( REPLICATE('0', 10-LEN( concat(@ProcessedCount, 'UA01'))), concat(@ProcessedCount, 'UA01')))
INSERT INTO @outputtable
SELECT
top 1
ua.recid, 'UA',
----
@seqcode+
rtrim(reporttype) + space(1 - len(reporttype)) + --reporttype
rtrim(inscompanycode) + space(9 - len(inscompanycode)) + --inscompanycode
rtrim(policynum) + space(30 -len(policynum)) + --Claimant number
rtrim(policytype) + space(4 -len(policytype)) + --policytype --required field
space(8) + --policyinceptiondate
space(8) + --policyexpate
space(1) + --policyrenewalind
space(1) + --riskpolicyind
rtrim(claimno) + space(30 - len(claimno)) + --required field
rtrim(dateofloss) + space(8 - len(dateofloss)) + --required field
space(4) + --timeofloss
space(1) + --CatIndicator
space(3) + --cat@
space(8) + --companyrecdate
space(50) + --lossdesc
space(50) + --loclossaddr1
space(50) + --loclossaddr2
space(25) + --loclosscity
rtrim(loclossst + space(2 -len(loclossst)) + --required field
space(9) + --loclosspostal
'USA' + --required field
space(4) + --Filler
space(35) + --agencynotifiedofloss
space(15) + --policefirereportnum
space(20) + --routingmisinfo
space(1) + --nosearchind
space(8) + --datefirstclaimpayment
space(1) + --fundclaim
space(50) + --vesselcallnum
space(1) + --apdfastcheckindicator
space(1) + --legacyclaimconversionind
space(1) + --requestclaimconversionind
space(1) + --policereportinoccurenceind
space(1) + --singlevehicleaccidentindicator
space(1) + --phantomvehaccind
space(1) + --wasaccwitnessed
space(1) + --molddamage
space(1) + --filler2
space(2) + --filler3
space(1) + --hitandrun
space(1) + --reqrecallind
space(1) + --masstortind
selfinsuredind) + space(1 -len(selfinsuredind)) + --selfinsuredind
space(9) + --cobcassignedsectionID
space(9) + --tin
space(9) + --siteid
space(7) + --nmvtisoprepid
space(9) + --filler4 --required field
space(5) --filler5 --required field
--
FROM @ua01 ua
WHERE ua.recid NOT IN
(
select recid from @outputtable where rectype = 'UA'
)
SET @ProcessedCount = @ProcessedCount+1
SET @LastProcessedType = 'UO'
END
IF (@LastProcessedType = 'UO')
BEGIN
print 'UO'
set @seqcode = (concat ( REPLICATE('0', 10-LEN( concat(@ProcessedCount, 'UO01'))), concat(@ProcessedCount, 'UO01')))
INSERT INTO @outputtable
SELECT
top 1
uo.recid, 'UO',
----------------------
@seqcode + --Record indicator
'CL' + --roleinclaim
'B' + --individualbusinessind
rtrim(businessname) + space(70 - len(businessname)) + -- business name --required field
rtrim(lastname) + space(30 - len(lastname)) + --Last name --required field
rtrim(firstname) + space (20 - len(firstname)) + --Firstname --required field
rtrim(middlename) + space (20 - len(middlename)) + --middle name --required field
rtrim(dob) + space (8 - len(dob)) + --required field
rtrim(gender) + space (1 - len(gender)) + --required field
rtrim(ssn) + space (9 - len(ssn)) + --gender
space(9) + --tin tax id num
space(20) + --drivers license number
space(2) + --driver license state
space(50) + --occupationrating
space(15) + --medproflicense
rtrim(addr1) + space (50 - len(addr1)) + --required field
rtrim(addr2) + space (50 - len(addr2)) +
rtrim(city) + space (25 - len(city)) + --required field
rtrim(statecode) + space (2 - len(statecode)) + --required field
rtrim(postalcode) + space (9 - len(postalcode)) + --postalcode
space(3) + --spacecountrycode
space(10) + --homephone
space(10) + --busphone
space(10) + --cellphone
space(10) + --pagernum
space(7) + --pagerpin
space(2) + --vehicreltoown
space(2) + --filler
space(1) + --nosearchind
space(9) + --passportnum
space(5) + --routinginfo
space(20) + --vinofoccupant
space(8) + --datereportedloss
space(10) + --faxnum
space(1) + --searchchildsupportind
space(1) + --filler1
space(70) --filler2
----------------------
FROM @uo01 uo
WHERE uo.recid NOT IN
(
select recid from @outputtable where rectype = 'UO'
)
SET @ProcessedCount = @ProcessedCount+1
SET @LastProcessedType = 'UC'
IF (@LastProcessedType = 'UC')
BEGIN
print 'UC'
set @seqcode = (concat ( REPLICATE('0', 10-LEN( concat(@ProcessedCount, 'UC01'))), concat(@ProcessedCount, 'UC01')))
INSERT INTO @outputtable
SELECT top 1
uc.recid, 'UC',
------------------------
@seqcode + --Record indicator
space(9) + --filler --required field
space(30) + --adjnamelast
space(20) + --adjnamefirst
space(20) + --adjnamemid
space(10) + --adjphone
rtrim(losstype) + space(4 - len(losstype)) + --losstype --required field
rtrim(coveragetype) + space(4 -len(coveragetype)) + --coveragetype
rtrim(injurysdamage) + space(50 - len(injurysdamage)) + --injurysdamage --required field
space(2) + --bodypart
space(3) + --filler1 --required field
space(3) + --claimstatus
space(2) + --tortthresholdtype
space(2) + --tortthresholdstate
space(1) + --suitindicator
space(11) + --estlossamt
space(11) + --reserveamt
space(11) + --settlementamt
space(8) + --dateclaimclosed
space(20) + --routingmiscinfo
space(8) + --losstimestartdate
space(8) + --losstimeenddate
space(5) + --totallostdays
space(10) + --courtfiledtype
space(8) + --courtfileddate
space(1) + --physicaldamage
space(6) + --icd9code
space(6) + --icd9code1
space(6) + --icd9code2
space(6) + --icd9code3
space(6) + --icd9code4
space(1) + --deathresultofinjury
space(8) + --dateattorneyinvolv
space(8) + --noticedateemployer
space(5) + --cptcode char
space(3) + --benefittypecode
space(2) + --employmentstatcode
space(8) + --dateofretirement
space(8) + --dateofstrike
space(2) + --preexistingdiscode
space(1) + --personinsattimeofloss
space(8) + --empdateofhire
space(8) + --datetermlaid
space(1) + --ambulance
space(1) + --disabled
space(11) + --pdmedbills
space(11) + --pdindemnity
space(11) + --totalpaid
space(25) + --courtcounty
space(2) + --courtstate
space(25) + --docketnum
space(50) + --adjustemailadd
space(1) + --erisaclaim
space(15) --filler2
------------------------------
FROM @uc01 uc
WHERE uc.recid NOT IN
(
select recid from @outputtable where rectype = 'UC'
)
SET @LastProcessedType = 'UA'
SET @ProcessedCount = @ProcessedCount+1
END
END
END
INSERT INTO @outputtable
SELECT
0,
'TRAILER',
concat('0000', @ProcessedCount , 'UZ01' , '0000', @ProcessedCount)
--left(space(6 - len(convert(varchar, count(*)))) + convert(varchar, count(*)), 6) from @outputtable
--select '0000'+ @ProcessedCount + 'UZ01' + '10000'+ @ProcessedCount
--000042UZ01000042
select result from @outputtable order by id