QUOTENAME导致SQL中的转换错误?

时间:2014-08-18 13:06:27

标签: sql sql-server tsql

编辑:

好吧,这真的让我失望了,我让查询工作了两次然后无缘无故地再次停止工作并且每次都给我不同的错误。现在我收到了这个错误:

Msg 8115, Level 16, State 8, Procedure gsp_rpt_Custom_Extract_Haven_test_2, Line 12
Arithmetic overflow error converting varchar to data type numeric.

它是如此不一致,我可以取出某些连接并且查询运行但是这些连接与错误完全无关,并且独立地工作绝对精细。对这一个感到非常困惑。

结束编辑

我有这个查询,它是一个相当冗长的查询。我正在使用查询BCP命令,这就是为什么它看起来很奇怪。我试图通过使用quotename()添加文本限定符(因为BCP不能这样做),查询运行正常,没有引号名称由于某些原因我一直收到此错误:

Msg 245, Level 16, State 1, Procedure gsp_rpt_Custom_Extract_test, Line 12
Conversion failed when converting the varchar value '05-01-2015 12:49:05' to data type smallint.

正如您在查询中看到的那样,我已经将我尚未作出的列作为诊断的一部分进行了评论。所有列都可以正常工作,直到最后一个(case语句)导致问题。但就像我上面所说的,当我删除所有的名字时,querty运行正常。

非常感谢任何帮助。

由于

完整代码:

WITH QUEST_CTE AS (

SELECT CompanyID AS Company_ID, Q.QuestionID, QuestionText, CAST(Q.Answer AS VARCHAR) AS Answer
FROM Questionnaire AS Q
JOIN CallCentre_Web.dbo.Questionnaire_Questions QQ
ON Q.QuestionID = QQ.QuestionID
JOIN dbo.ProfileMapping AS PM
ON PM.ProfileMappingID = Q.ProfileMappingID
JOIN CampaignMapping AS CM
ON PM.MapID = CM.MapID
WHERE QuestionTypeID = 0
AND QQ.GoldenField = 1
AND CampaignID = 18
UNION ALL

SELECT CompanyID AS Company_ID, Q.QuestionID, QuestionText, CAST(QA.Answer AS VARCHAR) 
FROM Questionnaire AS Q
JOIN CallCentre_Web.dbo.Questionnaire_Questions QQ
ON Q.QuestionID = QQ.QuestionID
JOIN CallCentre_Web.dbo.Questionnaire_Answers QA
ON Q.Answer = QA.AnswerID
JOIN dbo.ProfileMapping AS PM
ON PM.ProfileMappingID = Q.ProfileMappingID
JOIN CampaignMapping AS CM
ON PM.MapID = CM.MapID
WHERE QuestionTypeID = 1
AND QQ.GoldenField = 1
AND CampaignID = 18
UNION ALL

SELECT CompanyID AS Company_ID, Q.QuestionID, QuestionText,
        CAST(STUFF((select ',' + e.Answer
        FROM Questionnaire a
        cross apply dbo.SplitString(a.Answer,',') b 
        join CallCentre_Web.dbo.Questionnaire_Answers e 
        on b.s = e.AnswerID
        where isnumeric(b.s) = 1 
        and QuestionTypeID = 2 
        and a.QuestionnaireID = q.QuestionnaireID
        for xml path('')),1,1,'') as Varchar) as Answer
FROM Questionnaire AS q
JOIN dbo.ProfileMapping AS PM
ON PM.ProfileMappingID = Q.ProfileMappingID
JOIN CampaignMapping AS CM
ON PM.MapID = CM.MapID
JOIN CallCentre_Web.dbo.Questionnaire_Questions QQ
ON Q.QuestionID = QQ.QuestionID
WHERE QuestionTypeID = 2
AND QQ.GoldenField = 1
AND CampaignID = 18
)

select CAST(Quotename('GS Id',char(34))as Varchar(255)),
CAST(Quotename('CompanyName', char(34))as Varchar(255)),
CAST(Quotename('Address', char(34))as Varchar(255)),
CAST(Quotename('Country',  char(34))as Varchar(255)),
CAST(Quotename('SicDescription',  char(34))as Varchar(255)),
CAST(Quotename('Sector', char(34))as Varchar(255)),
CAST(Quotename('Employees',  char(34))as Varchar(255)),
CAST(Quotename('Website', char(34))as Varchar(255)),
CAST(Quotename('CompanyTurnover', char(34))as Varchar(255)),
CAST(Quotename('Phone',  char(34))as Varchar(255)),
CAST(Quotename('MobilePhone', char(34))as Varchar(255)),
CAST(Quotename('Fax',  char(34))as Varchar(255)),
CAST(Quotename('Salutation',  char(34))as Varchar(255)),
CAST(Quotename('Forename',  char(34))as Varchar(255)),
CAST(Quotename('Surname',  char(34))as Varchar(255)),
CAST(Quotename('Jobtitle', char(34))as Varchar(255)),
CAST(Quotename('EmailAddress', char(34))as Varchar(255)),
CAST(Quotename('LeadSource', char(34))as Varchar(255))
--CAST(Quotename('CallResult', char(34))as Varchar(255)),
--CAST(Quotename('What is your company registration number?', char(34))as Varchar(255))
--CAST(Quotename('Who is your electricity supplier?', char(34))as Varchar(255)),
--CAST(Quotename('Number of sites', char(34))as Varchar(255)),
--CAST(Quotename('MPAN Number', char(34))as Varchar(255)),
--CAST(Quotename('Do you use Half Hourly Meters?', char(34))as Varchar(255)),
--CAST(Quotename('What is your average use for electricity? (in GWh)', char(34))as Varchar(255)),
--CAST(Quotename('Do you use a fixed or flexible contract for your electricity? (if over 20GWh)', char(34))as Varchar(255)),
--CAST(Quotename('What is your average spend on electricity?', char(34))as Varchar(255)),
--CAST(Quotename('Do you have any involvement with a TPI?', char(34))as Varchar(255)),
--CAST(Quotename('If so, who is your TPI Supplier?',char(34))as Varchar(255)),
--CAST(Quotename('Electricity Contract Expiry Date',char(34)) as Varchar(255))

union all


select
Quotename(cast(C.CompanyID as varchar(255)),char(34)),  
Quotename(CompanyName, char(34)),
Quotename(ISNULL(AddressLine1,'') + case when addressline1 is null then '' else ', ' end + ISNULL(AddressLine2,'') + case when addressline2 is null then '' else ', ' end + ISNULL(AddressLine3,'') + case when addressline3 is null then '' else ', ' end + ISNULL(Town,'') + case when town is null then '' else ', ' end + ISNULL(County,'') + case when county is null then '' else ', ' end + ISNULL(Region,'') + case when region is null then '' else ', ' end + Postcode, char(34)),
Quotename('UK', char(34)),
Quotename(SIC.SicDescription, char(34)),
Quotename(sec.Sector, char(34)),
cast(quotename(ISNULL(NumberOfEmployees,''),char(34)) as varchar(255)),
cast(Quotename(ISNULL(Website,''),char(34)) as Varchar(255)),
cast(Quotename(ISNULL(CompanyTurnover,''),char(34)) as varchar(255)),
Quotename(ISNULL(MainTel.Phonenumber,''),char(34)) ,
Quotename(ISNULL(MobTel.PhoneNumber,''),char(34)),
Quotename(ISNULL(FaxTel.PhoneNumber,''), char(34)),
Quotename(ISNULL(Salutation,''), char(34)),
Quotename(ISNULL(Forename,''), char(34)),
Quotename(ISNULL(Surname,''), char(34)),
Quotename(ISNULL(Jobtitle,''),char(34)),
Quotename(ISNULL(EmailAddress,''),char(34)),
CASE  WHEN [What is your average use for electricity? (in GWh)] >= 1.5 OR [What is your average spend on electricity?] >= 150000 THEN quotename('GS1',char(34))
      WHEN [What is your average use for electricity? (in GWh)] < 1.5 OR [What is your average spend on electricity?] < 150000 THEN quotename('GS1 SME',char(34))
      ELSE quotename('GS1',char(34)) END
--Quotename(CallResult,char(34))
--Quotename(ISNULL([What is your company registration number?],''),char(34))
--CASE WHEN ISNULL([Who is your electricity supplier?],'') = 'Other' THEN ISNULL([If other please state who (electric)],'')
--ELSE ISNULL([Who is your electricity supplier?],'') END,
--Quotename(ISNULL([Number of sites],''),char(34))
--ISNULL([MPAN Number],''),
--ISNULL([Do you use Half Hourly Meters?],'') ,
--ISNULL([What is your average use for electricity? (in GWh)],''),
--ISNULL([Do you use a fixed or flexible contract for your electricity? (if over 20GWh)],''),
--ISNULL([What is your average spend on electricity?],''),
--ISNULL([Do you have any involvement with a TPI?],''),
--ISNULL([If so, who is your TPI Supplier?],''),
--CAST(ISNULL(replace(convert(Varchar(10),[Electricity Contract Expiry Date],103),'-','/'),'') AS Varchar(20))
from company c
join callcentre_web.dbo.LK_SIC as sic
on sic.sicid = c.sicid
and sic.sictype = c.sictype
join callcentre_web.dbo.LK_Sector as sec
on sec.sectorid = sic.sectorid
and sic.sictype = sec.SectorType
left join ( 
            SELECT * FROM (
            select row_number() over (partition by companyID order by C.CompanyID) as ROW, 
            CompanyID, Surname , Forename , s.salutation, Emailaddress, Jobtitle
            from contacts as C
            join callcentre_web.dbo.lk_Salutation as s
            on c.salutation = s.salutationid
            join contactdetails as cd 
            on cd.contactid = c.contactid
            and campaignid = 18
            where responsibilityID = 3 /* PRIMARY CONTACT */
            ) x
            WHERE ROW = 1
           ) as Con
on Con.CompanyID = c.CompanyID
left join (
            SELECT * FROM (
            select row_number() over (partition by cn.companyID order by Cn.CompanyID) as ROW,
            cn.companyid, PhoneNumber
            from contactnumbers cn
            join company as c
            on c.companyid = cn.companyid
            join campaignmapping as cm
            on cm.companyid = c.companyid
            where phonetypeid not in (3,4,5)--Mobile, TPS, Fax
            and campaignid = 18
            ) x
            where row = 1
            ) AS MainTel
ON MainTel.companyid = C.companyID
left join (
            SELECT * FROM (
            select row_number() over (partition by cn.companyID order by Cn.CompanyID) as ROW,
            cn.companyid, PhoneNumber
            from contactnumbers cn
            join company as c
            on c.companyid = cn.companyid
            join campaignmapping as cm
            on cm.companyid = c.companyid
            where phonetypeid = 3--Mobile
            and PhoneNumber like '07%'
            and campaignid = 18
            ) x
            where row = 1
            ) AS MobTel
ON MobTel.companyid = C.companyID
left join (
            SELECT * FROM (
            select row_number() over (partition by cn.companyID order by Cn.CompanyID) as ROW,
            cn.companyid, PhoneNumber
            from contactnumbers cn
            join company as c
            on c.companyid = cn.companyid
            join campaignmapping as cm
            on cm.companyid = c.companyid
            where phonetypeid = 5--Fax
            and campaignid = 18
            ) x
            where row = 1
            ) AS FaxTel
ON FaxTel.companyid = C.companyID
join campaignmapping as cm
on cm.companyid = c.companyid
and CampaignID = 18
join calldetails as cd
on cd.mapid = cm.mapid
left join callcentre_web..lk_callresults as lkc
on lkc.callresultid = cd.callresultid
left join ( SELECT * FROM
                        (
                            SELECT Company_ID, QuestionText, Answer
                            FROM Quest_CTE
                        ) AS source
PIVOT
(
    MAX(Answer)
    FOR QuestionText IN ([What is your company registration number?],
[Who is your electricity supplier?],[Number of sites],[If other please state who (electric)],
[MPAN Number],[Do you use Half Hourly Meters?],
[Do you use a fixed or flexible contract for your gas? (if over 20GWh)],
[What is your average use for electricity? (in GWh)],
[Do you use a fixed or flexible contract for your electricity? (if over 20GWh)],
[What is your average spend on Gas?],[What is your average spend on electricity?],
[Do you have any involvement with a TPI?],[If so, who is your TPI Supplier?],
[Gas Contract expiry date],[Electricity Contract Expiry Date])
) as pvt
) AS PV 
ON PV.Company_ID = C.CompanyID

0 个答案:

没有答案