6

たくさんのグーグルの後、私はデータベースに接続し、既存の一時テーブルを削除してから新しいものを作成することを望んでいた次のマクロに行き着きました(それを入力して結果を表示します)。

Dim adoCn As ADODB.Connection
Dim adoRs As ADODB.Recordset
Dim adoCm As ADODB.Command
Dim strSQL As String

Set adoCn = New ADODB.Connection
With adoCn
    .ConnectionString = "Provider=SQLOLEDB;" & _
                        "Initial_Catalog=XXX;" & _
                        "Integrated Security=SSPI;" & _
                        "Persist Security Info=True;" & _
                        "Data Source=XXX;" & _
                        "Extended Properties='IMEX=1'"
    .CursorLocation = adUseServer
    .Open
End With

Set adoCm = New ADODB.Command

With adoCm
    Set .ActiveConnection = adoCn
    .CommandType = adCmdText
    .CommandText = "IF OBJECT_ID('tempdb..#AgedProducts') IS NOT NULL DROP TABLE #AgedProducts"
    .Execute
    .CommandText = "CREATE TABLE #AgedProducts " & _
                   "(Source_Order_Number VARCHAR(255)) " & _
                   "INSERT INTO #AgedProducts VALUES ('AB-123-456') " & _
                   "SELECT * FROM #AgedProducts (NOLOCK) "
    .Execute
End With

Set adoRs = New ADODB.Recordset
With adoRs
    Set .ActiveConnection = adoCn
    .LockType = adLockBatchOptimistic
    .CursorLocation = adUseServer
    .CursorType = adOpenForwardOnly
    .Open "SET NOCOUNT ON"
End With
adoRs.Open adoCm

MsgBox "Recordset returned...", vbOKOnly

While Not adoRs.EOF
    Debug.Print adoRs.Fields(0).Value
    adoRs.MoveNext
Wend

adoCn.Close

Set adoCn = Nothing
Set adoRs = Nothing

クエリを実行すると、次のエラーメッセージが表示されます。

Run-time error '-2147217887 (80040e21)':

The requested properties cannot be supported

このNOCOUNT行はhttp://support.microsoft.com/kb/235340からのものです(上記のコードの多くがそうであるように)。IMEX=1注文番号には複数のタイプがある可能性があることを考慮して追加しましたが、そこで問題が発生しているとは思えません。

どんな助けでも大歓迎です!

4

2 に答える 2

6

recodsetを開く方法を変更し、コマンドからrecodsetopenメソッド呼び出しにselectを移動します。

With adoCm
    Set .ActiveConnection = adoCn
    .CommandType = adCmdText
    .CommandText = "IF OBJECT_ID('tempdb..#AgedProducts') IS NOT NULL DROP TABLE #AgedProducts"
    .Execute
    .CommandText = "CREATE TABLE #AgedProducts " & _
                   "(Source_Order_Number VARCHAR(255)) " & _
                   "INSERT INTO #AgedProducts VALUES ('AB-123-456') "
    .Execute
End With

Set adoRs = New ADODB.Recordset
With adoRs
    Set .ActiveConnection = adoCn
    .LockType = adLockBatchOptimistic
    .CursorLocation = adUseServer
    .CursorType = adOpenForwardOnly

End With
adoRs.Open "SELECT * FROM #AgedProducts (NOLOCK)"
于 2013-02-18T16:17:35.940 に答える
0

一時テーブルについての私の理解は、それらを作成した接続でのみ使用可能であるということです。その場合、別の接続から1つをドロップしようとするのは賢明ではありません。

エラーメッセージには、コードのどの行が原因であるかは示されていません。その場合は、コードを少しずつテストすることをお勧めします。接続を作成することから始めます。次に、それを開閉します。次に、接続が開いている間に作業を開始しますが、一度に1つのことを行います。

于 2013-02-18T15:40:14.880 に答える