[英]insert table1 select from table2,table3 where
I am using SQL Server 2008 R2 on Windows 7, and I am trying to fill in the missing rows in a table. 我在Windows 7上使用SQL Server 2008 R2,并且尝试填写表中缺少的行。 For background, I have
对于背景,我有
a set of "parameters" which all have a (string) value. 一组都具有(字符串)值的“参数”。 There are about 200 of these parameters
这些参数大约有200个
a set of "context"s where I need to have a value for every parameter. 一组“上下文”,我需要为每个参数都有一个值。 There are dozens of these contexts.
这些上下文有数十种。
I am modelling this in the database with three tables like this: 我在数据库中使用三个表对此建模:
[Parameter] (
[ID] int not null,
[DefaultValue] varchar(100) not null
)
[Context] (
[ID] int not null
)
[ParameterValues] (
[ID] int IDENTITY(1,1) not null,
[ParameterID] int not null,
[ContextID] int not null,
[Value] varchar(100) not null
)
So there should be a row in the parameter values table for every combination of context and parameter. 因此,对于上下文和参数的每种组合,参数值表中都应有一行。 So for 200 parameters and 10 contexts, I should expect to have 200x10 = 2000 rows in the ParameterValues table.
因此,对于200个参数和10个上下文,我应该期望ParameterValues表中具有200x10 = 2000行。
For historical reasons, we have some parameter values missing for some contexts, and I want to fill in all the missing values in the ParameterValues table. 出于历史原因,某些上下文缺少一些参数值,我想在ParameterValues表中填写所有缺少的值。
I am trying to use the following query: 我正在尝试使用以下查询:
insert [ParameterValues]
select [Parameter].[ID], [Context].[ID], [Parameter].[DefaultValue]
from [Parameter],[Context]
where not exists
(select 1 from [ParameterValues]
where [ParameterValues].[ContextID] = [Context].[ID]
and [ParameterValues].[ParameterID] = [Parameter.[ID])
I keep getting the following error: 我不断收到以下错误:
Subquery returned more than 1 value.
子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
I guess I am just not understanding the limitations of the insert...select syntax etc. Any suggestions for getting this correct would be greatly appreciated. 我想我只是不了解insert ... select语法等的局限性。对于获得此正确性的任何建议将不胜感激。
Update: 更新:
If I just run the "select ... from ... where not exists..." then I get the correct number of rows showing up (I know how many are missing in each context). 如果我只是运行“从...中不存在的……中选择...”,那么我将得到正确显示的行数(我知道每种情况下会丢失多少行)。 I have checked those rows and they seem to have the correct values and there are no duplicates.
我已经检查了这些行,它们似乎具有正确的值,并且没有重复项。
If I do the select into a temp table, it works fine, and I get the expected contents in that temp table. 如果我将选择放入临时表中,则可以正常工作,并且可以在该临时表中获得所需的内容。 But I can't then do:
但是我不能这样做:
insert [ParameterValues] select * from #temptable
because that gives me the same error. 因为那给了我同样的错误。 So there must be something in the structure or FKs in my parameter values table that breaks this.
因此,在我的参数值表中的结构或FK中必须有某些东西可以打破这一点。
Using SSMS, I can copy and paste those rows directly into the target table and get no errors, so the values in those rows look fine, and don't break any FKs etc. I just checked again and there are no triggers or SPs in this database at all, and the FK relationships look OK. 使用SSMS,我可以将这些行直接复制并粘贴到目标表中,而不会出现任何错误,因此这些行中的值看起来很好,并且不会破坏任何FK等。我再次检查了一下,并且没有触发器或SP这个数据库,FK关系看起来还不错。
I have gone round the houses on this for the last several hours, and it is driving me nuts. 在过去的几个小时里,我一直在这附近转过屋,这让我发疯了。 In the short term, I have the data that I need to work with, but I will have to repeat this process on other copies of the DB and would like to do it in a standard repeatable way via scripts etc, rather than manual hacks...
在短期内,我拥有需要使用的数据,但是我将不得不在数据库的其他副本上重复此过程,并且希望通过脚本等以标准可重复的方式进行此操作,而不是手动操作。 ..
You can do it like this: 您可以这样做:
insert into ParameterValues (ParameterID, ContextID, Value)
select P.ID, C.ID, P.DefaultValue
from Parameter as P, Context as C
where not exists
(select top 1 * from ParameterValues as PV
where PV.ContextID = C.ID and PV.ParameterID = P.ID)
UPDATE UPDATE
Frankly speaking, your original query should work too (despite query using table aliases looks more elegant from my point of view, and it is always better to explicitly specify column names you're inserting into). 坦白地说,您的原始查询也应该起作用(尽管从我的角度来看,尽管使用表别名进行查询看起来更优雅,但始终最好明确指定要插入的列名)。 And after some thoughts I can't find a reason for your query to throw such an exception.
经过一番思考,我找不到您的查询引发此类异常的原因。
Are you sure you've provided your actual query you're having problems with? 确定要提供遇到的实际查询吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.