转换为VBA代码时,SQL查询不起作用

时间:2017-01-10 14:24:26

标签: sql sql-server vba

我正在尝试将我的SQL查询转换为VBA代码,转换后,当我在excel中运行我的宏时,它没有显示任何结果。我刚刚对VBA代码做了一些基本的改动。任何人都可以为我提供见解。我正在使用SQL服务器。提前致谢 !

 objMyCmd.CommandText = "SELECT c1.[RDT_FileID], C1.[Master Policy Number], c1.[Work item /Submission no#],c1.[Insured Name], c1.[Credited Office]," & _
                                " c1.[Credited Underwriter], c1.[Product Line], c1.[Product Line Subtype], c1.[Current Status], c1.[Effective Date], c1.[Expiry Date], c1.[Original Currency],  c1.[Premium in Local Currency] " & _
                            " FROM  Actuarial.dbo.View_Property_Rater_Of_Record a " & _
                                " left join " & _
                                "( SELECT b.[Current Status], a.* FROM" & _
                                "( SELECT [Master Policy Number],SUM(CAST([Premium in Local Currency] AS numeric)) AS SumPremium, MAX([Work item /Submission no#]) AS MaxSubmissionNumber" & _
                                " FROM  IT.dbo.View_Property_Rater_Of_Record " & _
                                " WHERE [Master Policy Number] IS NOT NULL AND [Master Policy Number] <> ''" & _
                                " Group by [Master Policy Number] ) a" & _
                                " INNER JOIN IT.dbo.View_Property_Rater_Of_Record b on a.[MaxSubmissionNumber] = b.[Work item /Submission no#]" & _
                                " WHERE [Current Status] = 'Cancellation' and SumPremium = 0 " & _
                                "  ) c2 on c1.[Master Policy Number] = c2.[Master Policy Number]" & _
                                " WHERE c2.[Master Policy Number] Is Null " & _
                                " AND c1.[RDT_FileID] is null " & _
                                " AND c1.[Product Line Subtype] <>  '0102-Marine' " & _
                                " AND c1.[Master Policy Number] NOT LIKE '___PRI__________'" & _
                                " AND c1.[Master Policy Number] NOT LIKE '___BLA__________'" & _
                                " AND c1.[Effective Date] >= '2014-04-01' " & _
                                " AND c1.[Effective Date] >= " & PED(0) - 2 & " and c1.[Effective Date] <= " & PED(1) - 2 & " " & _
                                " AND c1.[Current Status] ='Bound' " & _
                                " ORDER BY c1.[Effective Date] ASC"

我的SQL查询如下: -

            select 
                   c1.[RDT_FileID],
                   C1.[Master Policy Number],
                   c1.[Work item /Submission no#],
                   c1.[Insured Name],
                   c1.[Credited Office],
                   c1.[Product Line Subtype],
                   c1.[Effective Date],
                   c1.[Current Status], 
                   c1.[Premium in Local Currency]
            from IT.dbo.View_Property_Rater_Of_Record c1
            left join
                   (
                   select 
                          b.[Current Status],
                          a.*
                   from 
                   (
                   select
                          [Master Policy Number],
                          sum(cast([Premium in Local Currency] as numeric)) as SumPremium,
                          max([Work item /Submission no#]) as MaxSubmissionNumber
                   from Actuarial.dbo.View_Property_Rater_Of_Record
                   where [Master Policy Number] is not null and [Master Policy Number] <> ''
                   group by
                          [Master Policy Number]
                          ) a
                   inner join IT.dbo.View_Property_Rater_Of_Record b on a.[MaxSubmissionNumber] = b.[Work item /Submission no#]
                   where [Current Status] = 'Cancellation' and SumPremium = 0
                   ) c2 on c1.[Master Policy Number] = c2.[Master Policy Number]
            where 
                   c1.[Effective Date] >= '2016-01-01' 
                   and c1.[Effective Date] <= '2017-01-01'
                   and C1.[Current Status] = 'Bound'
                   and c1.[Credited Office]= '002 - New York'
                   and c1.[Product Line Subtype] <> '0102-Marine'
                   and c1.[Master Policy Number] NOT LIKE  '___PRI__________'
                   and c1.[Master Policy Number] NOT LIKE  '___BLA__________'
                   and c1.[RDT_FileID] is null
                   and c2.[Master Policy Number] is null

1 个答案:

答案 0 :(得分:1)

您的代码有许多不同之处和注意事项,其中一些在此处: 我认为您需要更改您的查询字符串,如下所示:

objMyCmd.CommandText = "" & _ 
    "SELECT" & _
        " c1.[RDT_FileID]," & _
        " c1.[Master Policy Number]," & _                'Edit C1 to c1 => in newest version of SQL Server case-sensitivity is important
        " c1.[Work item /Submission no#]," & _
        " c1.[Insured Name]," & _
        " c1.[Credited Office]," & _
        " c1.[Credited Underwriter]," & _ 
        " c1.[Product Line]," & _ 
        " c1.[Product Line Subtype]," & _ 
        " c1.[Current Status]," & _ 
        " c1.[Effective Date]," & _ 
        " c1.[Expiry Date]," & _ 
        " c1.[Original Currency]," & _ 
        " c1.[Premium in Local Currency] " & _
    " FROM IT.dbo.View_Property_Rater_Of_Record c1 " & _ 'Edit `Actuarial.dbo.View_Property_Rater_Of_Record a` to `IT.dbo.View_Property_Rater_Of_Record c1`
        " left join (" & _ 
            " SELECT " & _ 
                 " b.[Current Status]," & _ 
                 " a.*" & _ 
            " FROM (" & _ 
                " SELECT " & _ 
                    " [Master Policy Number]," & _ 
                    " SUM(CAST([Premium in Local Currency] AS numeric)) AS SumPremium," & _ 
                    " MAX([Work item /Submission no#]) AS MaxSubmissionNumber" & _
                " FROM Actuarial.dbo.View_Property_Rater_Of_Record " & _  'Edit IT.dbo.View_Property_Rater_Of_Record to Actuarial.dbo.View_Property_Rater_Of_Record
                " WHERE [Master Policy Number] IS NOT NULL AND [Master Policy Number] <> ''" & _
                " Group by" & _ 
                    " [Master Policy Number] ) a" & _
            " INNER JOIN IT.dbo.View_Property_Rater_Of_Record b on a.[MaxSubmissionNumber] = b.[Work item /Submission no#]" & _
            " WHERE [Current Status] = 'Cancellation' and SumPremium = 0 " & _
            " ) c2 on c1.[Master Policy Number] = c2.[Master Policy Number]" & _
    " WHERE " & _ 
        " c2.[Master Policy Number] Is Null " & _
        " AND c1.[RDT_FileID] is null " & _
        " AND c1.[Product Line Subtype] <>  '0102-Marine' " & _
        " AND c1.[Master Policy Number] NOT LIKE '___PRI__________'" & _
        " AND c1.[Master Policy Number] NOT LIKE '___BLA__________'" & _
        " AND c1.[Effective Date] >= '2014-04-01' " & _         'I think you should remove this line
        " AND c1.[Effective Date] >= '" & PED(0) - 2 $ "' " & _   'Add `'` around a string input
        " and c1.[Effective Date] <= '" & PED(1) - 2 & "' " & _   'Add `'` around a string input
        " AND c1.[Current Status] ='Bound' " & _
        " AND c1.[Credited Office]= '002 - New York' " & _        'Add this missing criteria
    " ORDER BY c1.[Effective Date] ASC"
  

注意:您可以使用COALESCE([Master Policy Number], '') <> ''代替[Master Policy Number] IS NOT NULL AND [Master Policy Number] <> ''

  

注意:您的日期过滤设计非常糟糕,如果您的数据库中有datetime字段,我建议您使用CAST([Effective Date] as date) >= '20160101'

HTH;)