Sql server SQL查询在SQLServerManagementStudio中运行良好,但当我从VBA运行它时,它返回空白

Sql server SQL查询在SQLServerManagementStudio中运行良好,但当我从VBA运行它时,它返回空白,sql-server,vba,ssms,Sql Server,Vba,Ssms,这是代码。是否存在无法从Excel运行的内容?我已经在Excel中测试了SQL代码,将文本输出到单元格,复制到SQLServerManagementStudio并运行,效果良好 提前谢谢 将@GroupNumber声明为varchar5 将@GroupName声明为varcharmax 将@ValuationDate声明为日期 将@GroupStartDate声明为日期 将@GroupAnniversayDate声明为日期 将@ValuationYRMO声明为varchar6 将@PlanCod

这是代码。是否存在无法从Excel运行的内容?我已经在Excel中测试了SQL代码,将文本输出到单元格,复制到SQLServerManagementStudio并运行,效果良好

提前谢谢

将@GroupNumber声明为varchar5 将@GroupName声明为varcharmax 将@ValuationDate声明为日期 将@GroupStartDate声明为日期 将@GroupAnniversayDate声明为日期 将@ValuationYRMO声明为varchar6 将@PlanCode声明为varchar6 -**************************在这里换车********* 设置@GroupNumber='01142' 设置@ValuationDate='08-31-2019' -************************************************** 设置@ValuationYRMO=convertvarchar6,year@ValuationDate*100 + month@ValuationDate -******************删除临时表 如果对象_ID'tempdb..Census'不为空 下表普查 如果对象_ID'tempdb..RateTablePrepStep1'不为空 删除表RateTablePrep步骤1 如果对象_ID'tempdb..RateTablePrepStep2'不为空 删除表RateTablePrep2 如果对象_ID'tempdb..RateTable'不为空 下降表比率表 如果对象_ID'tempdb..MembershipData'不为NULL 删除表成员身份数据 如果对象_ID'tempdb..Membership'不为NULL 下拉表成员资格 如果对象_ID'tempdb..ClaimData'不为空 删除表ClaimData 如果对象_ID'tempdb..Claims'不为空 放弃表格索赔 如果对象_ID'tempdb..Completion'不为空 删除表完成 如果对象_ID'tempdb..ValuationDates'不为空 删除表赋值日期 如果对象_ID'tempdb..PaymentStatus'不为空 drop table PaymentStatus 如果对象_ID'tempdb..InvoiceData'不为空 删除表InvoiceData 如果对象_ID'tempdb..AgentGroup'不为NULL 删除表代理组 如果对象_ID'tempdb..GroupEarnedPremium'不为空 drop table GroupEarnedPremium 如果对象_ID'tempdb..EarnedPremium'不为空 下降表收益溢价 如果对象_ID'tempdb..PaymentFrequency'不为空 投递表支付频率 如果对象_ID'tempdb..InvoiceWOTax'不为空 删除表发票税 -******************开始日期和周年日期 Set@GroupStartDate=选择前1个a.FirstEffectiveDate 自[精算师].[当前月].[PolicyAdminPolicy]a 左键加入a.PolicyGroupId=b.PolicyGroupId上的[精算].[CurrentMonth].[PolicyAdminPolicyGroup]b 其中b.GroupNumber=@GroupNumber Set@GroupAnniversayDate=选择前1名a.RenewalDate 自[精算师].[当前月].[PolicyAdminPolicy]a 左键加入a.PolicyGroupId=b.PolicyGroupId上的[精算].[CurrentMonth].[PolicyAdminPolicyGroup]b 其中b.GroupNumber=@GroupNumber -******************普查 选择不同的b.GroupNumber, 如果b.[Description]为null,则pl.[name]否则b.[Description]以[Group name]结尾, ps.[name]作为[组状态], 抄送【名称】为【集团国家】, b1.作为子公司的集团编号, a2.PolicyId作为[证书], ps2.[名称]作为[证书状态], me.MemberId, con.FirstName作为[名字], con.LastName为[姓氏], mt[名称]作为[成员类型], ms.[姓名]作为[会员身份], con.出生日期为[出生日期], @ValuationDate>=dateaddyear时的情况,year@ValuationDate-出生年月日,出生年月日 然后year@ValuationDate-出生年月日 其他的year@ValuationDate-年份Con.出生日期-1以年龄结束, ge。[姓名]作为性别, pl.[代码], pl.[Name]作为[计划名称], 采购订单【名称】为【计划选项】 进入普查 自[精算师].[当前月].[PolicyAdminPolicy]a left join[精算师].CurrentMonth[PolicyAdminPolicyCountry]个人计算机位于a.PolicyId=pc.PolicyId pc.CountryId=cc.CountryId和pc.EndDate上的左联合[精算].[CurrentMonth].[GenericCountry]cc为空 在a.StatusId=ps.StatusId上左键联接[精算].[CurrentMonth].[GenericStatus]ps 左键加入a.PolicyGroupId=b.PolicyGroupId上的[精算].[CurrentMonth].[PolicyAdminPolicyGroup]b 在a.policyid=a1.ParentPolicyId上左加入[精算].[CurrentMonth].[PolicyAdminPolicy]a1 左侧加入a1上的[精算].[CurrentMonth].[PolicyAdminPolicyGroup]b1。PolicyGroupId=b1.PolicyGroupId 左加入[精算].[CurrentMonth].[PolicyAdminPolicy]a 2在a1.policyid=a2.ParentPolicyId上 在a2上左键加入[精算].[CurrentMonth].[GenericStatus]ps2.StatusId=ps2.StatusId 左键加入a2上的[精算].[CurrentMonth].[PolicyAdminPolicyQualification]pe。policyid=pe.policyid 在pe.PlanId=pl.PlanId上左加入[精算].[CurrentMonth].[ProductConfigPlan]pl left join[精算].[CurrentMonth].[PolicyAdminMemberQualification]pe上的me.PolicyEligibilityId=me.PolicyEligibilityId 左键加入[精算].[CurrentMonth].[GenericStatus]ms on me.StatusId=ms.StatusId 左键加入[精算].[CurrentMonth].[ProductConfigPlanOption]我的采购订单。PlanOptionId=po.PlanOptionId 左加入[精算].[CurrentMonth].[ProductConfigMemberType]mt在我身上。MemberTypeId=mt.MemberTypeId 左键加入[精算].[CurrentMonth].[PolicyAdminMember]我的mem.MemberId=mem.MemberId 左加入[精算].[CurrentMonth].[ContactMgmtContact]con在mem.ContactId=con.ContactId上 左联合[精算].[CurrentMonth].[GenericGender]ge on con.GenderId=ge.GenderId 其中b.GroupNumber=@GroupNumber和pe.EndDate为null,me.EndDate为null 按b.groupnumber、b1.groupnumber、a2.PolicyId排序 根据【集团编号】、【子公司】、【计划选项】、【证书】从普查订单中选择* 我被困在如何继续下去的问题上。我已经用一个简单的top 10查询测试了我的记录连接,它在VBA中运行良好

以下是我的VBA代码:

    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim rst2 As New ADODB.Recordset
    Dim ConnectionString As String
    Dim StrQueryBase As String
    Dim StrQueryBase2 As String
    Dim StrQueryBase3 As String
    Dim StrQueryGroupDate As String
    Dim StrQueryCensus As String
    Dim StrQueryCensus2 As String
    Dim StrQueryCensus3 As String
    Dim StrQueryTest As String

    ConnectionString = "Provider=SQLOLEDB; Data Source=bddc1didw1;Initial Catalog=Actuarial; Trusted_connection=Yes; Integrated Security='SSPI'"
    'Opens connection to the database
    cnn.Open ConnectionString

    'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
    cnn.CommandTimeout = 1500


    StrQueryBase = "Declare @GroupNumber as varchar(5) " & _
                    "Declare @GroupName as varchar(max) " & _
                    "Declare @ValuationDate as Date " & _
                    "Declare @GroupStartDate as Date " & _
                    "Declare @GroupAnniversayDate as Date " & _
                    "Declare @ValuationYRMO as varchar(6) " & _
                    "Declare @PlanCode as varchar(6) " & _
"Set @GroupNumber = '01142' " & _
"Set @ValuationDate = '08-31-2019'  " & _
"Set @ValuationYRMO = convert(varchar(6),year(@ValuationDate)*100 + month(@ValuationDate)) " & _
"IF OBJECT_ID('tempdb..#Census') IS NOT NULL " & _
            "drop table #Census " & _
"IF OBJECT_ID('tempdb..#RateTablePrepStep1') IS NOT NULL " & _
            "drop table #RateTablePrepStep1 " & _
"IF OBJECT_ID('tempdb..#RateTablePrepStep2') IS NOT NULL " & _
            "drop table #RateTablePrepStep2 " & _
"IF OBJECT_ID('tempdb..#RateTable') IS NOT NULL " & _
            "drop table #RateTable " & _
"IF OBJECT_ID('tempdb..##MembershipData') IS NOT NULL " & _
            "drop table ##MembershipData " & _
"IF OBJECT_ID('tempdb..#Membership') IS NOT NULL " & _
            "drop table #Membership "

    StrQueryBase2 = "IF OBJECT_ID('tempdb..##ClaimData') IS NOT NULL " & _
            "drop table ##ClaimData " & _
"IF OBJECT_ID('tempdb..#Claims') IS NOT NULL " & _
            "drop table #Claims " & _
"IF OBJECT_ID('tempdb..##Completion') IS NOT NULL " & _
            "drop table ##Completion " & _
"IF OBJECT_ID('tempdb..#ValuationDates') IS NOT NULL " & _
            "drop table #ValuationDates " & _
"IF OBJECT_ID('tempdb..#PaymentStatus') IS NOT NULL " & _
            "drop table #PaymentStatus " & _
"IF OBJECT_ID('tempdb..#InvoiceData') IS NOT NULL " & _
            "drop table #InvoiceData " & _
"IF OBJECT_ID('tempdb..#AgentGroup') IS NOT NULL " & _
            "drop table #AgentGroup " & _
"IF OBJECT_ID('tempdb..#GroupEarnedPremium') IS NOT NULL " & _
            "drop table #GroupEarnedPremium " & _
"IF OBJECT_ID('tempdb..#EarnedPremium') IS NOT NULL " & _
            "drop table #EarnedPremium " & _
"IF OBJECT_ID('tempdb..#PaymentFrequency') IS NOT NULL " & _
            "drop table #PaymentFrequency " & _
"IF OBJECT_ID('tempdb..#InvoiceWOTax') IS NOT NULL " & _
            "drop table #InvoiceWOTax " & _
"Set @GroupStartDate = (Select top 1 a.FirstEffectiveDate " & _
                                    "From [Actuarial].[CurrentMonth].[PolicyAdminPolicy] a " & _
                                    "left join [Actuarial].[CurrentMonth].[PolicyAdminPolicyGroup] b on a.PolicyGroupId=b.PolicyGroupId "

StrQueryBase3 = "where b.GroupNumber=@GroupNumber) " & _
"Set @GroupAnniversayDate = (Select top 1  a.RenewalDate " & _
                                    "From [Actuarial].[CurrentMonth].[PolicyAdminPolicy] a " & _
                                    "left join [Actuarial].[CurrentMonth].[PolicyAdminPolicyGroup] b on a.PolicyGroupId=b.PolicyGroupId " & _
                                    "where b.GroupNumber=@GroupNumber) "


   StrQueryCensus = "SELECT distinct b.GroupNumber, " & _
                         "Case when b.[Description] is null then pl.[name] else b.[description] end as [Group Name], " & _
                         "ps.[name] as [Group Status], " & _
                         "cc.[name] as [Group Country], " & _
                         "b1.GroupNumber as Subsidiary, " & _
                         "a2.PolicyId as [Certificate], " & _
                         "ps2.[name] as [Certificate Status], " & _
                         "me.MemberId, " & _
                         "con.FirstName as [First Name], " & _
                         "con.LastName as [Last Name], " & _
                         "mt.[name] as [Member Type], " & _
                         "ms.[Name] as [Member Status], " & _
                         "con.DateOfBirth as [Date of Birth], " & _
                         "Case when @ValuationDate > = dateadd(year,year(@ValuationDate)-year(con.DateOfBirth),con.DateOfBirth) " & _
                                          "then year(@ValuationDate)-year(con.DateOfBirth) " & _
                                          "else year(@ValuationDate)-year(con.DateOfBirth) -1 end as Age, " & _
                         "ge.[name] as Gender, " & _
                         "pl.[Code], " & _
                         "pl.[Name] as [Plan Name], " & _
                         "po.[name] as [Plan Option] " & _
  "into #Census " & _
  "FROM [Actuarial].[CurrentMonth].[PolicyAdminPolicy] a " & _
  "left join [Actuarial].CurrentMonth.[PolicyAdminPolicyCountry] pc on a.PolicyId=pc.PolicyId " & _
  "left join [Actuarial].[CurrentMonth].[GenericCountry] cc on pc.CountryId=cc.CountryId and pc.EndDate is null "


   StrQueryCensus2 = "left join [Actuarial].[CurrentMonth].[GenericStatus] ps on a.StatusId=ps.StatusId " & _
  "left join [Actuarial].[CurrentMonth].[PolicyAdminPolicyGroup] b on a.PolicyGroupId=b.PolicyGroupId " & _
  "left join [Actuarial].[CurrentMonth].[PolicyAdminPolicy] a1 on a.policyid=a1.ParentPolicyId " & _
  "left join [Actuarial].[CurrentMonth].[PolicyAdminPolicyGroup] b1 on a1.PolicyGroupId=b1.PolicyGroupId " & _
  "left join [Actuarial].[CurrentMonth].[PolicyAdminPolicy] a2 on a1.policyid=a2.ParentPolicyId " & _
  "left join [Actuarial].[CurrentMonth].[GenericStatus] ps2 on a2.StatusId=ps2.StatusId " & _
  "left join [Actuarial].[CurrentMonth].[PolicyAdminPolicyEligibility] pe on a2.policyid=pe.PolicyId " & _
  "left join [Actuarial].[CurrentMonth].[ProductConfigPlan] pl on pe.PlanId=pl.PlanId " & _
  "left join [Actuarial].[CurrentMonth].[PolicyAdminMemberEligibility] me on pe.PolicyEligibilityId=me.PolicyEligibilityId " & _
  "left join [Actuarial].[CurrentMonth].[GenericStatus] ms on me.StatusId=ms.StatusId " & _
  "left join [Actuarial].[CurrentMonth].[ProductConfigPlanOption] po on me.PlanOptionId=po.PlanOptionId " & _
  "left join [Actuarial].[CurrentMonth].[ProductConfigMemberType] mt on me.MemberTypeId=mt.MemberTypeId " & _
  "left join [Actuarial].[CurrentMonth].[PolicyAdminMember] mem on me.MemberId=mem.MemberId " & _
  "left join [Actuarial].[CurrentMonth].[ContactMgmtContact] con on mem.ContactId=con.ContactId " & _
  "left join [Actuarial].[CurrentMonth].[GenericGender] ge on con.GenderId=ge.GenderId " & _
  "where b.GroupNumber=@GroupNumber and pe.EndDate is null and me.EndDate is null " & _
  "order by b.groupnumber,b1.GroupNumber,a2.PolicyId " & _
  "select * from #census order by [GroupNumber],Subsidiary,[Plan Option],[Certificate] "

  StrQueryCensus3 = StrQueryBase & StrQueryBase2 & StrQueryBase3 & StrQueryCensus & StrQueryCensus2 "

'
      ActiveSheet.Range("e1") = StrQueryCensus3




     rst2.Open StrQueryCensus3, cnn

    ActiveSheet.Range("e2").CopyFromRecordset rst2

我应该补充一点,******普查之前的所有部分都是正确的,工作正常,因为我在另一个正在工作的查询中使用了它。如果脚本的前半部分工作正常,那么我就不知道为什么后半部分不工作。您确定Excel正在连接到您在Management Studio中使用的同一个数据库吗?是的,因为我通过从同一数据库执行简单的select top 1查询来测试它,它给了我正确的结果。奇怪。我会把你的脚本缩减到你知道的效果,然后一点一点地重新构建它,看看它在什么时候停止工作。问题是我知道它一直工作到连接,因为我在不同的工作查询中使用了该部分,但是连接和相应的选择是一部分。当直接从stringquery字段的输出复制时,它们都在sql中工作。