Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询在转换为VBA代码时不起作用_Sql_Sql Server_Vba - Fatal编程技术网

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

SQL查询在转换为VBA代码时不起作用,sql,sql-server,vba,Sql,Sql Server,Vba,我正在尝试将SQL查询转换为VBA代码,转换后,当我在excel中运行宏时,它不会显示任何结果。我刚刚对VBA代码做了一些基本更改。任何人都能给我提供见解。我正在使用SQL server。提前谢谢 objMyCmd.CommandText = "SELECT c1.[RDT_FileID], C1.[Master Policy Number], c1.[Work item /Submission no#],c1.[Insured Name], c1.[Credited Office]," &a

我正在尝试将SQL查询转换为VBA代码,转换后,当我在excel中运行宏时,它不会显示任何结果。我刚刚对VBA代码做了一些基本更改。任何人都能给我提供见解。我正在使用SQL server。提前谢谢

 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

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

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]

注意:日期筛选的设计非常糟糕,如果数据库中有datetime字段,我建议您使用CAST[生效日期]as date>='20160101'


HTH

哇,这很难理解。在vba中使用一个更简单的例子,比如从表中选择*并从表中构造它,怎么样。只是为了验证底层调用机制是否正常工作这是什么后端DBMS?微软接入?SQL Server?并考虑在后端保存这样的查询作为视图。与脚本字符串查询相比,存储查询经过优化、缓存以获得最佳计划。您的意思是像调用存储过程一样?顺便说一句,它们不是完全相同的查询。查看WHERE子句日期筛选器。您在记录a的精算师.dbo.View\u Property\u Rater\u中的别名错误。c1从未定义过。