Blog Pro de Jean-Baptiste HEREN - Tag - Excel Notes d'un consultant Freelance en Informatique 2015-11-03T21:34:19+01:00 JB HEREN urn:md5:e39389b5ec134d99645112fce3d957df Dotclear Generate Extended properties in MS Sql Server 2005+ urn:md5:c93bd7c9259f6303069b285bf0dc2c62 2011-03-28T23:02:00+02:00 2011-03-29T13:46:40+02:00 Jean-Baptiste Heren Décisionnel Business IntelligenceExcelSQL Server 2005 <p>When you work with databases, it's cool to Keep all the documentation in the system, so that it is available for reporting and all kind of analysis.</p> <p>Microsoft <a href="http://blog.jbheren.com/tag/SQL%20Server%202005">SQL Server 2005</a>+ provides the ability to create Extended properties on tables (or even fields). Those properties are available using SQL management studio, on the tables properties panel.</p> <p><img src="http://blog.jbheren.com/public/Decisionnel/sql_server/.extended_properties_panel_m.jpg" alt="extended_properties_panel.jpg" title="extended_properties_panel.jpg, mar. 2011" /></p> <p>Here I will explain how you can generate them fast using Excel and macro AND Extract them using SQL.</p> <h2>1- Generate SQL Script to insert properties and values from an <a href="http://blog.jbheren.com/tag/Excel">Excel</a> sheet</h2> <p>Usually, for any initial documentation, it will be easyer to fill an excel sheet and generate allk the indidual properties automatically. You don't want to spend a week filling all the stuff manually(!). You will find as an attachment to this post, an <a href="http://blog.jbheren.com/tag/Excel">Excel</a> demonstration excel file containing a simple sheet template and a macro. The maco will gerenate SQL instructions like this :</p> <pre>[SQL] EXEC sys.sp_addextendedproperty @name=N'MY_PROPERTY_NAME', @value=N'MY_PROPERTY_VALUE', @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'MY_TABLE_NAME'; </pre> <p>Here is the SQL macro code</p> <pre>[VB] Sub generateSQLSpecialProperties() ' Author: Jean-Baptiste Heren - 2011-03-29 - http://blog.jbheren.com ' Loop on current scheet and generate sql sys.sp_addextendedproperty based on sheet contents ' First column contains the table name ' first line contains the Properties names ex: Table_name | prop_description | prop_rule ... Dim tableName As String 'tablename extracted from first row of each column Dim filePath As String Dim slashPosition As Integer Dim pathOnly As String Dim dataSQL As String ' will contain the SQL Contents Dim separator As String Dim myRow As Integer 'Row counter Dim myCol As Integer 'Column counter ' get current excel file Path filePath = ThisWorkbook.FullName slashPosition = InStrRev(filePath, &quot;\&quot;) pathOnly = Left(filePath, slashPosition) MyFile = pathOnly + ActiveSheet.Name + &quot;_macro_generated.sql&quot; 'get column values from second row myRow = 2 myCol = 2 'set and open file for output fnum = FreeFile() Open MyFile For Output As fnum Do Until ActiveSheet.Cells(myRow, myCol) = &quot;&quot; 'Loop on rows until blank. tableName = Trim(CStr(ActiveSheet.Cells(myRow, 1))) 'get Table name from first column Do Until ActiveSheet.Cells(myRow, myCol) = &quot;&quot; 'Loop on columns until blank. property_name = Trim(CStr(ActiveSheet.Cells(1, myCol))) 'get Property name from first column property_descr = Replace(Trim(CStr(ActiveSheet.Cells(myRow, myCol))), &quot;'&quot;, &quot;''&quot;) dataSQL = &quot;EXEC sys.sp_addextendedproperty @name=N'&quot; + property_name + &quot;'&quot; dataSQL = dataSQL + &quot;, @value=N'&quot; + property_descr + &quot;'&quot; dataSQL = dataSQL + &quot;, @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE'&quot; dataSQL = dataSQL + &quot;,@level1name=N'&quot; + tableName + &quot;';&quot; myCol = myCol + 1 Loop myCol = 2 ' Return to specifiedcolumn myRow = myRow + 1 ' Move to next row Print #fnum, dataSQL Loop ' Close the file Close #fnum End Sub </pre> <h2>2- Extract table properties from SQL Sercer System tables</h2> <pre>[SQL] /* get extended properties on Tables */ SELECT A.NAME as table_name ,B.name as property_name ,B.value as property_value FROM SYSOBJECTS A INNER JOIN sys.extended_properties B on A.id = B.major_id WHERE A.type = 'U' </pre> Macro Excel : Générer les ordres SQL INSERT à partir d'une feuille Excel urn:md5:c4d6ffac743fb86b786cce09263ea5d3 2010-06-29T16:57:00+02:00 2010-07-01T12:30:34+02:00 Jean-Baptiste Heren Décisionnel ExcelSQL <p>Voici le code d'une macro permettant la génération automatique d'ordres SQL INSERT à partir des données d'une feuille excel.</p> <p>prérequis :</p> <ul> <li>La première ligne de données doit contenir le nom des colonnes.</li> <li>Le nom de la feuille doit correspondre au nom de la table</li> </ul> <p>Résultat:</p> <p>La macro génère un fichier .sql portant le nom de la table cible, contenant les ordres SQL du type INSERT INTO mytable(col1,col2,...) VALUES(val1,val2,...).</p> <br /> <br /> Sub generateSQLInsert()<br />   'Loop on current scheet and generate sql INSERT based on column names as columns an sheet name as table<br />   ' Will create one statement per line aka  INSERT INTO mytable(col1,col2,...) VALUES(val1,val2,...)<br />   ' Author: Jean-Baptiste Heren - 29-06-2010 - http://blog.jbheren.com<br />  <br />   Dim tableName As String 'tablename extracted from first row of each column<br />   Dim filePath As String<br />   Dim slashPosition As Integer<br />   Dim pathOnly As String<br />  <br />   Dim columnsSQL As String ' will contain the insert into table (col1,col2,...) statement<br />   Dim dataSQL As String    ' will contain the values (val1,val2,...) statement<br />   Dim separator As String<br />  <br />   Dim myRow As Integer 'Row counter<br />   Dim myCol As Integer 'Column counter<br />  <br />   'set Tablename from sheet name<br />   tableName = ActiveSheet.Name<br />  <br />   ' get current excel file Path<br />   filePath = ThisWorkbook.FullName<br />   slashPosition = InStrRev(filePath, &quot;\&quot;)<br />   pathOnly = Left(filePath, slashPosition)<br />  <br />   MyFile = pathOnly + tableName + &quot;.sql&quot;<br />  <br />   'get column names from first row<br />   myColInput = InputBox(&quot;What column to START on ?&quot;)<br />   myCol = myColInput<br />   myRow = 1<br />   columnsSQL = &quot;INSERT INTO &quot; + tableName + &quot; (&quot;<br />   separator = &quot; &quot;<br />  <br />   Do Until ActiveSheet.Cells(myRow, myCol) = &quot;&quot; 'Loop until you find a blank.<br />     <br />     columnsSQL = columnsSQL + separator + Trim(ActiveSheet.Cells(myRow, myCol))<br />     separator = &quot;, &quot;<br />     <br />     myCol = myCol + 1 ' Move to next column<br />   Loop<br />   columnsSQL = columnsSQL + &quot;) &quot;<br />  <br />   'get column values from second row<br />   myRow = 2<br />   myCol = myColInput<br />  <br />   'set and open file for output<br />   fnum = FreeFile()<br />   Open MyFile For Output As fnum<br />  <br />  <br />   Do Until ActiveSheet.Cells(myRow, myCol) = &quot;&quot; 'Loop on rows until blank.<br />     <br />     dataSQL = &quot;VALUES (&quot;<br />     separator = &quot; &quot;<br />     <br />       Do Until ActiveSheet.Cells(myRow, myCol) = &quot;&quot; 'Loop on columns until blank.<br />         dataSQL = dataSQL + separator + &quot;'&quot; + Trim(CStr(ActiveSheet.Cells(myRow, myCol))) + &quot;'&quot;<br />         separator = &quot;, &quot;<br />         myCol = myCol + 1<br />       Loop<br />     <br />     dataSQL = dataSQL + &quot;);&quot;<br />     <br />     myCol = myColInput ' Return to specifiedcolumn<br />     myRow = myRow + 1 ' Move to next row<br />     <br />     Print #fnum, columnsSQL + dataSQL<br />   Loop<br />  <br />   ' Close the file<br />   Close #fnum<br />  <br /> End Sub<br /> <br /> <br /> Convertir un fichier csv en xls en ligne de commande : mycsv2xls.vbs urn:md5:dca64bff25a19117b8b39f7315fbb10a 2010-05-27T16:01:00+02:00 2010-06-14T12:05:13+02:00 Jean-Baptiste Heren Décisionnel csv2xlsExcelMicrosoft SSIS <p>Suite à un besoin ponctuel d'intégration de données, j'ai développé un petit script en VB, utilisant l'application Excel pour faire l'import automatique de fichiers délimités par des points-virgule dans un fichier Excel.</p> <p>En effet, une source Excel dans Microsoft SSIS est beaucoup plus facile d'emploi qu'un fichier plat, notamment lorsque le nombre de colonnes dans le fichier n'est pas fixe.</p> <p>Le Script en question pourra être exécuté dans SSIS par l'utilisation d'un 'Execute Process Task'.</p> <p>le programme <a href="http://blog.jbheren.com/public/Decisionnel/cscript/mycvs2xls.zip">mycsv2xls</a> pour Windows fonctionne comme suit :</p> <blockquote> <p>mycvs2xls.vbs source_full_path target_directory</p> </blockquote>