在select语句sql中声明和设置变量

时间:2016-12-13 02:19:34

标签: sql sql-server sql-server-2014

所以我有一段非常重复的代码需要最小化,因为我需要使用的LMS需要最多2000个字符输入。这2000个字符是" CASE"声明,而不是" FROM"后来的陈述。

基本上,我有一个用户通过LMS提交的反馈表,我想从多个表中获取该信息并构建报告。

以下是获得回复的代码:

case when 
        (select e.response_value 
        from t_qti_item_evaluation r INNER JOIN t_qti_response e 
            ON r.response_id = e.response_id INNER JOIN t_qti_evaluationflowmaterial f 
            ON f.subitem_id = r.subitem_id INNER JOIN [t_qti_evaluationmaterial] i 
            ON i.flowmaterial_id = f.flowmaterial_id 
                where i.flowmaterial_id = 1385628 
                    and i.position = 1 
                    and r.item_evaluation_id = b.item_evaluation_id) = 1 then 'Highly Satisfied' 
  when 
    (select e.response_value 
        from t_qti_item_evaluation r INNER JOIN t_qti_response e 
            ON r.response_id = e.response_id INNER JOIN t_qti_evaluationflowmaterial f 
            ON f.subitem_id = r.subitem_id INNER JOIN [t_qti_evaluationmaterial] i 
            ON i.flowmaterial_id = f.flowmaterial_id 
                where i.flowmaterial_id = 1385628 
                    and i.position = 1 
                    and r.item_evaluation_id = b.item_evaluation_id) = 2 then 'Satisfied' 
  when 
    (select e.response_value 
        from t_qti_item_evaluation r INNER JOIN t_qti_response e 
            ON r.response_id = e.response_id INNER JOIN t_qti_evaluationflowmaterial f 
            ON f.subitem_id = r.subitem_id INNER JOIN [t_qti_evaluationmaterial] i 
            ON i.flowmaterial_id = f.flowmaterial_id 
                where i.flowmaterial_id = 1385628 
                    and i.position = 1 
                    and r.item_evaluation_id = b.item_evaluation_id) = 3 then 'Disatisfied' 
  when 
    (select e.response_value 
        from t_qti_item_evaluation r INNER JOIN t_qti_response e 
            ON r.response_id = e.response_id INNER JOIN t_qti_evaluationflowmaterial f 
            ON f.subitem_id = r.subitem_id INNER JOIN [t_qti_evaluationmaterial] i 
            ON i.flowmaterial_id = f.flowmaterial_id 
                where i.flowmaterial_id = 1385628 
                    and i.position = 1 
                    and r.item_evaluation_id = b.item_evaluation_id) = 4 then 'Highly Disatisfied' 
  when 
    (select e.response_value 
        from t_qti_item_evaluation r INNER JOIN t_qti_response e 
            ON r.response_id = e.response_id INNER JOIN t_qti_evaluationflowmaterial f 
            ON f.subitem_id = r.subitem_id INNER JOIN [t_qti_evaluationmaterial] i 
            ON i.flowmaterial_id = f.flowmaterial_id 
                where i.flowmaterial_id = 1385628 
                and i.position = 1 
                and r.item_evaluation_id = b.item_evaluation_id) = 0 then 'Neither' else '' end

from 

t_qti_evaluation_status a
INNER JOIN t_qti_item_evaluation b
ON a.evaluation_id = b.item_evaluation_id 
INNER JOIN t_qti_evaluation c
ON a.content_id = c.content_id 
INNER JOIN t_qti_content d
ON a.content_id = d.content_id
INNER JOIN t_qti_response e
ON b.response_id = e.response_id
INNER JOIN t_qti_evaluationflowmaterial f
ON f.subitem_id = b.subitem_id
INNER JOIN t_qti_evaluationmaterial i
ON i.flowmaterial_id = f.flowmaterial_id
INNER JOIN person p
ON a.candidate_id = p.person_id

所以唯一改变的是响应是1,2,3,4还是0(我在反馈表中与响应关联的值)。此外,需要进行一些更改以包含更多" i.flowmaterial_id"这就是代码失败的原因(字符过多)

正如你所看到的,非常重复,所以我创建了它(它在SQL中工作但在LMS中不起作用):

declare @test varchar(100) = (select e.response_value
    from 
        t_qti_item_evaluation r INNER JOIN t_qti_response e
            ON r.response_id = e.response_id INNER JOIN t_qti_evaluationflowmaterial f
            ON f.subitem_id = r.subitem_id INNER JOIN [t_qti_evaluationmaterial] i
            ON i.flowmaterial_id = f.flowmaterial_id
    where 
        i.position = 1 and i.flowmaterial_id = 1246978 
--connects to the outer query
--and r.item_evaluation_id = b.item_evaluation_id
        ), 

        @result varchar(100)

    select @result =     CASE  @test 
            WHEN  1 THEN 'Highly Satisfied'   
            WHEN  2 THEN 'Satisfied'
            WHEN  3 THEN 'Dissatisfied' 
            WHEN  4 THEN 'Highly Dissatisfied'
            WHEN  0 THEN 'Neither' 
        END    

        print @result

这个问题是LMS只会将一个select语句作为输入,你不能只是转储代码并运行它。

所以我要问的是,是否可以对代码进行大量改进,以便能够添加更多ID,或以某种方式在select语句中获取第二个代码,以便将其放入LMS中。 / p>

不确定LMS使用的SQL版本,但我在SQL 2014中进行测试

对于扩展的问题,我们很抱歉,这只是具体的问题。

1 个答案:

答案 0 :(得分:1)

使用case的其他形式:

(case (select e.response_value 
       from t_qti_item_evaluation r INNER JOIN t_qti_response e 
            ON r.response_id = e.response_id INNER JOIN t_qti_evaluationflowmaterial f 
            ON f.subitem_id = r.subitem_id INNER JOIN [t_qti_evaluationmaterial] i 
            ON i.flowmaterial_id = f.flowmaterial_id 
       where i.flowmaterial_id = 1385628 and
             i.position = 1 and
             r.item_evaluation_id = b.item_evaluation_id
      )
      when 1 then 'Highly Satisfied' 
      when 2 then 'Satisfied' 
      when 3 then 'Disatisfied' 
      when 4 then 'Highly Disatisfied' 
      when 0 then 'Neither'
      else ''
 end)