带有 SELECT 和 INNER JOIN 的 SQL INSERT INTO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7040390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL INSERT INTO with SELECT and INNER JOIN
提问by sXing
Hey guys I hope you can help me with this little problem.
嘿伙计们,我希望你能帮助我解决这个小问题。
I am not quite sure of how to explain the situation to you so Ill just give it a try.
我不太确定如何向你解释这种情况,所以我只是试一试。
What I am trying to do is the following:
我正在尝试做的是以下内容:
I want to insert some specific values and parameters (which I type in myself) into the table RFC_Risks, so basically every time I find a specific reason inside the table RCF_Risks, I want to write a new row that updates the priority of the RfC, every time that happens, the position shall be increased by 1.
我想在表 RFC_Risks 中插入一些特定的值和参数(我自己输入),所以基本上每次我在表 RCF_Risks 中找到特定原因时,我都想写一个新行来更新 RfC 的优先级,每次发生这种情况时,位置应增加 1。
My problem is now, that when I run this statement, I just get the SELECT part :-), not inserting is done, neither do I get a SQL statement error or anything like that. I just type in the parameters and then I get a SELECT
Table thats all.
我现在的问题是,当我运行这个语句时,我只得到 SELECT 部分:-),没有插入,我也没有得到 SQL 语句错误或类似的东西。我只是输入参数,然后我得到一个SELECT
表格。
I'm using MS Access 2010 and I hope you can help me out with my "little" problem.
我正在使用 MS Access 2010,希望您能帮助我解决我的“小”问题。
INSERT INTO RFC_Risks (RFC_No, RiskPos, Datum, Comments, RiskPrio, Reason)
SELECT RFC_Risks.RFC_No, (RFC_Risks.RiskPos +1) AS RiskPos, [Aktuelles Datum] AS Datum, [Kommentartext] AS Comments, [Neue Prio] AS RiskPrio, RFC_Risks.Reason
FROM RFC_Risks INNER JOIN Risk_Reasons ON RFC_Risks.Reason = Risk_Reasons.Reasontext
WHERE RFC_Risks.Reason = Risk_Reasons.Reasontext;
回答by HansUp
I can't spot anything about your SQL statement which would prevent it from executing and/or throw an error. (I think your WHERE clause is redundant, but that should not cause the db engine to reject it.) What method are you using to "run" it?
我无法发现有关您的 SQL 语句的任何内容,这会阻止它执行和/或引发错误。(我认为您的 WHERE 子句是多余的,但这不应该导致数据库引擎拒绝它。)您使用什么方法来“运行”它?
If you're using the Access query designer, and switch from Design View to Datasheet View, your query isn't actually executed ... Datasheet View will show you the rows which would be affected if the query were executed.
如果您正在使用 Access 查询设计器,并从设计视图切换到数据表视图,则您的查询实际上并未执行...数据表视图将显示执行查询时会受到影响的行。
The situation is the same as if you were building a delete query in the query designer ... Datasheet View would show you which rows would be deleted ifthe query were executed, but switching to Datasheet View does not delete those rows.
这种情况与在查询设计器中构建删除查询的情况相同......数据表视图会显示如果执行查询将删除哪些行,但切换到数据表视图不会删除这些行。
To executethe query, click the icon which has a red exclamation point.
要执行查询,请单击带有红色感叹号的图标。
回答by taz
Try enclosing the subselect in parentheses.
尝试将子选择括在括号中。
回答by Mathieu Pagé
You need to execute your query. If you only display it in Datasheet View, the SELECT Statement is executed and displayed to you as a kind of preview.
您需要执行查询。如果您只在数据表视图中显示它,则 SELECT 语句将被执行并作为一种预览显示给您。
To really execute the query, when you are in design mode, you can click on the "Run" button (a red exclamation mark) in the toolbar. You can also double-click on the query in the database window.
要真正执行查询,当您处于设计模式时,可以单击工具栏中的“运行”按钮(红色感叹号)。您还可以双击数据库窗口中的查询。