案件情况破裂需要协助解决

时间:2011-09-29 14:03:46

标签: sql sql-server

SQL新手,所以请接受我的道歉。在

时创建了一个查询
If HBL_CLNT_CAT._HS_EB_CODE1 = 'BF' then value = TBM_BILLGRP._HS_EB_DET1
If HBL_CLNT_CAT._HS_EB_CODE2 = 'BF' then value = TBM_BILLGRP._HS_EB_DET2

但是_HS_EB_DET#超过100个字符时添加'*'。

通过帮助开发了一个查询,但它打破了条件规则,因为'then语句/操作会失败,因为它的数量大于条件语句(select _hs_eb_code1 from hbl_cat where hs_eb_code = 'bf'只返回1条记录)。

select
  case when len(format) > 100
       then left(format, 100) + '*'
       else format 
 end as format
from
  ( select 
      case when exists ( select _hs_eb_code1
                         from hbl_cat 
                         where hs_eb_code = 'bf'
                       )
           then tbm_bllgrp._hs_eb_det1 
      end
   ) as format 
     from tbm_bllgrp 

2 个答案:

答案 0 :(得分:2)

格式化代码可以帮助您找到错误。试试这个:

select
  case when len(format) > 100
       then left(format, 100) + '*'
       else format 
  end as format
from
  ( select 
      case when exists ( select _hs_eb_code1
                         from hbl_cat 
                         where hs_eb_code = 'bf'
                       )
           then tbm_bllgrp._hs_eb_det1 
      end as format 
     from tbm_bllgrp 
  ) as tmp

答案 1 :(得分:1)

以上查询在几个地方被破坏了。工作声明可能如下所示:

SELECT CASE
    WHEN len(x.myval) > 100 THEN
        left(x.myval,100) + '*'
    ELSE
        x.myval
    END AS format
 FROM (
    SELECT CASE
            WHEN h.HS_EB_CODE1 = 'BF' THEN
                t._HS_EB_DET1
            WHEN h._HS_EB_CODE2 = 'BF' THEN
                t._HS_EB_DET2
            ELSE
                'unknown option'
            END AS myval
      FROM HBL_CLNT_CAT AS h
      JOIN TBM_BILLGRP AS t ON  ???  -- how are the two tables connected?
     WHERE ??? -- some condition or do you want all rows in the table?
        ) AS x

但您需要先告诉我们,如何加入TBM_BILLGRP和HBL_CLNT_CAT,以及如何选择行。
顺便说一句,大写在SQL-Server中毫无意义。标识符不区分大小写,只要它们不用双引号" "或括号[ ]括起来。