SQL - SELECT withi a SELECT
I was trying to re-write some of the stored procedures for one of my office project the other day to exclude certain unwanted entries. It was done in such a manner.
SELECT *
FROM [table_A]
LEFT JOIN [table_B]
ON [table_A].column_id = [table_B].column_id
WHERE [table_A].column_c NOT IN
(
SELECT [table_A].column_c
FROM [table_A]
WHERE [table_A].column_c LIKE '%something'
OR [table_A].column_c LIKE '%somethingC'
OR [table_A].column_c IN ('value_A', 'value_B', 'value_C')
)
I tested it in the SQL 2005 Manager Console, and it works fine. I tried it out as well on the actual website, and it does work as well. However, it did a significant drawback that I was not able to notice. This statement will not return anything, or throwing an exception saying "Timeout operation" when it was trying to return a large number of rows.
I can't figure out why it doesn't work when it was executed from another media, i.e. a ASP.NET C# codes, and it's really baffling. The reason why I wanted to do it as the above is because I wanted to have the code clean, and I could just simply copy & paste the sub select code to a new stored procedure. Because of this problem, I have to re-write the code again to exclude the sub select and put in extra conditions under WHERE clause.
I don't really know if it's the SQL problem or it's the .NET Framework problems because my friend told me that it was SQL, but as far as I noticed, if it's SQL problem, then it should throw "operation timeout" as well when it was executed within the SQL 2005 Manager Console, so why it doesn't?
SELECT *
FROM [table_A]
LEFT JOIN [table_B]
ON [table_A].column_id = [table_B].column_id
WHERE [table_A].column_c NOT IN
(
SELECT [table_A].column_c
FROM [table_A]
WHERE [table_A].column_c LIKE '%something'
OR [table_A].column_c LIKE '%somethingC'
OR [table_A].column_c IN ('value_A', 'value_B', 'value_C')
)
I tested it in the SQL 2005 Manager Console, and it works fine. I tried it out as well on the actual website, and it does work as well. However, it did a significant drawback that I was not able to notice. This statement will not return anything, or throwing an exception saying "Timeout operation" when it was trying to return a large number of rows.
I can't figure out why it doesn't work when it was executed from another media, i.e. a ASP.NET C# codes, and it's really baffling. The reason why I wanted to do it as the above is because I wanted to have the code clean, and I could just simply copy & paste the sub select code to a new stored procedure. Because of this problem, I have to re-write the code again to exclude the sub select and put in extra conditions under WHERE clause.
I don't really know if it's the SQL problem or it's the .NET Framework problems because my friend told me that it was SQL, but as far as I noticed, if it's SQL problem, then it should throw "operation timeout" as well when it was executed within the SQL 2005 Manager Console, so why it doesn't?
Comments