使用VBA复制sql语句

时间:2018-07-06 11:21:08

标签: vba excel-vba tsql excel

我正在尝试将复制的ID放在sql语句的括号之间-最终目标是在剪贴板中具有ID的完整sql语句。 我目前的方法是:

  

1)循环遍历id的最后一行,并为每个id添加“,”,除非它是最后一个。

     
    

2)将带有“,”的格式化ID移到“ SQL格式”表并将结果复制到剪贴板

  

注意:我真的不希望将Id粘贴到表格上,如果可以某种方式跳过它,那将是很棒的,但不是必须的。

如何/在哪里存储Sql语句,并告诉宏经过括号之间复制的ID,然后复制整个更新的语句?

VBA:

Sub QueryFormat()
  Dim Lastrow As Long, i As Long
    Dim ws As Worksheet
    Dim OracleQuery As String

    Set ws = Sheets("File") 'from here I copy the Id's
    Set sq = Sheets("Sql format") 'past here the Id's with ","
    Set fl = Sheets("main") ' here is my button for macro


    Lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row

    With ws
        For i = 2 To Lastrow
            If Len(Trim(.Range("A" & i).Value)) <> 0 Then _
            sq.Range("A" & i).Formula = .Range("A" & i) & ","
        If i = Lastrow Then _
        sq.Range("A" & i).Formula = .Range("A" & i)
        Next i
        sq.Range("A1:A" & i).Copy
      'Copy to clipboard the required format for the SQL query
    End With

SQL语句:

SELECT x.PERSON_ID,
       x.FULL_NAME,
       x.CURRENT_EMPLOYEE_FLAG,
       (SELECT xxx.full_name
          FROM per_people_x xxx
         WHERE xxx.PERSON_ID = xx.SUPERVISOR_ID) SUPERVISOR,
       (SELECT ppos.actual_termination_date
          FROM per_periods_of_service ppos
         WHERE ppos.period_of_service_id = xx.period_of_service_id
           AND ppos.actual_termination_date < trunc(SYSDATE)) Termination_Date,
       (SELECT trunc(ppos.last_update_date)
          FROM per_periods_of_service ppos
         WHERE ppos.period_of_service_id = xx.period_of_service_id
           AND ppos.actual_termination_date < trunc(SYSDATE)) Last_Update_Date
  FROM per_people_x x, per_all_assignments_f xx
WHERE x.PERSON_ID IN ()
   AND x.PERSON_ID = xx.PERSON_ID
   AND xx.PRIMARY_FLAG = 'Y'
   AND xx.ASSIGNMENT_TYPE = 'E'
   AND xx.effective_start_date =
       (SELECT MAX(f.effective_start_date)
          FROM per_all_assignments_f f
         WHERE f.person_id = xx.person_id
           AND f.primary_flag = 'Y'
           AND f.assignment_type = 'E')

该ID必须放在方括号之间:

  

x.PERSON_ID IN()

关于, 斯拉瓦

2 个答案:

答案 0 :(得分:0)

将ID存储在表格中将使引用它们变得容易

  Range("Table1[ID]")

enter image description here

enter image description here

然后您可以使用VBA.Join()WorksheetFunction.TextJoin()来包装ID。

  Chr(34) & Join(WorksheetFunction.Transpose( Range("Table1[ID]") ), Chr(34) & "," & Chr(34)) & Chr(34)

  Chr(34) & WorksheetFunction.TextJoin( Chr(34) & "," & Chr(34),True, Range("Table1[ID]")) & Chr(34)

答案 1 :(得分:0)

在不断流血试图解决这个问题之后,我终于找到了解决方案: 1)从Sheet1复制+过去的ID到(Sheet2,(A2)),在最后一行之外的每一行添加“,”(这样SQL查询将起作用); 2)存储在Sheet2(A1)中的查询的第一部分-必须是一行,没有空格,并且为文本格式 3)用Id查找最后一行,并超过SQL查询的第二部分; 4)将Sheet2 A1:A(Lastrow)中的范围复制到剪贴板以准备使用。

    Sub PlSqlQueryFormat()

        Dim Lastrow As Long, i As Long
        Dim ws As Worksheet
        Dim OracleQuery As String


        Set ws = Sheets("File") 'Source with plain Id's
        Set sq = Sheets("Sql format") 'Destination to past the Adjusted Id's
        Set fl = Sheets("main") 'My main UI with buttons & Instructions


        Lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
     Application.ScreenUpdating = False
        With ws
            For i = 2 To Lastrow 'It starts with 2 because I don't want to start from header in A1
                If Len(Trim(.Range("A" & i).Value)) <> 0 Then _
                sq.Range("A" & i).Formula = .Range("A" & i) & "," 'If there is Value AKA ID, add ","
            If i = Lastrow Then _
            sq.Range("A" & i).Formula = .Range("A" & i) 'If the ID is the last raw DONT add "," - just copy as is
            Next i

          'Copy to clipboard the required format for the SQL query
          Dim Ne As Long
       Ne = sq.Cells(Rows.Count, "A").End(xlUp).Row + 1 'searching for last row to past the second part of the query after the last ID
       sq.Cells(Ne, "A").Value = sq.Range("B1").Value ' B1=Second part of the query

        sq.Range("A1:A" & i).Copy ' copy the whole range with the Query and the Id's inside the query

        End With
        MsgBox "PlSql Query was successfully copied "

      '  fl.Range("D7") = "Done" 'Message text in cel AFTER: 1)Format is made & 2)Copied to clipboard
         Application.ScreenUpdating = True
End Sub

希望它将对以后完成类似任务的人有所帮助。

关于, 斯拉瓦