Académique Documents
Professionnel Documents
Culture Documents
- Dumping required data from MSSQL Server in flat file, Load this data in MySQL.
- Doing this the fastest available way.
- Understanding and Considering ETL methodology (ETL: Extract, Transform, Load)
Topics:
1. MSSQL Server Bulk Copy Program (command: bcp, to extract data (E of Etl)
2. MSSQL date handling for queries (data transformation/formatting, T of eTl)
3. Loading the dump file in MySQL (data loading, L of etL)
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------Topic 1: MSSQL Server Bulk Copy Program (Command: bcp), to extract data (E of Et
l)
------------------------------------------------------------------------------------Command Example:
bcp "SELECT * FROM databaseName.Schema.TableName" queryout/out "Path to file\out
put_file_name.ext" -n/c t"|f|" -r"|r|" -SServerName[\instanceName] -UuserName -Pp
assword
Explanation of parameters used above:
queryout/out : "queryout" means we are dumping result of a query. We can dump a
complete table, use databaseName.Schema.TableName instead of query and use "out"
instead of "queryout"
-n or -c : if -n is used, data will be dumped in native format (we don't want it
, because data comes out encrypted).
if -c is used, all data will be dumped in ASCII character format (we
need to use this)
-t: field terminator. Can be one or more character. In above example 3 character
s "|f|" are used. If omitted, tab character will be used as default.
-r: row terminator. Can be one or more character. In above example 3 characters
"|r|"" are used. If omitted, CrLf character combination will be used as default.
** Using 3 characters as field/row separator will increase dump file size, but
I think this combination will make us more certain whether a field or a row is a
ctually terminated.
In table fields, there is possibility of having Tab character or NewLine cha
racter as data.
-S: ServerName[\instanceName]
ServerName: Get it by running this query - "select @@servername"
[\instanceName]: We may not need this if a single instance is running on
the server. Get it by running this query - "select @@servicename"
**
1. Command parameter are cAsE sensitive.
emp_name
----------hulla
mia
rahman
jillur
naim
salary
-------------1000.78
300.7
15.6
55.9
-----Mostly we need to dump data filtered by date. So some useful date functions are
demonstrated below.
-- today
select getdate();
output> 2015-05-06 23:15:58.833
-- tomorrow
select getdate()+1;
output> 2015-05-07 23:16:24.740
-- yesterday
select getdate()-1;
output> 2015-05-05 23:17:32.467
-- only date, in mysql/canonical format
select convert(date, getdate());
output> 2015-05-06
select convert(date,getdate()-1);
output > 2015-05-05
-- a sample query on the above table emphasizing date
SELECT convert(date,logtime) as logtime, emp_name, salary
FROM testDb.dbo.testTab
where
convert(date,logtime) = convert(date,getdate()-1)
and emp_name like '%i%';
-- a query in bcp command, emphasizing date
bcp "SELECT convert(varchar(20),logtime, 20) as logtime, emp_name, salary FROM t
estDb.dbo.testTab where convert(date,logtime) = convert(date,getdate()-1) and em
p_name like '%i%'" queryout "C:\output_file.csv" -c -t"|f|" -r"|r|" -SPX-LAP -Us
a -Pporosh
-- content of data dump file (c:\output_file.csv) generated from above bcp execu
tion will look like below:
2015-05-05 15:30:00|f|jillur|f|15.6|r|2015-05-05 15:30:00|f|naim|f|55.8999999999
99999|r|
------------------------------------------------------------------------------------Topic 3: Loading the dump file in MySQL (data loading, L of etL)
------------------------------------------------------------------------------------We will load the dump file data in testDb.testTab which is a MySql table having
the exact same columns as the dumped data.
+----------+--------------+
| Field
| Type
|
+----------+--------------+
| logtime | datetime
|
| emp_name | varchar(250) |
| salary | double
|
+----------+--------------+