在Excel VBA中连接表时遇到麻烦

时间:2014-03-26 18:41:11

标签: sql excel-vba vba excel

我正在尝试使用水晶报告sql并对其进行转换,以便我可以在excel中使用它。我在Excel中加入语句的语法遇到了麻烦。我可以单独做几张桌子,但不能将它们结合起来。

来自水晶报道:我想要复制的内容:

SELECT "Material_Req"."Job", "Material_Req"."Pick_Buy_Indicator", "Material_Req"."Material", "Material_Req"."Description", "Material_Req"."Vendor_Reference", "PO_Detail"."PO", "Material_Req"."Est_Qty", "Source"."Act_Qty", "PO_Header"."Vendor", "PO_Detail"."Due_Date"
 FROM   (("PRODUCTION"."dbo"."Material_Req" "Material_Req" LEFT OUTER JOIN "PRODUCTION"."dbo"."Source" "Source" ON "Material_Req"."Material_Req"="Source"."Material_Req") LEFT OUTER JOIN "PRODUCTION"."dbo"."PO_Detail" "PO_Detail" ON "Source"."PO_Detail"="PO_Detail"."PO_Detail") LEFT OUTER JOIN "PRODUCTION"."dbo"."PO_Header" "PO_Header" ON "PO_Detail"."PO"="PO_Header"."PO"
 WHERE  "Material_Req"."Pick_Buy_Indicator"='b' AND "Material_Req"."Est_Qty">"Source"."Act_Qty"
 ORDER BY "Material_Req"."Job"

上一个有效:

sqlMatlReq = "select job.job,  material_req.vendor_reference, material_req.Material,'' As Description, material_req.Vendor," _
        & "(material_req.est_qty) As Qty, (material_req.act_qty) As Qty,   " _
        & "material_req.due_date, " _
        & " material_req.status " _
        & "from (material_req inner join job on material_req.job=job.job) " _
        & "left join material on material_req.material=material.material " _
        & "where material.material is not null " _
        & "and job.part_number is not null " _
        & "and Job.Status in('Active') " _
        & "and material_req.act_qty in('0') " _
        & "Union all " _
        & "select job.job, material_req.vendor_reference, material_req.Material, material_req.description, material_req.Vendor," _
        & "(material_req.est_qty), (material_req.act_qty) As Qty," _
        & " material_req.due_date, material_req.status " _
        & "from (material_req inner join job on material_req.job=job.job) " _
        & "left join material on material_req.material=material.material " _
        & "where material.material is null " _
        & "and job.part_number is not null " _
        & "and Job.Status in('Active') " _
        & "and material_req.act_qty in('0') " _
        & "order by job.job;"

据我所知:结合三个

有麻烦
'sqlMatlReq = "select job.job, material_req.material, material_req.Vendor_Reference, material_req.description, material_req.Est_Qty  " _
      '& "from (material_req inner join job on material_req.job=job.job) " _

'sqlMatlReq = "select source.Act_Qty, PO_Detail.PO, PO_Detail.Due_Date " _
      '& "from source left outer join PO_Detail on source.Act_qty=PO_Detail.PO_Detail "

'sqlMatlReq = "select PO_Header.vendor " _
      '& "from PO_Header"

提前致谢。

1 个答案:

答案 0 :(得分:0)

用于Crystal Reports的SQL Server存储过程

 CREATE PROCEDURE sp_SampleProcedure
    AS

 BEGIN 
 SET NOCOUNT ON;

 SELECT table_catalog, table_schema, table_name, table_type
   FROM tbldm.information_schema.tables 
  WHERE table_schema = 'dbo'
    AND table_type = 'BASE TABLE';

 END;
  

如果要查看向Crystal Reports客户端提供数据的任何查询,应使用SQL Server STORED过程中发出的最后一个SELECT语句表示。此示例指向表名和类型的SQL数据字典。

  1. 在Crystal Reports客户端和SQL数据库服务器之间定义DSN连接。在此之前可能已经设计了其他一份报告。

  2. 在选择要使用的数据库对象时,存储过程应显示在其架构目录中,以及Crystal报表编写器通常找到的其他对象。

  3. 注意,程序可以参数化,并且在整个过程中使用它们的输入变量。