在我的Sql server查询中添加SPACE会更改结果集

时间:2014-05-06 13:55:32

标签: sql sql-server-2008

我有一个SQL服务器查询..当我在查询中的任何地方添加一个空格时,它将结果集更改为410行到38行。

怎么可能这样呢?

先谢谢..

声明@ReportDate日期,@ Land varchar(50),@ Missing bit = 1

--declare @ReportDate datetime, @Range varchar(50) = '', @Missing bit = 0

--declare @ReportDate date, @Range varchar(50)
declare @RangeId int

set @ReportDate = '30 Apr 2014'
set @Range = ''

select @RangeId = ID from fs.FundGroup where Name = @Range

declare @FundProperty table (FundId int, FundCode varchar(25), FundName varchar(255), FundRange varchar(50), ParentFundCode varchar(25), ParentFundId int, AllocationType varchar(25), SourceName varchar(255), UpdatedSourceName varchar(255), AllocationTypeId int)

if @Range = ''
    insert into @FundProperty(FundId, FundCode, FundName, FundRange, AllocationType, SourceName)
    select F.FundId, F.FundCode, F.FundName, FG.Name, PT.Name, S.Name 
    from fs.FundDataSource FDS, fs.Fund F, fs.PropertyType PT, fs.Source S, fs.FundGroup FG
    where FDS.FundId = F.FundId
    and FDS.PropertyTypeId = PT.Id
    and FDS.SourceId = S.Id
    and F.FundGroupId = FG.Id
    and S.Name like '%PPMA%'
    and PT.Name not like '%top10%'
else
    insert into @FundProperty(FundId, FundCode, FundName, FundRange, AllocationType, SourceName)
    select F.FundId, F.FundCode, F.FundName, FG.Name, PT.Name, S.Name 
    from fs.FundDataSource FDS, fs.Fund F, fs.PropertyType PT, fs.Source S, fs.FundGroup FG
    where FDS.FundId = F.FundId
    and FDS.PropertyTypeId = PT.Id
    and FDS.SourceId = S.Id
    and F.FundGroupId = FG.Id
    and S.Name like '%PPMA%'
    and PT.Name not like '%top10%'
    and F.FundGroupId = @RangeId

update @FundProperty set UpdatedSourceName = SourceName

update @FundProperty set ParentFundId = F1.FundId, ParentFundCode = F1.FundCode
from fs.Fund F1, fs.FundIdentifier FI, @FundProperty FP, fs.Fund F2
where FI.FundId = FP.FundId
and F1.FundCode = FI.[Parent Fund Code]
and F2.FundId = FI.FundId
and F1.FundGroupId = F2.FundGroupId

update @FundProperty set AllocationTypeId = Id from fs.AllocationType AT, @FundProperty FP
where AT.Name = case when FP.AllocationType = 'Rating' then 'Credit Rating' else FP.AllocationType end

--select * from @FundProperty

create table #Allocations(FundCode varchar(25), FundName varchar(255), FundRange varchar(50), AllocationType varchar(50), UpdatedSourceName varchar(255), PositionsName varchar(255), ISIN varchar(50), Percentage float, PPMAName varchar(255), AssetName varchar(255), Maturity varchar(50), SnPRating varchar(25), MoodyRating varchar(25))

insert into #Allocations(FundCode,FundName,FundRange,AllocationType,UpdatedSourceName,PositionsName,ISIN,Percentage)
select distinct FP.ParentFundCode, FP.FundName, FP.FundRange, FP.AllocationType, FP.UpdatedSourceName, ALS.Name, ALS.ISIN, ALC.Percentage 
from fs.Allocation ALC, fs.AllocationSecurity ALS, @FundProperty FP
where ALC.AllocationTypeId = ALS.AllocationTypeId
and ALC.SecurityId = ALS.Id
and ALC.FundId = FP.ParentFundId
and ALC.AllocationTypeId = FP.AllocationTypeId
and ALC.ReportDate = @ReportDate

insert into #Allocations(FundCode,FundName,FundRange,AllocationType,UpdatedSourceName,PositionsName,ISIN,Percentage)
select distinct FP.ParentFundCode, FP.FundName, FP.FundRange, FP.AllocationType, FP.UpdatedSourceName, ALS.Name, ALS.ISIN, ALC.Percentage 
from fs.Allocation ALC, fs.AllocationSecurity ALS, @FundProperty FP
where ALC.SecurityId = ALS.Id
and ALC.FundId = FP.ParentFundId
and ALC.AllocationTypeId = FP.AllocationTypeId
and FP.FundRange = 'PRULINK PHILIPPINES'
and ALC.ReportDate = @ReportDate

update #Allocations set PPMAName = PPMA.Merrill_Industry___Level_2
from fs.PPMADataExtract PPMA, #Allocations T 
where T.AllocationType = 'Sector'
and T.ISIN = PPMA.ISIN
and PPMA.ReportDate = @ReportDate
/*and case when right(PPMA.as_of_date,3) like '/%' --
            then convert(datetime,REPLACE(PPMA.As_Of_Date,right(PPMA.As_Of_Date,3),'/20' + right(PPMA.As_Of_Date,2) ),103)
            else convert(datetime,PPMA.As_Of_Date,103) end = @ReportDate --*/

update #Allocations set 
SnPRating = PPMA.S_P_Rating,
MoodyRating = PPMA.Moody_Rating
from fs.PPMADataExtract PPMA, #Allocations T 
where T.AllocationType = 'Rating'
and T.ISIN = PPMA.ISIN
and PPMA.ReportDate = @ReportDate
/*and case when right(PPMA.as_of_date,3) like '/%' --
            then convert(datetime,REPLACE(PPMA.As_Of_Date,right(PPMA.As_Of_Date,3),'/20' + right(PPMA.As_Of_Date,2) ),103)
            else convert(datetime,PPMA.As_Of_Date,103) end = @ReportDate --*/

update #Allocations set 
    PPMAName = CONVERT(date,PPMAName,103),
    Maturity = 
case when convert(float,datediff(day,@ReportDate,convert(date,PPMAName,103)))/365 <= 1 then '0-1 Year'
    when convert(float,datediff(day,@ReportDate,convert(date,PPMAName,103)))/365 between 1 and 5 then '1-5 Years'
    when convert(float,datediff(day,@ReportDate,convert(date,PPMAName,103)))/365 between 5 and 10 then '5-10 Year'
    when convert(float,datediff(day,@ReportDate,convert(date,PPMAName,103)))/365 > 10 then '> 10 Years'
    else 'NA'
    end
where AllocationType = 'Maturity'

update #Allocations set AssetName = ALS.Name from fs.AllocationSecurity ALS, #Allocations T where T.ISIN = ALS.ISIN
and ALS.AllocationTypeId = 9 and T.FundRange in ('IOF','IOF HK')

--select * from #Allocations

update #Allocations set FundRange = 'IOF SG' where FundRange = 'IOF'

if @Missing = 0
    select convert(varchar,@ReportDate,103) [ReportDate], * from #Allocations where 
    PPMAName is not null 
    and ISIN <> 'NA'
    order by FundName, AllocationType
else
begin
    delete from #Allocations where FundRange = 'IOF HK'

    select 
        convert(varchar,@ReportDate,103) [ReportDate], 
        FundCode,
        FundName,
        AllocationType,
        ISIN,
        PositionsName,
        Percentage,
        PPMAName,
        UpdatedSourceName,
        AssetName
    from #Allocations 
    where PPMAName is null and SnPRating is null and MoodyRating is null  
    and ISIN <> 'NA'
    order by FundName, AllocationType
end

drop table #Allocations

0 个答案:

没有答案