sql server - Import Table definition (fields) from excel sheet -
my excel sheets contains table definitions follows:
fieldname fieldtype id int name text
each sheet represents different table , got 20 tables total of 800 fields.i need creating these tables in database, in quickest way possible. solutions have in mind are:
1- create them 1 one manually
2- copy , past excel sheet field definitions sql management studio , add/adjust necessary syntax each field "create table" query.
i find both of these solutions time-consuming, there way directly import table definition excel sheet?
there few ways build table definitions in excel. either through vb script/macro and/or excel formulas. vb script provide opportunity more robust solution more involved. however, formulas can of job done quickly.
i put example formulas. in question, present field name , field type. helpful add field width , precision variable length string fields , numeric fields. mentioned using sql server. in case, use actual sql server field types in spreadsheet. example, use "varchar" instead of "text". following example:
|a |b |c |d |e |f -------------------------------------------------------------------------- 1|fieldname |fieldtype|fieldwidth|precision| | 2| | | | |create table table1 ( |typed text 3|id |int | | |id int , |formula 4|name |varchar |30 | |name varchar( 30 ), |formula 5|dateofbirth|date | | |dateofbirth date , |formula 6|hoursworked|numeric |5 |2 |hoursworked numeric( 5, 2),|formula 7| | | | |) |typed text 8| | | | |go |typed text
snapshot of table creation excel worksheet
in example, column e rows 2 through 8 contain resulting sql script. further, column e, row 2 typed straight in text. same case column e row 7 , row 8.
for column e rows 3 through 6 there formula entered.
the formula in column e row 3 follows:
=if(isnumber(d3),trim(concatenate(a3," ",b3,"( ",c3,", ",d3,"),")),if(isnumber(c3),trim(concatenate(a3," ",b3,"( ",c3," ",d3,"),")),trim(concatenate(a3," ",b3," ",c3," ",d3,","))))
that formula, once pasted column e row 3 copied down through row 6.
the results in column e rows 2 through 8 can copied , pasted sql server management studio create table1.
note: once script in management studio, remove "," in last field definition. it's there because excel formula not robust enough keep showing up.
Comments
Post a Comment