SQL Server 2012 : Manageability for Administrator

sqlserver2012Beberapa bulan lalu, pada acara SQL Server Meeting di kantor Microsoft Indonesia, saya mangajukan diri untuk membuat salah satu bab ebookWhat’s New di SQL Server 2012. Saya mendapat jatah membuat materi Manageablity for Administrator

Saya sudah menyelesaikan materi tersebut pada bulan Februari 2012. Ketika memulai membuat materi , SQL Server 2012 masih pada tahap RC0, dan sampai hari peluncurannya (melewati tahap RTM) saya  belum mendengar kabar nasib ebook tersebut. Pada acara monthly meeting bulan April kebetulan saya tidak bisa hadir karena sedang tugas di luar kota, pada saat itu saya berniat menanyakan kelanjutannya.

Pada akhirnya.. Yo wess lah..., daripada materinya jamuran di harddisk laptop, lebih baik saya share saja, toh pada akhirnya akan dibagikan gratis juga dalam bentuk ebook.

Download [download#13]

Linked Servers, How To Read MySQL On SSMS 2008

A linked server configuration enables Microsoft SQL Server (MSSQL) to execute commands against OLE DB data sources on remote servers. Linked servers offer the following advantages: Remote server access ; The ability to issue distributed queries, updates, commands, and transactions on heterogeneous data sources across the enterprise ; and The ability to address diverse data sources similarly.

Theese are the real world scenario : Server A (Windows Server ) is installed with Microsoft SQLServer 2008 (MSSQL 2008) Database equip with SQL Server Management Studio (SSMS), Server B (Windows Server,*NIX, etc) is intalled with MySQL Database.

Next, how we setup a linked server ?

Theese steps are setup on Server A.

1.Download and Install Connector/ODBC Driver (5.1.8 or latest) form MySQL

http://www.mysql.com/downloads/connector/odbc/

2.Create and Configure DSN above ODBC Driver

GoTo START : > Settings > Control Panel > Administartif Tools > Data Source (ODBC) > User DSN > Add > MySQL ODBC 3.51 Driver > Finish

Fill in the blank : Data Source Name, Description , Server, User, Password, Database

GoTo TAB: > Connect Options >

Fill in the blank: Port (MySQL Default Port = 3306)

Click ‘OK’

3.SetUp MSDASQL Provider Properties
Open MSSQL database on Server A, via SQL Server Management Studio (SSMS) 2008.

Goto : SSMS > Server Objects > Linked Servers > Providers > Right Click “MSDASQL

Check (activate) / tick theese items : Nested Queries, Level Zero Only, Allow Inpocess, Supports “LIKE” Operator

4.Create Linked Server
Open MSSQL database on Server A, via SQL Server Management Studio (SSMS) 2008.

Create Linked Server Object : SSMS > Server Objects > Linked Servers > Right Click > New Linked Server

 Fill in the blank : Linked Server, Provider, Product Name, Data Source

Provider: Microsoft OLEDB Provider for ODBC Drivers

5.Create and test it with SELECT statement.

Open “New Query” and test it with “SELECT … OPENQUERY” statement

SELECT * FROM OPENQUERY([FOO], ‘SELECT * FROM FOOTABLE  LIMIT 10′)

and..here we go, first problem

Msg 7342, Level 16, State 1, Line 2
An unexpected NULL value was returned for column “[MSDASQL].dtadded” from OLE DB provider “MSDASQL” for linked server “FOO”. This column cannot be NULL.

Ups, i think the driver default configuration cannot handle NULL value properly, and luckly there is a open configuration to manage NULL value.

GOTO : DataSource > ODBC > FOODSN > ADVANCED > CURSOR/RESULT >

Then tick/check

Return SQL_NULL_DATA for zero date

Then again, test it with SQL/OPENQUERY statement. If other probelem (error message) still persist.. , like this one

Msg 8114, Level 16, State 10, Line 1
Error converting data type DBTYPE_DBDATE to date.

and it’s time to use ODBC scalar convertion

Explicit data type conversion is specified in terms of SQL data type definitions. The ODBC syntax for the explicit data type conversion function does not restrict conversions. The validity of specific conversions of one data type to another data type will be determined by each driver-specific implementation. The driver will, as it translates the ODBC syntax into the native syntax, reject those conversions that, although legal in the ODBC syntax, are not supported by the data source. The ODBC function SQLGetInfo, with the conversion options (such as SQL_CONVERT_BIGINT, SQL_CONVERT_BINARY, SQL_CONVERT_INTERVAL_YEAR_MONTH, and so on), provides a way to inquire about conversions supported by the data source.

Example:

SELECT dtadded FROM OPENQUERY([FOO], ‘SELECT CONVERT(dtadded, CHAR) dtadded FROM FOOTABLE  LIMIT 10′)

That should do the work  … Good Luck!

HeidiSQL Rock!

Arrghh!! need to manage an old version of MySQL database. Turnout the vary of MySQL version is a serious problem to lot of free MySQL Graphic User Interface (GUI). Most of it, cannot manage old version of MySQL database, only the new one *DOH!!*. Finally found… The only free GUI that work best and flawlessly is

HeidiSQL.

HeidiSQL is a lightweight, Windows based interface for MySQL databases. It enables you to browse and edit data, create and edit tables, views, procedures, triggers and scheduled events. Also, you can export structure and data either to SQL file, clipboard or to other servers.

HeidiSQL also support MariaDB and Microsoft SQL Server!

Shrink / Truncate Log File On SQL Server 2008

The common T-SQL script to shrink/truncate log file is using the script below

USE [foo]
GO
DBCC SHRINKFILE(foo_log, 1)

But, if the script runs on SQL Server 2008 instance, it will fail. You still have the same log file size, nothing change (shrink) . In other word you cannot shrink the log file.

To solve the issue here’s a work around:

Microsoft SQL Server 2008 has a default setting ‘FULL‘ for Recovery Model, so that means that we cannot just shrink Log File to minimum size.
Change the recovery model to ‘SIMPLE‘ and Shrink the Log File using:

USE [foo]
DBCC SHRINKFILE(foo_log, 1)

If ‘FULL‘ Recovery Mode still needed, cause is crucial for transaction DB, you can use ALTER sql command to  change and recover Recovery Mode to its original state.

USE [foo]
GO
ALTER DATABASE [foo] SET RECOVERY SIMPLE WITH NO_WAIT
DBCC SHRINKFILE(foo_log, 1)
ALTER DATABASE [foo] SET RECOVERY FULL WITH NO_WAIT
GO

CURSOR FETCH on SQL Server

although CURSOR and FETCH is classified as ‘evil‘ in SQL server, but sometimes we cannot avoid using it. Cause in some scenarios, pivot and other similiar method to perform looping and transformation is not sufficient enough. here’s a link of good example implementing cursor.

all example copy from microsoft sites,

http://msdn.microsoft.com/en-us/library/ms180152.aspx

A. Using FETCH in a simple cursor

The following example declares a simple cursor for the rows in the Person.Person table with a last name that starts with B, and uses FETCH NEXT to step through the rows. The FETCH statements return the value for the column specified in DECLARE CURSOR as a single-row result set.

USE AdventureWorks2008R2;
GO
DECLARE contact_cursor CURSOR FOR
SELECT LastName FROM Person.Person
WHERE LastName LIKE 'B%'
ORDER BY LastName;

OPEN contact_cursor;

-- Perform the first fetch.
FETCH NEXT FROM contact_cursor;

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM contact_cursor;
END

CLOSE contact_cursor;
DEALLOCATE contact_cursor;
GO

B. Using FETCH to store values in variables

The following example is similar to example A, except the output of the FETCH statements is stored in local variables instead of being returned directly to the client. The PRINT statement combines the variables into a single string and returns them to the client.

USE AdventureWorks2008R2;
GO
-- Declare the variables to store the values returned by FETCH.
DECLARE @LastName varchar(50), @FirstName varchar(50);

DECLARE contact_cursor CURSOR FOR
SELECT LastName, FirstName FROM Person.Person
WHERE LastName LIKE 'B%'
ORDER BY LastName, FirstName;

OPEN contact_cursor;

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement. 

FETCH NEXT FROM contact_cursor
INTO @LastName, @FirstName;

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

   -- Concatenate and display the current values in the variables.
   PRINT 'Contact Name: ' + @FirstName + ' ' +  @LastName

   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM contact_cursor
   INTO @LastName, @FirstName;
END

CLOSE contact_cursor;
DEALLOCATE contact_cursor;
GO

C. Declaring a SCROLL cursor and using the other FETCH options

The following example creates a SCROLL cursor to allow full scrolling capabilities through the LAST, PRIOR, RELATIVE, and ABSOLUTE options.

USE AdventureWorks2008R2;
GO
-- Execute the SELECT statement alone to show the
-- full result set that is used by the cursor.
SELECT LastName, FirstName FROM Person.Person
ORDER BY LastName, FirstName;

-- Declare the cursor.
DECLARE contact_cursor SCROLL CURSOR FOR
SELECT LastName, FirstName FROM Person.Person
ORDER BY LastName, FirstName;

OPEN contact_cursor;

-- Fetch the last row in the cursor.
FETCH LAST FROM contact_cursor;

-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM contact_cursor;

-- Fetch the second row in the cursor.
FETCH ABSOLUTE 2 FROM contact_cursor;

-- Fetch the row that is three rows after the current row.
FETCH RELATIVE 3 FROM contact_cursor;

-- Fetch the row that is two rows prior to the current row.
FETCH RELATIVE -2 FROM contact_cursor;

CLOSE contact_cursor;
DEALLOCATE contact_cursor;
GO

SAP BW UD Connect and SQL Server Unicode Setting

sap logoWith UD Connect you can now integrate the data for the source object into SAP BW. You can either extract the data, load it into SAP BW and physically store it there, or, as long as the prerequisites for this are fulfilled, you can read the data directly in the source using a SAP RemoteCube. Example SQL Server 2008…

But, when we tries to extract data from SQL Server 2008 x64 database to SAP BW NW7 with UD Connect , we get an error message.

The error message when executing an  Info Package to load/transfer data from SQL Server 2008 x64 to SAP BW NW7.

S:RSSDK:400 Cannot convert field DECIMALS of type NUM to int

to solve the problem you have to check the SQl Server “UnicodeSetting

Steps:

1.) Go to SAP BW > RSA1 > Source System > UD Connect > ….

Right Clik on the UD connection name and Click on  Connection Param.
sap udconnect menu

3.)  Go to MDMP & Unicode Tab and Run Unicode Test you must have access to TCODE SM59

4.) If your target system is Unicode, then you have to tick Unicode for Communication Type for Target System.
sap udconnect unicode

5.) Save and Close the Source System Dialog

6. )re Run the Execution of Info package.

Good Luck

Part 2 : Geometry dan Geography di SQL Server 2008

Ada 2 Jenis tipe data baru (spatial), pada SQL server 2008. Kedua jenis data tersebut adalah GEOMETRY dan GEOGRAPHY.

1. Geometry, pada permukaan datar. (Flat Earth Model). presentasi dari X dan Y
2. Geography, pada permukaan yang bulat. (Ellipsoidal Model). presentasi dari Latitude dan Lontitude.

new data type Lakukan Instalasi SQL Server 2008 (Tipe apa saja, termasuk Express) dan anda akan menemukan ke dua jenis data ini. Great!! , ternyata versi Express pun masih mendukungnya.

Terpenting, Microsoft juga mengikuti Standart Open Geospatial  Concortium (OGC), sama seperti tipe data spatial lainnya yang sudah terkenal lebih dahu. Dengan mengacu ke standart yang sama, seharusnya(diharapkan) kompatibilitas data untuk migrasi jadi lebih mudah.

Tipe data GEOMETRY ataupun GEOGRAPHY menghasilkan 7 turunan tipe data (inheritance). Tujuh tipe data ini yang lebih dikenal di kalangan geografer sebagai tipe data yang mempresentasikan spatial.

1.Point
2.MultiPoint
3.LineString
4.MultiLineString
5.Polygon
6.MultiPolygon
7.GeometryCollection

Jadi, bahasa ‘keren’nya, ke tujuh tipe data tersebut disimpan/dikenali di tipe data GEOMETRY dan GEOGRAPHY nya SQL Server 2008. 🙂

Break dulu, pekerjaan rutin menanti…. to be continue..

Part 1 : Pengenalan Spatial Data Type di SQL Server 2008

Akhirnya Microsoft SQL Server 2008 mendukung tipe data spatial (keruangan). Sebelum memulai masuk ke bagian teknis ada baiknya kita sedikit berkenalan dahulu dengan data spatial.

Data spatial (keruangan) dasarnya di bagi menjadi 2 bagian yaitu jenis data vektor dan satunya lagi jenis data raster.

Jenis data vektor adalah “hasil” dari koordinat X,Y, Z untuk menggambarkan titik, kumpulan X dan Y untuk menggambarkan line dan shape(polygon) serta kemudian koordinat Z untuk menggambarkan ketinggian. Jenis data spatial dalam bentuk flat file / non db  sangat beragan, ada SHP, TAB, SDF dll.

Sedangkan jenis data raster digunakan untuk menggambarkan citra satelite (image). jenis flat file nya/non db  juga sangat beragam. JPEG200, MrSID, ECW, TIFF, dll. Perbedaan dengan image raster pada umumnya, tipe data raster untuk keperluan spatial memiliki informasi Georeference(rujukan geografis) dengan mengacu kepada jenis proyeksi tertentu.

Jenis proyeksi ? bagi kalangan Programmer istilah ini mungkin tidak familiar. istilah ini lebih familiar di lingkungan geografi atau geoinformatics (jurusan ini kayaknya nggak ada di indonesia deh ..:). Jenis Proyeksi adalah cara bagaimana kita melihat / memproyeksikan bumi. Karena bumi bentuknya bulat, makan dibutuhkan acuan proyeksi tertentu untuk menggambarkan bumi pada bidang datar (misal:kertas atau monitor).

Bayangkan jeruk, kupas kulitnya pada bagian dan luas tertentu, tempelkan dan tekan di bidang datar dan anda akan mendapatkan bagian yang luasnya menngecil atau meluas. 🙂

to be continue…

Part 3: Data Loading,Methods dan Sample GEOMETRY di SQL Server 2008

Pada tipe data GEOMETRY (lupakan dahulu GEOGRAPHY, agar tidak bingung), SQL Server 2008 memiliki 3 opsi loading data (membentuk data) : OGC Well Known Text (WKT), Well Known Binary (WKB) dan Geography Markup Language (GML). Sedangkan untuk mempresentasikan data, SQL Server memiliki beberapa Methods sesuai standart OGC dan beberapa yang sifatnya extended.

Contoh Syntax WKT:

POINT(7 7) // X Y (jadi titik)
POINT(7 7 3 2)  // X Y Z M // Z (elevation), dan M (measure) MULTIPOINT((2 3), (7 8 3), (4 5)) // gambar 3 titik
LINESTRING(40 40, 60 60) //titik awal, titik akhir (garis)
POLYGON((30 30, 30 100, 100 100, 100 30, 30 30)) // empat titik bentuk POLYGON, titik ke lima = pertama

Contoh Method (OGC) pada GEOMETRY Instance (sesuai abjad):

  • STArea
  • STAsBinary
  • STAsText
  • STBoundary
  • STBuffer
  • ….

lainnya bisa dilihat di bagian Help (F1) MSSQl Server 2008. 🙂

Ketika menjalankan perintah syntax spatial di SQL Server Management studio 2008, perhatikan bagian outputnya, selain tab ‘result’ dan ‘message’, juga terdapat tab ‘spatial’

Contoh penggunaan Syntax WKT dan Method untuk mengambar Polygon.

DECLARE @geocontoh Geometry
SET @geocontoh = Geometry::STGeomFromText(‘POLYGON(
(10 90, 10 100, 50 100, 50 90, 10 90),
(10 10, 10 90,  20 90,  20 10, 10 10),
(20 10, 20 20, 50 20, 50 10, 20 10),
(40 20, 40 50, 50 50, 50 20, 40 20),
(30 40, 30 50, 40 50, 40 40, 30 40)
)’,0)
SELECT @geocontoh
SET @geocontoh = @geocontoh.MakeValid();
SELECT @geocontoh.ToString();

Hasil:

contoh geometry

Note: Angka “0” setelah kumpulan koordinat polygon sebenarnya di tujukan untuk angka SIRD (spatial reference identifier) a.k.a refrensi proyeksi. Dalam contoh ini diabaikan. Sedangkan Method “MakeValid” digunakan untuk validasi syntax WKT, apakah polygon yang digambar saling memotong atau bertumpuk sehingga syntax WKT nya berubah dari POLYGON menjadi MULTIPOLYGON. Hemm.. i love it.

have a worderful weekend… to be continue….

Step 4: Extraction, Loading SPATIAL Data Type

Pada tiga penjelasan sebelumnya, kita sudah berkenalan dengan jenis data spatial di SQL Server 2008. Topik berikunya mengenai load data spatial jenis shapefile(shp) ke dalam table SQL Server 2008.

Mengapa saya memilih menyimpannya di SQL Server 2008 dibandingkan dengan penyimpanan format flat file macam shapefile (shp,idx,dbf) ? Berdasar pengalaman, data dalam bentuk tabular lebih mudah untuk di ‘manage’ walaupun tentunya terjadi sedikit penurunan dari segi performance.

Untuk menguji kehandalan tipe data spatial SQL Server 2008, saya akan mencobanya dengan load data besar berupa line (770 ribu line / 240 MB) dan polygon (1,7 juta polygon/ 560 MB) . Load data dilakukan dengan tools ‘Shape2SQL‘ ciptaan Morten Nielsen, best and simpliest tools so far.., and it is free. Jangan lupa untuk menghilangkan ‘tick’ create spatial index. pada beberapa shapefile akan mengakibatkan gagal load ke tabel SQL Server

Setelah menunggu selama 2 jam (heh..lama juga ya!), akhirnya data polygon dan line itu “mampet” semuanya ke tabel SQL Server 2008, tentunya dengan skip beberapa polygon yang rusak.

langkah berikutnya adalah melakukan preview di SQL Server Management Studio (SSMS). UNION kan preview dua tabel tersebut..

shape2sql 19-10-2010 10-10-15 - 3

Dan ini lah hasilnya:

shape2sql 19-10-2010 10-10-15 - 2

Pada posting berikutnya akan saya bagikan pengalaman membaca data spatial di SQl server 2008 tersebut melalui NET component