在子查询

时间:2015-10-12 16:33:01

标签: sql sql-server database

以下查询是"应该"使用子查询查找特定列中的项目,并对其进行透视,以使每个项目成为自己的列。子查询本身可以很好地工作,但是当我尝试将它添加到一个更大的查询中然后将子查询的结果插入表中时,我开始得到一些语法错误。以下是整个查询:

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

Insert Into tbl_AS ( Billing_Account_ID, Bill_Date, MTN, User_Name  ,Adjustment_To_Balance_Previous_Description,Adjustments_To_Previous_Balance
    ,Adjustment_Type)

Select
    As_Selection.Billing_Account_ID
    ,As_Selection.Bill_Date
    ,As_Selection.MTN
    ,As_Selection.User_Name
    ,As_Selection.Adjustment_To_Balance_Previous_Description
    ,As_Selection.Adjustments_To_Previous_Balance
    ,As_Selection.Adjustment_Type


    FROM( --Subquery in question

SELECT @cols = STUFF((SELECT distinct 
                        ',' +
                        QUOTENAME([Section_1])
                FROM [dbo].[Import_tbl_RDO]
                where [dbo].[Import_tbl_RDO].[Section_1] <> 'Prior Activity' AND [dbo].[1_Import_tbl_RDO].[Section_1] <> 'Group Details' AND [dbo].[Import_tbl_RDO].[Section_1] <> 'Wireless Line Summary' AND  [dbo].[Import_tbl_RDO].[Section_1] <> 'Wireless Details' AND  [dbo].[Import_tbl_RDO].[Section_1] <> 'Equipment & Accessories Charges'
            FOR XML PATH(''), TYPE 
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

Set @query = ' Select [Billing Account Number], [Period End Date], [Wireless Number], [User Name]' + @cols +'
    From 
        (
            Select 
                 [Amount] as SumOfAmount,[Billing Account Number], [Period End Date],  [Wireless Number], [User Name]
                 From [dbo].[Import_tbl_RDO]
                 Where [dbo].[Import_tbl_RDO].[Section ID] = ''IRA'' AND [dbo].[Import_tbl_RDO].[Section_1] IS Not Null
                 ) t
                 PIVOT 
                 (
                 SUM(SumOfAmount)
                    FOR [Section_1] IN(' + @cols + ')
                    ) p '

    Execute (@query)

    ) as AS_Selection

错误在@cols = STUFFFOR XML处显示为不正确的语法。我是否需要编写这个子查询?

编辑:

根据下面的建议,我将查询重新排序为:

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

SELECT @cols = STUFF((SELECT distinct 
                        ',' +
                        QUOTENAME([Section_1])
                FROM [dbo].[Import_tbl_RDO]
                where [dbo].[Import_tbl_RDO].[Section_1] <> 'Prior Activity' AND [dbo].[1_Import_tbl_RDO].[Section_1] <> 'Group Details' AND [dbo].[Import_tbl_RDO].[Section_1] <> 'Wireless Line Summary' AND  [dbo].[Import_tbl_RDO].[Section_1] <> 'Wireless Details' AND  [dbo].[Import_tbl_RDO].[Section_1] <> 'Equipment & Accessories Charges'
            FOR XML PATH(''), TYPE 
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

Set @query = 'INSERT INTO tbl_AS (
    Billing_Account_ID
    ,Bill_Date
    ,MTN
    ,User_Name
    ,As_Selection.Adjustment_To_Balance_Previous_Description
    ,As_Selection.Adjustments_To_Previous_Balance
    ,As_Selection.Adjustment_Type
    )


            Select
                As_Selection.[Billing Account Number]
                ,As_Selection.[Period End Date]
                ,As_Selection.[Wireless Number]
                ,As_Selection.[User Name]
                ,As_Selection.Adjustment_To_Balance_Previous_Description
                ,As_Selection.Adjustments_To_Previous_Balance
                ,As_Selection.Adjustment_Type


                FROM( 


                    Select [Billing Account Number], [Period End Date], [Wireless Number], [User Name]' + @cols +'
                        From 
                            (
                                Select 
                                     [Amount] as SumOfAmount,[Billing Account Number], [Period End Date],  [Wireless Number], [User Name]
                                     From [dbo].[Import_tbl_RDO]
                                     Where [dbo].[Import_tbl_RDO].[Section ID] = ''IRA'' AND [dbo].[Import_tbl_RDO].[Section_1] IS Not Null
                                     ) t
                                     PIVOT 
                                     (
                                     SUM(SumOfAmount)
                                        FOR [Section_1] IN(' + @cols + ')
                                        ) p 
                        ) as AS_Selection'
                Execute (@query)

虽然这似乎让我更接近,但我仍然遇到问题。现在唯一的错误是告诉我列User Name不存在。我知道列存在,所以我认为这只是由其他东西抛出的错误,可能是'+@cols+'。有什么想法吗?

编辑:

我已经打印了@query和@cols的值。 @query的结果与我几分钟前发布的编辑查询相同,除了print没有显示Declare或print语句。 @cols Print看起来像这样:

[Adjustment To Balance Previous Description], [Adjustments To Previous Balance], [Adjustment Type]

2 个答案:

答案 0 :(得分:2)

你离开了。子查询不能是多语句批处理。

您需要按此顺序执行操作:

  1. 设置@Cols,它可以保持原样。
  2. 设置@query,并在其中包含整个查询,包括外部选择,甚至插入。
  3. 执行@query。

答案 1 :(得分:0)

在看到@cols的值之后,在[user name]列之后,在与@cols变量串联之前,似乎缺少一个昏迷(,)

... Set @query = ' Select [Billing Account Number], [Period End Date], [Wireless Number], [User Name], ' + @cols +'
From ...