I have a problem using Intersections in SQL:
The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information.
I'm using a for loop to generate all queries to intersect and retrieve the desired data.
My problem:
I need to select varC where exists a varA in a list of values AND varB also in a list of values.
Using WHERE IN varA ('','','','',...'') and varB ('','','','',...'') the result are not correct because it not restrict all values.
So I'm using INTERSECT:
for (int i = 0; i < varA.Count; i++)
{
for (int j = 0; j < varB.Count; j++)
{
sqlQuery += @"SELECT DISTINCT varC FROM tblData WHERE varD='XPTO' AND varA='" + varA[i] + "' AND varB='" + varB[j] + "' INTERSECT ";
}
}
sqlQuery = sqlQuery.Remove(sqlQuery.Length - 10, 10); //to remove last INTERSECT
sqlQuery += " ORDER BY varC ASC";
There is any idea how to do it?
It works using a small dataset but if we increase the list of values it crashes even using a high command timeout.
Thanks in advance
INTERSECToperator? It doesn't feel correct to me since it means it will return distinct values that are returned by both the LEFT and the RIGHT query. Can you indeed give an example of the question you want to ask to the database? I'm sure there is a solution for your requirements that do not require an individual query for each combination ofvarAandvarB.