-
Esportare dati da sql server a excel
Salve a tutti ragazzi,
mi sono appena registrato, per sottoporvi questo problema.
Avrei bisogno di esportare una grande quantità di dati da SQL Server ad excel (diciamo nell'ordine dei 100000 record); inizialmente lho fatto usando excel.application, ma ho visto che il passaggio per i componenti ADO e per i componenti Excel portava ad eseguire il tutto in un tempo di circa 8 minuti... tempo piuttosto grande per un "utonto" che va facendo click in giro...
Per sveltire la procedura, ho trovato girando un po' in rete, una StoredProcedure, che è in grado di fare questo direttamente da SQL Server... non mi rimane quindi che usare i componenti ado per richiamarla.
Mi sono scritto dunque, questa funzione, per richiamarla:
Public Function SPExport(ByVal Query As String, ByVal DestFilePath As String, ByVal SheetName As String, ByVal SheetIndex As Integer, Optional ByVal Range As String) As Long
Dim Cmd As ADODB.Command
Set Cmd = New ADODB.Command
Dim Res As String
With Cmd
.ActiveConnection = Cn
.CommandType = adCmdStoredProc
.CommandText = "CS_SP_DMOExportToExcel"
.CommandTimeout = 0
End With
Cmd.Parameters.Append Cmd.CreateParameter("@SourceServer", adVarChar, adParamInput, 30, "(local)")
Cmd.Parameters.Append Cmd.CreateParameter("@SourceID", adVarChar, adParamInput, 30, "null")
Cmd.Parameters.Append Cmd.CreateParameter("SourcePWD", adVarChar, adParamInput, 30, "null")
Cmd.Parameters.Append Cmd.CreateParameter("QueryText", adVarChar, adParamInput, 8000, Query)
Cmd.Parameters.Append Cmd.CreateParameter("FileName", adVarChar, adParamInput, 256, DestFilePath)
Cmd.Parameters.Append Cmd.CreateParameter("WorkSheetName", adVarChar, adParamInput, 100, SheetName)
Cmd.Parameters.Append Cmd.CreateParameter("WorkSheetIndex", adInteger, adParamInput, , SheetIndex)
Cmd.Parameters.Append Cmd.CreateParameter("RangeName", adVarChar, adParamInput, 80, "null")
Cmd.Parameters.Append Cmd.CreateParameter("RowsAffected", adVarChar, adParamOutput, 512)
Cmd.Execute
If Cmd("RowsAffected") = "0" Or Cmd("rowsaffected") = "" Then
Res = ""
Else
Res = Cmd("RowsAffected")
End If
SPExport = Val(Res)
TerminateEXE "Excel.exe"
Set Cmd = Nothing
End Function
Dove TerminateEXE è una funzioncina che sfrutta delle API per terminare il processo di Excel nel caso rimanga aperto.
Ora il punto è... se eseguo questa funzione, mi da errore... se eseguo la SP da SQL Server funziona. Sono andato a vedere quindi, prima dell'esecuzione dell'oggetto command, qual'è il suo testo, ed ottengo la chiamata a funzione con tutti ? al posto dei parametri.
In che cosa sbaglio? Sapete darmi un'indicazione? Ah, dimenticavo di dire, ma forse qualcuno di voi l'ha già capito, che lavoro in vb6.0
Grazie mille a tutti x l'attenzione, ogni suggerimento è ben accolto.
Permessi di Scrittura
- Tu non puoi inviare nuove discussioni
- Tu non puoi inviare risposte
- Tu non puoi inviare allegati
- Tu non puoi modificare i tuoi messaggi
-
Regole del Forum
Segnalibri