如何使excel宏连接到SQL

如何使excel宏连接到SQL,sql,vba,Sql,Vba,我试图将SQL数据带到Excel电子表格中 有人帮我吗? 这里是信息 我在其他服务器上找到了这个vba宏。 那么,如何将这些数据用于我的服务器? 或者有什么想法 SERVER:LBOSS\BACKOFFICE ID:sa DB NAME:LogiDB PASSWORD: Table RPT_SUB,RPT_FIN F04,F254,F1056,F11057.... 在子查询中查找。。。。有一行以ConnectionString=..开头。。。。 将其更改为: Sub star

我试图将SQL数据带到Excel电子表格中

有人帮我吗? 这里是信息

我在其他服务器上找到了这个vba宏。 那么,如何将这些数据用于我的服务器? 或者有什么想法

SERVER:LBOSS\BACKOFFICE ID:sa DB NAME:LogiDB PASSWORD: Table RPT_SUB,RPT_FIN F04,F254,F1056,F11057....
在子查询中查找。。。。有一行以ConnectionString=..开头。。。。 将其更改为:

Sub start()

'Call init
Dim strQuery As String
Dim strStartDate As String

strStartDate = "'" & Cells(2, 4).Value & "'"

' No Category Report
Call Query("select report_daily_category(" & strStartDate & ", null)", _
Cells(5, 4))
' No Category GST
Call Query("SELECT report_daily_tax(" & strStartDate & ",1,null)", _
Cells(5, 7))
' Category Description
Call Query("SELECT description FROM category where not obsolete ORDER BY list_priority", _
Cells(6, 3))
' Category Report
Call Query("SELECT report_daily_category(" & strStartDate & ",category_id) FROM category where not obsolete ORDER BY list_priority", _
Cells(6, 4))
' Category GST Report
Call Query("SELECT report_daily_tax(" & strStartDate & ",1,category_id) from category where not obsolete ORDER BY list_priority", _
Cells(6, 7))
' Lotto Report
'Call Query("SELECT report_daily_lotto(" & strStartDate & ", true)", _
Cells(14, 7))
'Call Query("SELECT report_daily_lotto(" & strStartDate & ", false)", _
Cells(16, 7))
' Tax Description
Call Query("SELECT description FROM tax_rule where not disabled ORDER BY tax_id", _
Cells(22, 3))
' Tax Report
Call Query("SELECT report_daily_tax(" & strStartDate & ",tax_id) FROM tax_rule where not disabled ORDER BY tax_id", _
Cells(22, 4))
' Vendor Coupon
Call Query("select report_daily_coupon(" & strStartDate & ", null, null)", _
Cells(30, 4))
' Discount
Call Query("select report_daily_discount(" & strStartDate & ", true)", _
Cells(32, 4))
' Reward Point + Store Credit
Call Query("select report_daily_storecredit(" & strStartDate & ")", _
Cells(33, 4))
' Refund
Call Query("select report_daily_refund(" & strStartDate & ", true)", _
Cells(35, 4))
' Refund GST; Included in <Tax Report>
Call Query("SELECT report_daily_refund(" & strStartDate & ", null)", _
Cells(37, 4))
' Customer Count
Call Query("select count(*) from receipt where time >= " & strStartDate & " and time < cast(" & strStartDate & " as timestamp) + interval '1 day'", _
Cells(42, 4))
' Payment Description
Call Query("SELECT payment FROM payment_method ORDER BY list_priority", _
Cells(46, 3))
' Payment Report
Call Query("SELECT report_daily_payment(" & strStartDate & ",paymethod_id, null) FROM payment_method ORDER BY list_priority", _
Cells(46, 4))

' ROA
Call Query("SELECT report_daily_roa(" & strStartDate & ")", _
Cells(41, 12))

Call cleanup

End Sub
Sub init()
End Sub

Sub cleanup()
    Cells.Select
    With Selection
        .VerticalAlignment = xlCenter
        .HorizontalAlignment = xlRight
        .Font.Name = "Courier New"
        .Font.Size = 9
    End With
    For Each obj In ActiveSheet.QueryTables
        obj.Delete
    Next

End Sub
' Array("ODBC;DSN=PostgreSQL30;DATABASE=pos1;SERVER=192.168.1.200;PORT=5432;UID=postgres;PWD=nopass;ReadOnly=0;FakeOidIndex=0;Show")
Sub Query(strQuery As Variant, Pos As Variant)
    Dim ConnectionString As Variant
    ConnectionString = Array( _
        Array("ODBC;DSN=PostgreSQL30;DATABASE=pos1u;SERVER=192.168.1.250;PORT=5432;UID=postgres;PWD=nopass;ReadOnly=0;FakeOidIndex=0;ShowOidC"), _
        Array("OidColumn=0;RowVersioning=0;ShowSystemTables=0;ConnSettings=;Fetch=100;Socket=4096;UnknownSizes=0;MaxVarcharSize=254;MaxLongVar"), _
        Array("charSize=8190;Debug=0;CommLog=0;Optimizer=1;Ksqo=1;UseDeclareFetch=0;TextAsLongVarchar=1;UnknownsAsLongVarchar=0;BoolsAsChar=1;"), _
        Array("Parse=0;CancelAsFreeStmt=0;ExtraSysTablePrefixes=dd_;;LFConversion=1;UpdatableCursors=1;DisallowPremature=0;TrueIsMinus1=0") _
    )
    With ActiveSheet.QueryTables.Add(Connection:=ConnectionString, Destination:=Pos)
        .CommandText = Array(strQuery)
        .Name = "Query"
        .FieldNames = False
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With

End Sub
在PWD=之后输入您的密码,并在Start子部分编辑SQL查询。基本上应该可以

ConnectionString = "ODBC;DATABASE=LogiDB;SERVER=LBOSS\BACKOFFICE;UID=sa;PWD=password"