有一个问题,并希望有人可以帮助我。在@sqlMain中设置的动态sql当前正在抓取一个名为 lg.ColorGroup 的列,但是我实际上不想抓取该列,当我删除它时虽然我无法执行< em> ORDER BY ColorGroup 。有没有办法让ColorGroup在没有选中的情况下返回排序的结果集。谢谢!
INSERT INTO #ServerList
SELECT DISTINCT ServerURL, ServerName, InstanceType
FROM [CR_PIT_01].[dbo].[EFDM_Servers]
WHERE InstanceType = 'Production'
SELECT @ProgramList = STUFF((SELECT ',' + QUOTENAME(ServerName) FROM #ServerList ORDER BY ServerURL FOR XML PATH (''), type).value('.', 'nvarchar(max)'), 1, 1, '')
PRINT @ProgramList
CREATE Table #ServerListGrouped
(
Name varchar(300),
ColorGroup int
)
INSERT INTO #ServerListGrouped
SELECT DISTINCT Name, (case when SUBSTRING(LOWER(Name), 0, 3) = 'y_' then 2 when SUBSTRING(Lower(Name), 0, 3) = 'z_' then 3 when Exists (Select BuiltInField FROM [CR_PIT_01].[dbo].[EFDM_BuiltInFields] WHERE BuiltInField = Name) then 4 else 1 end) As FieldUsage
FROM CR_PIT_01.dbo.EFDM_ProjectFields
ORDER BY FieldUsage ASC, Name
SET @sqlMain = 'SELECT * FROM (SELECT P.Name, lg.ColorGroup, P.'+ @FilterValue +' , S.ServerName FROM [CR_PIT_01].[dbo].[EFDM_ProjectFields] AS P JOIN #ServerList AS S ON S.ServerURL = P.ServerURL Join #ServerListGrouped LG on p.Name = lg.Name) AS P PIVOT ( MIN(' + @FilterValue + ') FOR ServerName IN (' + @ProgramList + ')) AS PIV Where Name LIKE (''%' + @Name + '%'') ORDER BY colorgroup, PIV.Name'
--SET @sqlMain = 'SELECT * FROM (SELECT P.Name, P.'+ @FilterValue +' , S.ServerName FROM [CR_PIT_01].[dbo].[EFDM_ProjectFields] AS P JOIN #ServerList AS S ON S.ServerURL = P.ServerURL ) AS P PIVOT ( MIN(' + @FilterValue + ') FOR ServerName IN (' + @ProgramList + ')) AS PIV Where Name LIKE (''%' + @Name + '%'') ORDER BY PIV.Name;'
PRINT @sqlMain;
--EXEC sp_executesql @sqlMain;
DROP TABLE #ServerList
DROP TABLE #ServerListGrouped
谢谢
答案 0 :(得分:1)
从外部SELECT列表中删除ColorGroup,并将其保留在ORDER BY中。
不必选择用于订购的列。
这只是意味着您必须指定外部选择列表,而不是使用var heap = [];
heap.load = ... // same as above, but all instances of window.heap are replaced with just heap
window.heap = heap;
heap.load(APP_ID)
答案 1 :(得分:1)
将第一个select子句中动态sql中的星号更改为不包含colorgroup的显式列表。
SET @sqlMain = 'SELECT * FROM (SELECT P.Name, lg.ColorGroup, P.'+ @FilterValue +' , S.ServerName FROM
变为
SET @sqlMain = 'SELECT Name, '+ @FilterValue +' , ServerName FROM (SELECT P.Name, lg.ColorGroup, P.'+ @FilterValue +' , S.ServerName FROM ...
答案 2 :(得分:0)
SELECT PIV.Name FROM (...) ORDER BY colorgroup, PIV.Name
是一个有效的SQL。