FROM子句中的对象“ custflag”和“ custflag”具有相同的公开名称。使用相关名称来区分它们

时间:2019-11-07 20:54:57

标签: c# sql

我有以下查询,出现以下错误。我想知道是因为我需要给custflag加上别名还是应该看看其他东西吗?

            query += Environment.NewLine + " inner join custflag (nolock) on customer.cust_no = custflag.cust_no and custflag.flag in (" + commaSeperatedIncludedFlags + ")";

我的解决方案:

 query += Environment.NewLine + " inner join custflag as cflag (nolock) on customer.cust_no = cflag.cust_no and cflag.flag in (" + commaSeperatedIncludedFlags + ")";

    select distinct convert(bit,0) as IsJournalEntry, doneby.emp_id, employee.name as empname, service.serv_code,   servhist.service_id, servhist.donedate, service.posted, rating, temp, convert(int, isnull(windspeed,0)) as windspeed, winddir, servhist.start, servhist.[end], duration, convert(int,isnull(crewsize,1)) as crewsize, actmanhour, ";
         isnull(service.price,$0)-isnull(service.discamt,$0)-isnull(service.prgdiscamt,$0)-isnull(service.ppayamt,$0) as netsales,  
         service.size, service.price, prgmcd.progspec, cast(isnull(service.invoice_no,0) as int) as invoice_no,   
         doneby.[percent], service.prodvalue,cast(0 as int) as totalgps,cast(0 as int) as missgps,   

        service.service_id as RunningId, servhist.feedback,

        STUFF((SELECT ',  ' + CAST(ltrim(rtrim(isnull((select prodcode from prodcode with (nolock) where prodid = sp1.prodid), ''))) + ' ' + isnull(CAST(sp1.actamount AS VARCHAR(max)), '') as varchar(max))[text()] 
        FROM prodused sp1 with (nolock) where sp1.service_id = Service.service_id order by produsedid desc FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,2,' ') as ProductsUsed, 
        STUFF((SELECT ',  ' + CAST(ltrim(rtrim(isnull(sp1.prodid, ''))) + '|' + isnull(CAST(sp1.actamount AS VARCHAR(max)), '') + '|' + ltrim(rtrim(isnull((select prodcode from prodcode where prodid = sp1.prodid), ''))) as varchar(max))[text()] 
        FROM prodused sp1 with (nolock) where sp1.service_id = Service.service_id order by produsedid desc FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,2,' ') as ProductsUsedId, 
        STUFF((SELECT ',  ' + CAST(ltrim(rtrim(isnull((select prodcode from prodcode with (nolock) where prodid = sp1.prodid), '')))  as varchar(max))[text()] 
        FROM prodused sp1 with (nolock) where sp1.service_id = Service.service_id order by produsedid desc FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,2,' ') as TotalProducts, 
        STUFF((SELECT ',  ' + CAST(c1.condcode as varchar(max))[text()] 
        FROM servcond c1 with (nolock) where c1.service_id = Service.service_id order by condcode asc FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,2,' ') as Conditions"

        STUFF((SELECT ',  ' + CAST(ltrim(rtrim(isnull((select prodcode from prodcode with (nolock) where prodid = sp1.prodid), ''))) + ' ' + isnull(CAST(sp1.actamount AS VARCHAR(max)), '') as varchar(max))[text()] 
        FROM produsen sp1 with (nolock) inner join prodentr pe1 on sp1.prodentrid = pe1.prodentrid where pe1.service_id = Service.service_id order by produsedid desc FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,2,' ') as ProductsUsed, 
        STUFF((SELECT ',  ' + CAST(ltrim(rtrim(isnull(sp1.prodid, ''))) + '|' + isnull(CAST(sp1.actamount AS VARCHAR(max)), '') + '|' + ltrim(rtrim(isnull((select prodcode from prodcode where prodid = sp1.prodid), ''))) as varchar(max))[text()] 
        FROM produsen sp1 with (nolock) inner join prodentr pe1 on sp1.prodentrid = pe1.prodentrid where pe1.service_id = Service.service_id order by produsedid desc FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,2,' ') as ProductsUsedId, 
        STUFF((SELECT ',  ' + CAST(ltrim(rtrim(isnull((select prodcode from prodcode with (nolock) where prodid = sp1.prodid), '')))  as varchar(max))[text()] 
        FROM produsen sp1 with (nolock) inner join prodentr pe1 on sp1.prodentrid = pe1.prodentrid where pe1.service_id = Service.service_id order by produsedid desc FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,2,' ') as TotalProducts, 
        STUFF((SELECT ',  ' + CAST(c1.condcode as varchar(max))[text()] 
        FROM prodcond c1 with (nolock) inner join prodentr pe1 on c1.prodentrid = pe1.prodentrid where pe1.service_id = Service.service_id order by condcode asc FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,2,' ') as Conditions"

        FROM dbo.customer with (nolock)  
        INNER join dbo.program with (nolock) on customer.cust_no = program.cust_no  
        INNER join dbo.prgmcd with (nolock) on prgmcd.progdefid = program.progdefid  
        INNER join dbo.service with (nolock) on program.prog_id = service.prog_id  
        INNER join dbo.servhist with (nolock) on service.service_id = servhist.service_id  
        INNER join dbo.doneby with (nolock) on service.service_id = doneby.service_id   

        INNER join dbo.employee with (nolock) on doneby.emp_id = employee.emp_id   
        inner join custflag (nolock) on customer.cust_no = custflag.cust_no and custflag.flag 

0 个答案:

没有答案