When I write the following SQL statement, I get the result set that I want to input the @ c parameter.
Select * from @ tb t where t. id in (select id from tb where f = @ c)
However, if there are @ a, @ B, and @ c, and they respectively want to get different result sets from @ tb, for example
Copy codeThe Code is as follows:
If @ a is not null
Begin
-- Get What @ a wants
End
If @ B is not null
Begin
-- Get What @ B wants
End
If @ c is not null
Begin
-- Get What @ c wants
End
It seems that there is no problem, but if @ a and @ B are together, or even @ a, @ B, @ c, @ d, @ e, @ f and so on. Therefore, you must build @ tb first and then perform one-time query.
-- Construct @ tb
Select * from @ tb
If I have passed @ a and @ B to get a @ tb result set, when I use @ c again for conditional judgment, the result will be overwritten.
You can use the "delete records that do not meet the conditions" method. Because @ tb has obtained the results @ a and @ B, therefore, you only need to delete the results that do not match @ c. .
Copy codeThe Code is as follows:
If @ c is not null
Begin
Delete c from @ tb c where c. id not in (select id from tb where f = @ c)
End
Select * from @ tb