如何将循环添加到SQL SELECT

时间:2017-06-27 22:42:52

标签: sql sql-server

我是SQL的新手,我的任务是重构这段代码。当我看到这个时,我想要呕吐。

我尝试修改此sproc,以便它可以返回任意数量的列而不是固定数字。这就是现在的样子。

ALTER PROCEDURE [frsuser].[usp_report_UnitFeatureHeaders]
AS
BEGIN
CREATE TABLE #features (SortOrder INT IDENTITY(1,1)
                            ,label  NVARCHAR(20)  )
INSERT INTO #features (label)
    SELECT label FROM UnitAttributeDefinition order by tab_sort_order

SELECT   'UnitID'                                                   AS UnitID
        ,'UnitName'                                                 AS UnitName
        ,'UnitAddress'                                              AS UnitAddress
        ,'RateUnitType'                                             AS RateUnitType
        ,'TaxDistrict'                                              AS TaxDistrict
        ,'ContractType'                                             AS ContractType
        ,'InactiveUnit'                                             AS InactiveUnit
        ,(SELECT ISNULL(label,'') FROM #features WHERE SortOrder = 1) AS Feature01
        ,(SELECT ISNULL(label,'') FROM #features WHERE SortOrder = 2) AS Feature02
        ,(SELECT ISNULL(label,'') FROM #features WHERE SortOrder = 3) AS Feature03
        ,(SELECT ISNULL(label,'') FROM #features WHERE SortOrder = 4) AS Feature04
        .......
        ,(SELECT ISNULL(label,'') FROM #features WHERE SortOrder = 100) AS Feature100
    DROP TABLE #features
END

这就是我希望它看起来像什么的想法。

ALTER PROCEDURE [frsuser].[usp_report_UnitFeatureHeaders]
@numberOfFeatures Int = 100
AS
BEGIN
CREATE TABLE #features (SortOrder INT IDENTITY(1,1)
                            ,label  NVARCHAR(20)  )
INSERT INTO #features (label)
    SELECT label FROM UnitAttributeDefinition order by tab_sort_order

DECLARE @counter INT = 0;
DECLARE @featureName NVARCHAR (20) = 'Feature';


SELECT   'UnitID'                                                   AS UnitID
        ,'UnitName'                                                 AS UnitName
        ,'UnitAddress'                                              AS UnitAddress
        ,'RateUnitType'                                             AS RateUnitType
        ,'TaxDistrict'                                              AS TaxDistrict
        ,'ContractType'                                             AS ContractType
        ,'InactiveUnit'                                             AS InactiveUnit
        (WHILE @counter < @numberOfFeatures
            BEGIN 
                SET @featureName = 'Feature' + @counter;
                (SELECT ISNULL(label,'') FROM #features WHERE SortOrder = @counter) 
            END 
        ) AS @featureName

DROP TABLE #features

END

这样的重构是否可能?如果是这样,它会是什么样子?

1 个答案:

答案 0 :(得分:2)

您应该尝试构建动态sql查询,使用while循环连接所需的所有标签,然后使用 sp_executesql 运行查询,它应该是这样的:

    ALTER PROCEDURE [frsuser].[usp_report_UnitFeatureHeaders]
@numberOfFeatures Int = 100
AS
BEGIN
CREATE TABLE #features (SortOrder INT IDENTITY(1,1)
                            ,label  NVARCHAR(20)  )
INSERT INTO #features (label)
    SELECT label FROM UnitAttributeDefinition order by tab_sort_order

DECLARE @counter INT = 1;
DECLARE @featureName NVARCHAR (20) = 'Feature';


-- 
DECLARE @SQL_QUERY NVARCHAR(MAX)


SET @SQL_QUERY =
' 
SELECT   ''UnitID''                                                   AS UnitID
        ,''UnitName''                                                 AS UnitName
        ,''UnitAddress''                                              AS UnitAddress
        ,''RateUnitType''                                             AS RateUnitType
        ,''TaxDistrict''                                              AS TaxDistrict
        ,''ContractType''                                             AS ContractType
        ,''InactiveUnit''                                             AS InactiveUnit '

-- ADD FEATURES TO YOUR QUERY

WHILE @counter <= @numberOfFeatures

BEGIN 

    SET @SQL_QUERY = CAST((SELECT ISNULL(label,'') FROM #features WHERE SortOrder = @counter) AS nvarchar(100)) + ' AS Feauture '+ CAST(@counter as varchar(4)) + ' '
END


SET @SQL_QUERY = 'FROM  TABLE ....' --END OF THE QUERY STATEMENT

DROP TABLE #features    

EXEC sp_executesql @statement = @query 

我认为你应该使用包含所有标签的表的Count来获取@numberoffeautures,而不是作为存储过程的参数