SQL语法问题,结果=0的不要捞

下面这段结果=0的不要捞,该如何修改
declare @RemarkCol as nvarchar(200)
declare @Col as nvarchar(200)
declare @nDecimal float
set @RemarkCol = '0'
declare @Type as nvarchar(200)
declare @TypeCol as nvarchar(200)
select @Col = '' from Corporation where 1=0 set @RemarkCol = '0'
select @nDecimal = CONVERT(decimal(20,10),@RemarkCol)
select @Col = '' from Corporation where 1=0 set @TypeCol = '1'

declare @ItemName as nvarchar(4000)
declare @ItemNameTemp as nvarchar(4000)
declare @ItemName1 as nvarchar(300) --免税加项
declare @SalaryTaxTypeId1 as nvarchar(30) set @SalaryTaxTypeId1 = '0SalaryItemTaxType_003'
declare @ItemName2 as nvarchar(300) --应税加项
declare @SalaryTaxTypeId2 as nvarchar(30) set @SalaryTaxTypeId2 = '0SalaryItemTaxType_001'
declare @ItemName3 as nvarchar(300) --免税扣项
declare @SalaryTaxTypeId3 as nvarchar(30) set @SalaryTaxTypeId3 = '1SalaryItemTaxType_003'
declare @ItemName4 as nvarchar(300) --应税扣项
declare @SalaryTaxTypeId4 as nvarchar(30) set @SalaryTaxTypeId4 = '1SalaryItemTaxType_001'
declare @劳保 as int
declare @健保 as int
declare @补充保费 as int
declare @所得税 as int
declare @自提 as int
declare @年月 as nvarchar(20)

select @年月 = cast(min(SalaryMonth.SalaryYear100+SalaryMonth.Month) as nvarchar(6))+' - '+cast(max(SalaryMonth.SalaryYear100+SalaryMonth.Month) as nvarchar(6)) from SalaryMonth where (0=0)

select top 1 @ItemName =
stuff((
select ','+
case when SalaryItem.SalaryItemTaxTypeId = 'SalaryItemTaxType_002' then '0SalaryItemTaxType_003'+SalaryItem.Name else cast(SalaryItem.ItemCat as nvarchar(1))+SalaryItem.SalaryItemTaxTypeId +SalaryItem.Name end
from SalaryResult
left join SalaryResultDetail on SalaryResult.SalaryResultId = SalaryResultDetail.SalaryResultId
left join SalaryItem on SalaryResultDetail.SalaryItemId = SalaryItem.SalaryItemId
left join SalaryMonth on SalaryResult.SalaryMonthId = SalaryMonth.SalaryMonthId
where SalaryItem.IsSalary =1
and SalaryResultDetail.ItemValue <> 0
--and SalaryItem.SalaryItemTaxTypeId in('SalaryItemTaxType_001','SalaryItemTaxType_003')
and (0=0)
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d'
group by SalaryItem.ItemCat ,SalaryItem.SalaryItemTaxTypeId ,SalaryItem.ItemID,SalaryItem.Name
order by SalaryItem.ItemCat,SalaryItem.SalaryItemTaxTypeId desc,SalaryItem.ItemID
--order by case when SalaryItem.SalaryItemTaxTypeId = 'SalaryItemTaxType_002' then '0' else SalaryItem.ItemCat end,case when SalaryItem.SalaryItemTaxTypeId = 'SalaryItemTaxType_002' then 'SalaryItemTaxType_003' else SalaryItem.SalaryItemTaxTypeId end desc,SalaryItem.ItemID
for XML path('') ),1,1,'')
from Corporation
--where Corporation.CorporationId = 'root'

--免税加项
if(CHARINDEX(@SalaryTaxTypeId1,@ItemName) >0 )
begin
set @ItemNameTemp = replace(substring(@ItemName,CHARINDEX(@SalaryTaxTypeId1,@ItemName)+LEN(@SalaryTaxTypeId1),LEN(@ItemName) - CHARINDEX(@SalaryTaxTypeId1,@ItemName)+LEN(@SalaryTaxTypeId1)),@SalaryTaxTypeId1,'')
set @ItemName1 = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) <=0 then @ItemNameTemp else substring(@ItemNameTemp,1,CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp)-3) end
set @ItemName = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) >0 then REPLACE(@ItemNameTemp,@ItemName1+',','') end
end
else set @ItemName1 = ''

--应税加项
if(CHARINDEX(@SalaryTaxTypeId2,@ItemName) >0 )
begin
set @ItemNameTemp = replace(substring(@ItemName,CHARINDEX(@SalaryTaxTypeId2,@ItemName)+LEN(@SalaryTaxTypeId2),LEN(@ItemName) - CHARINDEX(@SalaryTaxTypeId2,@ItemName)+LEN(@SalaryTaxTypeId2)),@SalaryTaxTypeId2,'')
set @ItemName2 = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) <=0 then @ItemNameTemp else substring(@ItemNameTemp,1,CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp)-3) end
set @ItemName = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) >0 then REPLACE(@ItemNameTemp,@ItemName2+',','') end
end
else set @ItemName2 = ''

--免税扣项
if(CHARINDEX(@SalaryTaxTypeId3,@ItemName) >0 )
begin
set @ItemNameTemp = replace(substring(@ItemName,CHARINDEX(@SalaryTaxTypeId3,@ItemName)+LEN(@SalaryTaxTypeId3),LEN(@ItemName) - CHARINDEX(@SalaryTaxTypeId3,@ItemName)+LEN(@SalaryTaxTypeId3)),@SalaryTaxTypeId3,'')
set @ItemName3 = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) <=0 then @ItemNameTemp else substring(@ItemNameTemp,1,CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp)-3) end
set @ItemName = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) >0 then REPLACE(@ItemNameTemp,@ItemName3+',','') end
end
else set @ItemName3 = ''

--应税扣项
if(CHARINDEX(@SalaryTaxTypeId4,@ItemName) >0 )
begin
set @ItemNameTemp = replace(substring(@ItemName,CHARINDEX(@SalaryTaxTypeId4,@ItemName)+LEN(@SalaryTaxTypeId4),LEN(@ItemName) - CHARINDEX(@SalaryTaxTypeId4,@ItemName)+LEN(@SalaryTaxTypeId4)),@SalaryTaxTypeId4,'')
set @ItemName4 = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) <=0 then @ItemNameTemp else substring(@ItemNameTemp,1,CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp)-3) end
set @ItemName = case when CHARINDEX('SalaryItemTaxType_00',@ItemNameTemp) >0 then REPLACE(@ItemNameTemp,@ItemName4+',','') end
end
else set @ItemName4 = ''

--劳健税自提
select @劳保 = sum(SalaryResult.LabourEmpFee) ,@健保 = SUM(SalaryResult.HealthFee) ,
@所得税 = sum(SalaryResult.EmpTax), @自提 = sum(SalaryResult.PensionEmpFee),
@补充保费 = SUM(SalaryResult.Emp2ndNHI)
from SalaryResult,SalaryMonth
where SalaryResult.SalaryMonthId = SalaryMonth.SalaryMonthId
and (0=0)
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d'

if @劳保 >0 set @ItemName3 = @ItemName3+',劳保费'
if @健保 >0 set @ItemName3 = @ItemName3 +',健保费'
if @补充保费 >0 set @ItemName3 = @ItemName3 +',补充保费'
if @所得税 >0 set @ItemName3 = @ItemName3 +',所得税'
if @自提 >0 set @ItemName4 = @ItemName4 +',自提'
if len(@ItemName3) >0 and LEFT(@ItemName3,1) =',' set @ItemName3 = right(@ItemName3,LEN(@ItemName3)-1)
if len(@ItemName4) >0 and LEFT(@ItemName4,1) =',' set @ItemName4 = right(@ItemName4,LEN(@ItemName4)-1)

select distinct cast(SalaryMonth.SalaryYear*100+SalaryMonth.Month as nvarchar(6)) 薪资月, Employee.Code 工号,Employee.CnName 姓名 ,
--Department.Code 部门代号,Department.Name 部门名称,
部门代号 =case @TypeCol when '1' then Department.Code when '2' then CostCenter.Code when '3' then dept2.Code end,
部门名称 =case @TypeCol when '1' then Department.Name when '2' then CostCenter.Name when '3' then dept2.Code end,
@ItemName2 应税加项,@ItemName1 免税加项,@ItemName4 应税扣项,@ItemName3 免税扣项,
',,,'+stuff((
select ',,,' +item.Name+'计算值'+replace(CONVERT(nvarchar(20),cast(sum(detail.ItemValue) as money),1),'.00','')
from SalaryResult result,SalaryResultDetail detail,SalaryItem item
where result.SalaryResultId = detail.SalaryResultId
and result.SalaryMonthId = SalaryMonth.SalaryMonthId
and result.EmployeeId = Employee.EmployeeId
and detail.SalaryItemId = item.SalaryItemId
and item.IsSalary = 1
and detail.ItemValue <> 0
and detail.ItemValue <999999
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d'
group by item.ItemID,item.Name,result.EmployeeId,result.SalaryMonthId
order by item.ItemID for xml path('')),1,3,'') +
(
select
isnull(case when SUM(result.LabourEmpFee) >0 then ',,,劳保费计算值'+replace(CONVERT(nvarchar(20),cast(sum(result.LabourEmpFee) as money),1),'.00','') end,'') +
isnull(case when SUM(result.HealthFee) >0 then ',,,健保费计算值'+replace(CONVERT(nvarchar(20),cast(sum(result.HealthFee) as money),1),'.00','') end,'') +
isnull(case when SUM(result.Emp2ndNHI) >0 then ',,,补充保费计算值'+replace(CONVERT(nvarchar(20),cast(sum(result.Emp2ndNHI) as money),1),'.00','') end,'') +
isnull(case when SUM(result.EmpTax) >0 then ',,,所得税计算值'+replace(CONVERT(nvarchar(20),cast(sum(result.EmpTax) as money),1),'.00','') end,'') +
isnull(case when SUM(result.PensionEmpFee) >0 then ',,,自提计算值'+replace(CONVERT(nvarchar(20),cast(sum(result.PensionEmpFee) as money),1),'.00','') end,'')
from SalaryResult result
where result.SalaryMonthId = SalaryMonth.SalaryMonthId
and result.EmployeeId = Employee.EmployeeId
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d'

) 个人计算项 ,'应发合计' 应发合计名,'应扣合计' 应扣合计名,'实发' 实发合计名,@年月 年月,Corporation.Name 公司别,@nDecimal 小数位数 from SalaryResult 

left join Employee on SalaryResult.EmployeeId = Employee.EmployeeId
left join Department on Employee.DepartmentId = Department.DepartmentId
left join Corporation on Employee.CorporationId = Corporation.CorporationId
left join SalaryMonth on SalaryResult.SalaryMonthId = SalaryMonth.SalaryMonthId
left join CodeInfo on employee.FactoryId = CodeInfo.CodeInfoId
left join CostCenter on employee.CostCenterId = CostCenter.CostCenterId
left join Department dept2 on Department.DirectDeptId = dept2.DepartmentId where (0=0)
and (0=0)
and (0=0)
and SalaryGroupId = '18ece479-b009-4ae1-b439-43e35665789d' AND (1=1) order by Employee.Code,cast(SalaryMonth.SalaryYear*100+SalaryMonth.Month as nvarchar(6)) ;


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章