简体   繁体   English

使用视图联合插入表中导致问题

[英]insert into table using union of views causing issues

I am trying to insert data into a temp table by doing union of view but i am getting following error.What could be the reason for it. 我试图通过执行视图联合将数据插入到临时表中,但我收到以下错误。这可能是它的原因 When i do normal select the query runs fine. 当我正常选择查询运行正常。

Query processor could not produce a query plan because of the hints defined in this query. 由于此查询中定义的提示,查询处理器无法生成查询计划。 Resubmit the query without specifying any hints and without using SET FORCEPLAN. 重新提交查询而不指定任何提示,也无需使用SET FORCEPLAN。

insert into #temp
SELECT 'aaa', COUNT(*) FROM view1
 UNION
 SELECT 'bbb', COUNT(*) FROM view2
 UNION
 SELECT 'ccc', COUNT(*) FROM view3
 UNION
 SELECT 'ddd', COUNT(*) FROM view4
 UNION
 SELECT 'eee', COUNT(*) FROM view5
 UNION
 SELECT 'fff', COUNT(*) FROM view6
 UNION
 SELECT 'ggg', COUNT(*) FROM view7
 UNION
 SELECT 'hhh', COUNT(*) FROM view8

What you have written should work. 您所写的应该可以。 Check for 2 things. 检查2件事。 Since it is a temporary table, check if removing any view from the UNION of views fixes it. 由于它是一个临时表,因此请检查是否从视图的UNION中删除任何视图都可以修复该表。 Then, check that view individually. 然后,单独检查该视图。 Script all your views and check if any of those use hints like NOLOCK, EXPAND etc. and which one is causing the problem. 编写所有视图的脚本,并检查其中是否使用了诸如NOLOCK,EXPAND等提示,以及哪个引起了问题。

Also, you can try writing it like this 另外,您可以尝试这样写

insert into #temp
SELECT field, total FROM 
 (SELECT 'aaa' field, COUNT(*) total FROM view1
 UNION
 SELECT 'bbb', COUNT(*) FROM view2
 UNION
 SELECT 'ccc', COUNT(*) FROM view3
 UNION
 SELECT 'ddd', COUNT(*) FROM view4
 UNION
 SELECT 'eee', COUNT(*) FROM view5
 UNION
 SELECT 'fff', COUNT(*) FROM view6
 UNION
 SELECT 'ggg', COUNT(*) FROM view7
 UNION
 SELECT 'hhh', COUNT(*) FROM view8) Z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM