开发者社区> 问答> 正文

如何在存储过程中串联字符串

0

我正在从Python调用SQL Server中的存储过程。但是,根据Python中的条件,需要传递一个并集字符串。

以下是主要存储过程查询的外观:

BEGIN
SET NOCOUNT ON;

SELECT      
    mbh.id,
    COALESCE(oc.DowntimeReasonLevel1Name, 'Nothing for Now') AS 'DowntimeReasonLevel1Name',
    COALESCE(osw.DowntimeReasonLevel2Name, 'Nothing for Now') AS 'DowntimeReasonLevel2Name',
    CAST(SUM(DATEDIFF(SECOND, de.start_time, de.end_time))/60.0 AS decimal (8,2)) as Duration
FROM    
    mixer_downtime_event mde
JOIN 
    downtime_event de ON de.id = mde.downtime_event_id
JOIN 
    oee_status_word osw ON osw.id = de.oee_status_word_id
JOIN 
    oee_category oc ON oc.id = osw.oee_category_id
JOIN 
    mixer_batch_history mbh ON mbh.id = mde.mixer_batch_history_id
JOIN 
    compound_process cp ON cp.id = mbh.compound_process_id
JOIN 
    compound c ON c.id = cp.compound_id
WHERE 
    mde.mixer_batch_history_id IN (@IdList)
    AND osw.id <> 9000
    AND DATEDIFF(MINUTE, de.start_time, de.end_time) > 0
GROUP BY 
    mbh.id, DowntimeReasonLevel1Name, DowntimeReasonLevel2Name
--+
--@downtimeUnion
END
联```  
合查询可能类似于:

```js
UNION 
    SELECT  'Changeover' AS 'DowntimeReasonLevel1Name',
    'Changeover' AS 'DowntimeReasonLevel2Name',
    %f as Duration

或可以是空白字符串。

如何在存储过程中将这个查询连接在一起?

谢谢

展开
收起
祖安文状元 2020-01-04 15:32:24 540 0
1 条回答
写回答
取消 提交回答
  • 传入@Duration作为参数,然后添加:

    UNION ALL
    SELECT NULL,
           'Changeover' AS DowntimeReasonLevel1Name,
           'Changeover' AS DowntimeReasonLevel2Name,
            @Duration as Duration
    
    

    如果您希望这是有条件的,那么您可以让@Duration双重职责作为条件标记:

    UNION ALL
    SELECT NULL,
           'Changeover' AS DowntimeReasonLevel1Name,
           'Changeover' AS DowntimeReasonLevel2Name,
            v.Duration as Duration
    FROM (VALUES (@Duration)) v(Duration)
    WHERE v.Duration IS NOT NULL;
    
    2020-01-04 15:32:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载