2
Appr_ID   Status
----------------
Null      1
3         2
4         3
NULL      4
7         5
NULL      6

我在一个表中有两列,它们的值如上所示。

我需要编写一个存储过程,以便每当Appr_ID列具有 Not Null 值时,它都会从具有多个连接的某些表中获取和显示数据。

Appr_ID列为 NULL 时,它必须再次使用一些不同的条件来获取和显示数据。

我的问题是,当我尝试使用if-else语句时,无法在 if 语句中使用列值作为条件,例如

If Appr_ID is Null then
  Select...
Else
  Select...

所以,请提出替代想法来完成这个场景。

在此先感谢,克里希纳

4

3 回答 3

0

You should use a CASE statement.

Eg:

SELECT
  CASE
    WHEN Appr_ID IS NULL THEN (
      SELECT somevalue 
      FROM sometable 
           JOIN ... 
           JOIN ... 
      WHERE something=something)
    ELSE (SELECT somevalue 
          FROM someothertable 
               JOIN ... 
               JOIN ...  
          WHERE something=something)
  END as Your_Result_ColumnName

FROM
  YourTableWithApprIDInIt

This will be quite efficient if each sub-query is basically returning the same result regardless of some value in the main YourTableWithApprIDInIt table (eg Appr_ID iself). This is because that sub-query won't actually be executed over and over for each row; it's result would be cached by the DBMS and re-used for each row.

But if the subquery depends on Appr_ID (such that each subquery needs to be re-executed on each row), then rather than using sub-queries, you would probably use a JOIN instead (in your main FROM clause).

SELECT
  CASE
    WHEN a.Appr_ID IS NULL THEN b.somevalue 
    ELSE c.somevalue
  END as Your_Result_ColumnName

FROM
  YourTableWithApprIDInIt a
  JOIN sometable b ON a.x = b.x
  JOIN ... ON ..

  JOIN someothertable c ON a.x = c.x
  JOIN ... ON ..

You could still use subqueries in this scenario if you wanted, but it might not be so clean (and will possibly be less efficient, as the DBMS will be handling multiple logically disjointed result sets - although I say "possibly" because the DBMS may optimise this properly).

I'd test under your specific scenario to see if one approach yields better results than the other. If performance is even a factor.


Update:

In the case where you need to select multiple values depending on your Appr_ID value being null or not, then you'd need to repeat the case statement for each column in your main query, eg:

SELECT
  CASE
    WHEN Appr_ID IS NULL THEN (
      SELECT somevalue_1
      FROM sometable_1
           JOIN ... 
           JOIN ... 
      WHERE something=something)
    ELSE (SELECT somevalue_1
          FROM someothertable_1
               JOIN ... 
               JOIN ...  
          WHERE something=something)
  END as Your_Result_ColumnName_1,

  CASE
    WHEN Appr_ID IS NULL THEN (
      SELECT somevalue_2
      FROM sometable_2
           JOIN ... 
           JOIN ... 
      WHERE something=something)
    ELSE (SELECT somevalue_2
          FROM someothertable_2
               JOIN ... 
               JOIN ...  
          WHERE something=something)
  END as Your_Result_ColumnName_2,

  ...

FROM
  YourTableWithApprIDInIt

Or

SELECT
  CASE
    WHEN a.Appr_ID IS NULL THEN b.somevalue_1 
    ELSE c.somevalue_1
  END as Your_Result_ColumnName_1,

  CASE
    WHEN a.Appr_ID IS NULL THEN b.somevalue_2
    ELSE c.somevalue_2
  END as Your_Result_ColumnName_2,

  ...

FROM
  YourTableWithApprIDInIt a
  JOIN sometable b ON a.x = b.x
  JOIN ... ON ..

  JOIN someothertable c ON a.x = c.x
  JOIN ... ON ..

This is when the join syntax is really starting to look cleaner... But again, if each of your Your_Result_ColumnName_Xs are coming from different tables, and each of these subqueries doesn't depend on values from inside your main outer query (such that each only needs to be evaluated only one time), the first of the two patterns may be more efficient even though it doesn't look cleaner ;-)

But again, test both in your specific scenario to see if there's a difference, and it may be that they're evaluated/optimised/executed by the DBMS to be exaclty same (in which case, choose the syntax pattern that makes most sense to you!) :-)

于 2013-04-24T10:39:45.830 回答
0

你可以使用CASE WHEN声明

SELECT 
 CASE 
   WHEN a.Appr_ID IS NULL THEN  b.somevalue
   ELSE c.somevalue
 END AS ResultColumn

INNER JOIN TABLE b ON a.somecolumn = b.somecolumn
INNER JOIN TABLE c ON a.somecolumn = c.somecolumn
于 2013-04-24T11:03:00.660 回答
0

如果您需要在一个语句中执行此操作,请执行两个连接,然后使用 CASE .... WHEN 来决定在输出中使用哪些列。

对于我的意思的一个简短的例子: -

SELECT a.*, CASE WHEN a.somefield IS NULL THEN b.somefield ELSE c.someotherfield END AS OutputField
FROM SomeTable a
LEFT OUTER JOIN SecondTable b ON a.somefield = b.id
LEFT OUTER JOIN ThirdTable c ON a.somefield = c.id
于 2013-04-24T10:39:30.090 回答