<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'powershell', 'Walkthroughs', 'Web', and 'SQL Server'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=powershell,Walkthroughs,Web,SQL+Server&amp;orTags=0</link><description>Search results matching tags 'powershell', 'Walkthroughs', 'Web', and 'SQL Server'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>PowerShell: Read Excel to Create Inserts</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/03/18/powershell-read-excel-to-create-inserts.aspx</link><pubDate>Thu, 18 Mar 2010 12:09:36 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:23520</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;I’m writing &lt;a href="http://www.informit.com/guides/content.aspx?g=sqlserver&amp;amp;seqNum=342" target="_blank"&gt;a series of articles on how to migrate “departmental” data into SQL Server&lt;/a&gt;. I also hold workshops on the entire process – from discovering that the data exists to the modeling process and then how to design the Extract, Transform and Load (ETL) process. Finally I write about (and teach) a few methods on actually moving the data.&lt;/p&gt;  &lt;p&gt;One of those options is to use PowerShell. There are a lot of ways even with that choice, but the one I show is to read two columns from the spreadsheet and output statements that would insert the data using a stored procedure. Of course, you could re-write this as INSERT statements, out to a text file for bcp, or even use a database connection in the script to move the data directly from Excel into SQL Server. &lt;/p&gt;  &lt;p&gt;This snippet won’t run on your system, of course – it assumes a Microsoft Office Excel 2007 spreadsheet located at &lt;strong&gt;c:\temp&lt;/strong&gt; called &lt;strong&gt;VendorList.xlsx&lt;/strong&gt;. It looks for a tab in that spreadsheet called &lt;strong&gt;Vendors&lt;/strong&gt;. The statement that does the writing just uses one column: &lt;strong&gt;Vendor Code&lt;/strong&gt;. Here’s the breakdown of what I’m doing:&lt;/p&gt;  &lt;p&gt;In the first block, I connect to Microsoft Office Excel. That connection string is specific to Excel 2007, so if you need a different version you’ll need to look that up.&lt;/p&gt;  &lt;p&gt;In the second block I set up a selection from the entire spreadsheet based on that tab. Note that if you’re only after certain data you shouldn’t get the whole spreadsheet – that’s just good practice.&lt;/p&gt;  &lt;p&gt;In the next block I create the text I want, inserting the Vendor Code field as I go.&lt;/p&gt;  &lt;p&gt;Finally I close the connection.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;  &lt;p&gt;&lt;font color="#408080"&gt;$ExcelConnection= New-Object -com &amp;quot;ADODB.Connection&amp;quot;      &lt;br /&gt;$ExcelFile=&amp;quot;c:\temp\VendorList.xlsx&amp;quot;       &lt;br /&gt;$ExcelConnection.Open(&amp;quot;Provider=Microsoft.ACE.OLEDB.12.0;`       &lt;br /&gt;Data Source=$ExcelFile;Extended Properties=Excel 12.0;&amp;quot;) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#408080"&gt;$strQuery=&amp;quot;Select * from [Vendors$]&amp;quot;      &lt;br /&gt;$ExcelRecordSet=$ExcelConnection.Execute($strQuery) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#408080"&gt;do {      &lt;br /&gt;Write-Host &amp;quot;EXEC sp_InsertVendors '&amp;quot; $ExcelRecordSet.Fields.Item(&amp;quot;Vendor Code&amp;quot;).Value &amp;quot;'&amp;quot;      &lt;br /&gt;$ExcelRecordSet.MoveNext()}       &lt;br /&gt;Until ($ExcelRecordSet.EOF) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#408080"&gt;$ExcelConnection.Close()&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;           &lt;p&gt;&lt;/p&gt;         &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;&lt;font face="Calibri"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.&lt;/font&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</description></item></channel></rss>