SQL Server Query提供ASP错误

时间:2015-02-10 14:39:23

标签: sql asp.net sql-server

我有一系列SQL查询可以在网上查询分析器中正常工作,但我正在努力使它们适合我的asp页面运行。

查询是......

SELECT part1 AS 'idParts',
       count(*) AS 'Total' INTO #TempTable
FROM T_Jobs
WHERE MONTH(Date) = 1
  AND YEAR(Date) = 2015
GROUP BY part1
UNION
SELECT part2 AS 'idParts',
       count(*) AS 'Total'
FROM T_Jobs
WHERE MONTH(Date) = 1
  AND YEAR(Date) = 2015
GROUP BY part2
UNION
SELECT part3 AS 'idParts',
       count(*) AS 'Total'
FROM T_Jobs
WHERE MONTH(Date) = 1
  AND YEAR(Date) = 2015
GROUP BY part3
DELETE
FROM #TempTable
WHERE idParts = 'Select Part:'
  SELECT #TempTable.idParts,#TempTable.Total,
                             T_Parts.partCost INTO #TempTable2
  FROM #TempTable
  JOIN T_Parts ON #TempTable.idParts=T_Parts.idParts
  SELECT Sum(Total*partCost) AS RESULT
  FROM #TempTable2

这将返回当月使用的所有零件的总成本,从一个表中检索零件清单,并从另一个表中检索该零件的相关价格。

但是我怎么把它放到asp页面中所以我可以oConn.Execute(strSQL)它来显示来自#TempTable2的'Result'?

我似乎无法找出strSQL执行它的正确语法。

任何帮助或指导将不胜感激!谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个......

 strSQL = "select part1 as 'idParts',count(*) as 'Total' into #TempTable from     T_Jobs where MONTH(Date) = 1 AND YEAR(Date) = 2015 group by part1 " &
"UNION " &
 "select part2 as 'idParts',count(*) as 'Total' from T_Jobs where MONTH(Date) = 1 AND YEAR(Date) = 2015 group by part2 " &
 "UNION " &
 "select part3 as 'idParts',count(*) as 'Total' from T_Jobs where MONTH(Date) = 1 AND YEAR(Date) = 2015 group by part3 " &
 "GO " &
 "delete from #TempTable where idParts = 'Select Part:' " &
 "GO " &
 "select #TempTable.idParts,#TempTable.Total,T_Parts.partCost into #TempTable2 from #TempTable join T_Parts on #TempTable.idParts=T_Parts.idParts "  &
 "GO " &
 "select Sum(Total*partCost) as Result from #TempTable2 "

答案 1 :(得分:0)

好的解决了...这就是我的表现(在你的帮助下!)

我不得不将查询拆分为4个部分,并将临时表设为半永久表,以便我可以在不同的查询之间引用它。

strSQL ="选择part1为' idParts',将()计为' Total'来自T_Jobs的## TempTable,其中MONTH(日期)=" &安培; exp_month& "和年(日期)=" &安培; exp_year& "按第1部分分组" &安培; " UNION ALL" &安培; "选择part2作为' idParts',将()计为' Total'来自T_Jobs,其中MONTH(日期)=" &安培; exp_month& "和年(日期)=" &安培; exp_year& "第2部分组#34; &安培; " UNION ALL" &安培; "选择part3作为' idParts',将()计为' Total'来自T_Jobs,其中MONTH(日期)=" &安培; exp_month& "和年(日期)=" &安培; exp_year& "按第3部分分组"
strSQL2 ="从## TempTable中删除idParts ='选择部分:' "
strSQL3 ="选择## TempTable.idParts,## TempTable.Total,T_Parts.partCost到## TempTable2 ## TempTable join T_Parts on ## TempTable.idParts = T_Parts.idParts"
strSQL4 ="从## TempTable2"

中选择Sum(Total partCost)作为结果

exp_month和exp_year变量来自引用表单,因此我可以选择要查看的日期。

是一种享受。谢谢你的帮助。