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
或可以是空白字符串。
如何在存储过程中将这个查询连接在一起?
谢谢
传入@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;
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。