How to retrieve different result sets for a temporary table based on multiple conditions

Source: Internet
Author: User
In many cases, you need to use multiple parameters to query a temporary table. Each parameter requires different result sets. Some parameters do not correspond to fields in this temporary table, but to fields in another table associated with the temporary table ID.

In many cases, you need to use multiple parameters to query a temporary table. Each parameter requires different result sets. Some parameters do not correspond to fields in this temporary table, but to fields in another table associated with the temporary table ID.

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
The 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. .
The 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.