I am using SQL Server 2008 R2 on Windows 7, and I am trying to fill in the missing rows in a table. For background, I have
a set of "parameters" which all have a (string) value. There are about 200 of these parameters
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.
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.
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. 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.
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.
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.
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...