ALTER PROCEDURE [dbo].[SP]
AS
BEGIN
SELECT CMS_ORG.GUID, CM_ORG.NAME AS Name
FROM CMS_ORG
UNION
SELECT CMS_ORG.GUID, CMS_ORG_HISTORY.NAME
FROM CMS_ORG_HISTORY INNER JOIN CMS_ORG ON CMS_ORG_HISTORY.GUID = CMS_ORG.GUID
UNION
SELECT CMS_SHARE.GUID,CMS_SHARE.NAME
FROM CMS_ORG INNER JOIN CMS_SHARE ON CMS_ORG.GUID = CMS_SHARE.GUID
END
我是否可以将第二个查询显示为第一个输出而不是第三个查询。当我执行存储过程时,第三个查询的输出先显示,然后显示第一个查询,最后显示第二个查询。如果GUID全部相等,我如何对其进行排序以显示?
答案 0 :(得分:2)
添加另一个参数并按顺序排序:
ALTER PROCEDURE [dbo].[SP]
AS
BEGIN
SET NOCOUNT ON;
SELECT CMS_ORG.GUID, CM_ORG.NAME AS Name, 1 as ord
FROM CMS_ORG
UNION
SELECT CMS_ORG.GUID, CMS_ORG_HISTORY.NAME, 2 as ord
FROM CMS_ORG_HISTORY INNER JOIN CMS_ORG ON CMS_ORG_HISTORY.GUID = CMS_ORG.GUID
UNION
SELECT CMS_SHARE.GUID,CMS_SHARE.NAME, 3 as ord
FROM CMS_ORG INNER JOIN CMS_SHARE ON CMS_ORG.GUID = CMS_SHARE.GUID
order by ord
END
只需按照您想要的顺序更改数字。