Export Sql Data to Excel with sql command

SQL 2000

SQL 2005

มีสมาชิก greatfriends ถามมาจึงจัดให้ http://greatfriends.biz?73442

สำหรับ ท่านที่ใช้ SQL 2005 ให้เข้าไป Enable  OPENROWSET ตามรูป 1-3 ก่อนนะครับ ส่วน SQL 2000 เอา SQL Script ไปใช้ได้เลย


รูปที่2 Enable OPENROWSET

รูปที่3 Restart SQL Services

–SQL Script

–Query Excel
‘Excel 8.0;Database=C:Book1.xls;HDR=YES’,
‘SELECT * FROM [Sheet_Name$]’)

–Export SQL table to Excel
insert into OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’,
‘Excel 8.0;Database=C:Book1.xls;’,
‘SELECT * FROM [Sheet_Name$]’) select * from Table_Name

–Import Excel to SQL table
Insert into Table_Name
Select * FROM OPENROWSET (‘Microsoft.Jet.OLEDB.4.0’,
‘Excel 8.0;Database=D:Book1.xls;HDR=YES’,
‘SELECT * FROM [Sheet_Name$]’)

— หรือใช้ xp_cmdshell

Enable xp_cmdshell  ตามรูป

แล้ว Restart SQL Services อีก 1 ครั้ง

–Export SQL table to Excel
Exec Master..xp_cmdshell ‘bcp "Select * from Northwind..employees" queryout "C:Book1.xls" -c -UdbUserName -PdbPassword’


Nuchit Atjanawat is a Microsoft MVP, GreatFriends Community Leader, WindowsITPro Columnist(Mr.Nano), Silverlight Business Application Instructor, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession. He has a very good skill over Silverlight, LightSwitch, C#, XAML & Blend. He shares his technical findings, tutorials in his technical blog. Follow his Blog and Facebook page to be updated on latest articles. - Technical Blog: https://janawat.wordpress.com - WindowsITPro Columnist(Mr.Nano): http://windowsitpro.net - The GreatFriends.Biz Community Discussion Board (nano): http://greatfriends.biz - witter: https://twitter.com/janawat - Facebook Page: https://www.facebook.com/nuchit - Instructor: http://bit.ly/GF250-01, http://bit.ly/GF150-02

เขียนใน Database


Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out /  เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out /  เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out /  เปลี่ยนแปลง )


Connecting to %s

In Archive