SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


export query result to excel file


export query result to excel file

Author
Message
arias_melvin
arias_melvin
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 7
how can i export the result of my query below to excel file using
ms sql script?

select empno, name, premium from application

could anyone provide me a script

thanks.
Heinrich Braun
Heinrich Braun
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 1111
First possible method, if you need to export data to excel only now and then:
Execute the query in MS SQL Management Studio, right klick on the result grid, save data as csv-file, import this file into your excelsheet

Second possibility: using MS-Query, an option included in MS-Office suite, must be installed separatedly.
First create ODBC-connection. Open excel, select Data->import external data ->create new query
Select your ODBC-Connection, log in, select table, select fields, sorting ... quite easy


Third: create a viusal basic macro in excel, opening a database using an ADO connection-object:


Sub Retrieve_Records_from_Mytable1()


Dim sql As String
Dim fst As String


Dim wsht As Worksheet
Dim row, col, col1 As Integer
Dim frdid
Dim Con



' ADODB Connection

Set Con = CreateObject("ADODB.Connection")
Con.ConnectionString = "Provider=sqloledb;Data Source=localhost\sqlexpress;Initial Catalog=mydb;User Id=sa;Password=sapasswort"

Con.Open


Set rs = CreateObject("ADODB.Recordset")


frdid = InputBox("search for references ", "Referenz")

sql = "select * from mytable1 where reference like '" & frdid & "' order by reference"


rs.Open sql, Con



Set wsht = Worksheets(1)
wsht.Cells.Clear

col1 = 1

While col1 < rs.Fields.Count + 1
wsht.Cells(1, col1).Value = rs(col1 - 1).Name
wsht.Cells(1, col1).Font.Bold = True
wsht.Cells(1, col1).Font.Size = 12
wsht.Cells(1, col1).Font.Color = 50000
wsht.Cells(1, col1).Columns.AutoFit

'MsgBox "Fieldname " & rs(col1 - 1).Name & " Typ " & rs(col1 - 1).Type & " Predefined " & dbSTring
col1 = col1 + 1
Wend


'MsgBox "no of fields = " & rs.Fields.Count


row = 2 'no col titles
col = 1 'start at first cell

While Not rs.EOF
While col < rs.Fields.Count + 1

If Not IsNull(rs(col - 1).Value) Then
If rs(col - 1).Type = 200 Then wsht.Cells(row, col).NumberFormat = "@" 'als Text (Typ 200 ist String)
wsht.Cells(row, col).Value = CStr(rs(col - 1).Value)
'MsgBox "Wert von Feld " & col & " = " & rs(col-1).Value
Else
wsht.Cells(row, col).Value = ""
End If 'If Not IsNull(rs(col - 1).Value) Then

col = col + 1 'next col
Wend 'While col < rs.Fields.Count + 1
row = row + 1 'next line
col = 1 'first col
rs.MoveNext
Wend 'While Not rs.EOF

rs.Close
Con.Close
wsht.Columns.AutoFit
wsht.Name = "DS_as_" & frdid
Beep
MsgBox (row & " records retrieved")
End Sub



Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub




HopeThishelps

Heinrich
arias_melvin
arias_melvin
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 7
i already did the first solution. thanks for that. however i need to automate the extraction of data direct to excel that when i run the script it will create an excel file containing the selected data from a table in my database. i tried the bcp but it did not work.

could u give an idea or sample code regarding the above.

thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search