在动态sql查询中循环

时间:2014-08-27 16:58:53

标签: sql sql-server

我几乎得到了这个动态查询:

DECLARE 
@ord int = 1,
@wght int = 0,
@bit int,
@colname varchar(max),
@col2 varchar(max),
@sstr varchar(max),
@fstr varchar(max),
@wstr varchar(max),
@sum varchar(max) = ' 0',
@colist varchar(max) = '',
@frlist varchar(max) = '',
@whlist varchar(max) = '',
@sql varchar(max)

WHILE @wght < 2
BEGIN
    SET @wght = @wght + 1

        WHILE @ord < 3
        BEGIN
            SET @ord = @ord + 1
            SELECT @colname = BR FROM dbo.[Y2 Ordinal] WHERE ORDINAL_POSITION= @ord
            SET @sstr = '(A.' + @colname + '*B' + CAST(@wght AS varchar(max))+'.' + @colname +')'
            SET @sum = @sum + '+' + @sstr
        END

    SELECT @w  = [ID NO] FROM dbo.[Weightings] WHERE [ID NO]= @wght 

    SET @fstr = ' dbo.[Weightings] AS B' + CAST(@wght AS varchar(max))


    SELECT @col2 = [Batch ID] FROM dbo.[Weightings] WHERE [ID NO]= @wght
    SET @wstr = 'B' + CAST(@wght AS varchar(max)) + '.[Portfolio ID] = ' + '''' + @col2 + ''''

    SET @colist = @colist + ',' +(@sum)+' AS ' +''''+(@col2)+''''
    SET @frlist = @frlist + ',' +(@fstr)
    SET @whlist = @whlist  + ' AND ' +(@wstr)


END



SET @sql  = '(SELECT A.Sim' + @colist+ ' FROM dbo.[Y2 Net of Fees] As A' + @frlist +' WHERE A.[Simulation] IS NOT NULL ' + @whlist + ')ORDER BY Simulation'



PRINT(@sql);

输出sql语句(在此重新格式化)是:

(SELECT A.Sim,
0+(A.[Apples]*B1.[Apples])+(A.[Oranges]*B1.[Oranges])  AS 'Batch A', 
0+(A.[Apples]*B1.[Apples])+(A.[Oranges]*B1.[Oranges]) AS 'Batch B' 

FROM dbo.[Fruit] As A, 
dbo.[Weightings] AS B1, 
dbo.[Weightings] AS B2

WHERE A.[Simulation] IS NOT NULL  
AND B1.[Batch ID] = 'Batch A'
AND B2.[Batch ID] = 'Batch B')

ORDER BY Simulation

我想要的输出是:

SELECT A.Sim,
0+(A.[Apples]*B1.[Apples])+(A.[Oranges]*B1.[Oranges]) AS 'Batch A', 
0+(A.[Apples]*B2.[Apples])+(A.[Oranges]*B2.[Oranges]) AS 'Batch B' 

所以我的问题是在第二个while循环中@wght保持静态,我在想它在第一个循环中它增加的时间它将进入第二个循环。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

USE APPLICATION
GO

SET NOCOUNT ON

IF OBJECT_ID('tempdb.dbo.#Y2_Ordinal') IS NOT NULL
BEGIN
    DROP TABLE #Y2_Ordinal
END

IF OBJECT_ID('tempdb.dbo.#Weightings') IS NOT NULL
BEGIN
    DROP TABLE #Weightings
END

GO

CREATE TABLE #Y2_Ordinal(
   COLUMN_NAME VARCHAR(100)
   ,ORDINAL_POSITION INT
);

CREATE TABLE #Weightings(
    [Batch ID] INT
   ,[ID NO] INT
);

GO

INSERT INTO #Y2_Ordinal
            (COLUMN_NAME
             ,ORDINAL_POSITION) 
        VALUES('Apples'
                ,'1')

INSERT INTO #Y2_Ordinal
            (COLUMN_NAME
             ,ORDINAL_POSITION) 
        VALUES('Oranges'
                ,'2')       

--SELECT * FROM #Y2_Ordinal

--INSERT INTO #Y2_Ordinal
--          (COLUMN_NAME
--           ,ORDINAL_POSITION) 
--      VALUES('Grapes'
--              ,'3')   

INSERT INTO #Weightings VALUES('1','1') 
INSERT INTO #Weightings VALUES('2','2') 
INSERT INTO #Weightings VALUES('3','3') 

DECLARE 
@ord int = 1,
@wght int = 0,
@bit int,
@colname varchar(max),
@col2 varchar(max),
@sstr varchar(max),
@fstr varchar(max),
@wstr varchar(max),
@sum varchar(max) = '',
@colist varchar(max) = '',
@frlist varchar(max) = '',
@whlist varchar(max) = '',
@sql varchar(1000),
@w   VARCHAR(max)

WHILE @wght < 2
BEGIN
    SET @wght = @wght + 1

        --PRINT(@wght)
        SET @ord = 1
        Set @sum = ''
        WHILE @ord < 3
        BEGIN

            --PRINT('ord' + CAST(@ord AS VARCHAR(100)))

            SELECT  @colname = COLUMN_NAME FROM #Y2_Ordinal WHERE ORDINAL_POSITION= @ord
            --PRINT(@colname)
            SET @sstr = '(A.' + @colname + '*B' + CAST(@wght AS varchar(max))+'.' + @colname +')'
            --PRINT(@sstr)
            SET @sum = @sum + '+' + @sstr
            --PRINT(@sum)
            SET @ord = @ord + 1
        END



    SELECT @w  = [ID NO] FROM #Weightings WHERE [ID NO]= @wght 

    SET @fstr = ' dbo.[Weightings] AS B' + CAST(@wght AS varchar(max))


    SELECT @col2 = [Batch ID] FROM #Weightings WHERE [ID NO]= @wght
    --PRINT ('col2' + @col2)
    SET @wstr = 'B' + CAST(@wght AS varchar(max)) + '.[Portfolio ID] = ' + '''' + @col2 + ''''

    SET @colist = @colist + ', 0' +(@sum)+' AS ' +''''+(@col2)+''''
    --PRINT ('col_list' + @colist)
    SET @frlist = @frlist + ',' +(@fstr)
    SET @whlist = @whlist  + ' AND ' +(@wstr)
END


--PRINT(@colist)
--PRINT(@frlist)
--PRINT(@whlist)

SET @sql  = '(SELECT A.Sim' + @colist+ ' FROM dbo.[Y2 Net of Fees] As A' 
            + @frlist +' WHERE A.[Simulation] IS NOT NULL ' 
            + @whlist + ')ORDER BY Simulation'

PRINT @SQL