我想将以下查询设置为Query2 varchar(max)变量,但我收到错误
Conversion Failed when converting the varchar value 'SELECT...
我想也许我错过了关于+或/或&#39s的内容 我确实不确定。
Set @Query2 = 'SELECT tc.RequestID, cast(d2.CalendarYearNumber AS VARCHAR(4))' + 'P' + ' AS [Year],
CAST(SUM(HHIncomeBelow10k) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income Less than $10,000P],
CAST(SUM([HHIncome10k-15k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $10,000 - $15,000P],
CAST(SUM([HHIncome15k-20k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $15,000 - $20,000P],
CAST(SUM([HHIncome20k-25k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $20,000 - $25,000P],
CAST(SUM([HHIncome25k-30k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $25,000 - $30,000P],
CAST(SUM([HHIncome30k-35k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $30,000 - $35,000P],
CAST(SUM([HHIncome35k-40k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $35,000 - $40,000P],
CAST(SUM([HHIncome40k-45k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $40,000 - $45,000P],
CAST(SUM([HHIncome45k-50k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $45,000 - $50,000P],
CAST(SUM([HHIncome50k-60k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $50,000 - $60,000P],
CAST(SUM([HHIncome60k-75k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $60,000 - $75,000P],
CAST(SUM([HHIncome75k-100k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $75,000 - $100,000P],
CAST(SUM([HHIncome100k-125k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $100,000 - $125,000P],
CAST(SUM([HHIncome125k-150k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $125,000 - $150,000P],
CAST(SUM([HHIncome150k-200k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $150,000 - $200,000P],
CAST(SUM(HHIncomeAbove200k) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $200,000 or moreP],
CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k]
+ [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k]
+ [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k]
+ [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS TotalP
INTO #tmpHHPct
FROM Fact.DemographicBlockGroup d
INNER JOIN Report.RequestMSTGeoID_CBG tc ON d.GeographyID = tc.GEOID
INNER JOIN dim.Date d2 ON d.DateSK = d2.DateSK
WHERE CurrentRecord = 1
AND tc.RequestID = ' + @RequestID + '
GROUP BY tc.RequestID, d2.CalendarYearNumber'
答案 0 :(得分:0)
假设@RequestID不是VARCHAR和某种类型的数字类型,如INT。
试试这个:
Set @Query2 = 'SELECT tc.RequestID, cast(d2.CalendarYearNumber AS VARCHAR(4))' + 'P' + ' AS [Year],
CAST(SUM(HHIncomeBelow10k) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income Less than $10,000P],
CAST(SUM([HHIncome10k-15k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $10,000 - $15,000P],
CAST(SUM([HHIncome15k-20k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $15,000 - $20,000P],
CAST(SUM([HHIncome20k-25k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $20,000 - $25,000P],
CAST(SUM([HHIncome25k-30k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $25,000 - $30,000P],
CAST(SUM([HHIncome30k-35k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $30,000 - $35,000P],
CAST(SUM([HHIncome35k-40k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $35,000 - $40,000P],
CAST(SUM([HHIncome40k-45k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $40,000 - $45,000P],
CAST(SUM([HHIncome45k-50k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $45,000 - $50,000P],
CAST(SUM([HHIncome50k-60k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $50,000 - $60,000P],
CAST(SUM([HHIncome60k-75k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $60,000 - $75,000P],
CAST(SUM([HHIncome75k-100k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $75,000 - $100,000P],
CAST(SUM([HHIncome100k-125k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $100,000 - $125,000P],
CAST(SUM([HHIncome125k-150k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $125,000 - $150,000P],
CAST(SUM([HHIncome150k-200k]) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $150,000 - $200,000P],
CAST(SUM(HHIncomeAbove200k) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS [Income $200,000 or moreP],
CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k]
+ [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k]
+ [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k]
+ [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT) / CAST(SUM(HHIncomeBelow10k + [HHIncome10k-15k] + [HHIncome15k-20k] + [HHIncome20k-25k] + [HHIncome25k-30k] + [HHIncome30k-35k] + [HHIncome35k-40k] + [HHIncome40k-45k] + [HHIncome45k-50k] + [HHIncome50k-60k] + [HHIncome60k-75k] + [HHIncome75k-100k] + [HHIncome100k-125k] + [HHIncome125k-150k] + [HHIncome150k-200k] + HHIncomeAbove200k) AS FLOAT)
AS TotalP
INTO #tmpHHPct
FROM Fact.DemographicBlockGroup d
INNER JOIN Report.RequestMSTGeoID_CBG tc ON d.GeographyID = tc.GEOID
INNER JOIN dim.Date d2 ON d.DateSK = d2.DateSK
WHERE CurrentRecord = 1
AND tc.RequestID = ' + CAST(@RequestID AS VARCHAR(100)) + '
GROUP BY tc.RequestID, d2.CalendarYearNumber'