วันพุธที่ 10 มีนาคม พ.ศ. 2553

Microsoft SQL Server 2008 "BCP" is not support UTF-8 encoding ????

ลองดูที่นี่ครับ คนด่ากันระงม ฮ่า ๆ 
เป็นไปได้ไงที่ใน Version ก่อน คือ 2005 Support แล้วตอนนี้ 2008 ไม่ Support
Codepage ของ UTF-8 คือ 65001 นะ

SQL Server 2008 : BCP document
https://connect.microsoft.com/SQLServer/feedback/details/370419/bulk-insert-and-bcp-does-not-recognize-codepage-65001

SQL Server 2005 : BCP document
http://msdn.microsoft.com/en-us/library/ms162802(SQL.90).aspx

How to copy data between difference collations
http://msdn.microsoft.com/en-us/library/ms190657.aspx

วันอังคารที่ 9 มีนาคม พ.ศ. 2553

Export text file from sql server using BCP and SQL Script

- How to use bcp in sql server ?
- Export text file,csv file from sql server via sql script 
- Export ข้อมูลจาก SQL Server เป็น Text file,csv file ด้วย SQL Script

การ Export ไฟล์จากฐานข้อมูล Sql server เป็น Text file / CSV file
BCP เป็น Utility ตัวหนึ่งของ SQL Server ที่ใช้สำหรับ Bulk insert/ Export ไฟล์ในรูปแบบต่าง ๆ

How to use BCP in SQL Server, (วิธีการใช้งาน BCP ใน SQL Server )
1 Enabling  sys.xp_cmdshell   (เปิดใช้งาน xp_cmdshell)

sp_configure 'show advanced options',1
GO
RECONFIGURE
GO
sp_configure xp_cmdshell,1
GO
RECONFIGURE

If xp_cmdshell is disable this error will occure "SQL Server blocked access to procedure 'sys.xp_cmdshell"

2 Check  xp_cmdshell value
Exec sp_configure
GO
go to last line and check config_value of xp_cmdshell , value need to be "1"

(ไปที่บรรทัดสุดท้ายเพื่อตรวจสอบค่า หากเป็น "0" ต้อง set เป็น "1" ในขั้นตอนที่ 2)

 3 Create test sript to export data to text file (ตัวอย่าง BCP Script สำหรับ Export text file)
This example will show How to export data from table delimited by comman (",")
ในตัวอย่างจะแสดงวิธีการ Export ข้อมูลออกเป็น text file โดยคั่น Columns ด้วย  comma
USE AdventureWorks

Declare @Headers varchar(1000),@sql varchar(8000), @data_file varchar(100),
@x varchar(300),@file_name varchar(100)
BEGIN
SET @file_name = 'C:\test_bcp.txt'
---------------- HEADER ROWS GENERATE ----------------
Select @Headers = IsNull(@Headers + ',', '') + Column_Name
From INFORMATION_SCHEMA.COLUMNS
Where Table_Name = 'CreditCard' ORDER BY ORDINAL_POSITION ASC

print @Headers

set @sql = 'bcp "select ''' + @Headers + '''" queryout "'+@file_name+'" -U "sa" -P "1234" -c -C 65001 -t "," -r \n'


print @sql
exec master..xp_cmdshell @sql
set @sql = 'exec master..xp_cmdshell ' + @sql
print @sql

-----------------DATA SECTION ------------------------
select @data_file=substring(@file_name,1,len(@file_name)-charindex('\',reverse(@file_name)))+'\data_file.csv'
print @data_file


set @sql = 'bcp "select * from [AdventureWorks].sales.CreditCard '+' " queryout "'+@data_file+'" -U "sa" -P "1234" -c -C 65001 -t "," -r \n'


print @sql
exec master..xp_cmdshell @sql
--Copy dummy file to passed CSV file


set @sql= 'exec master..xp_cmdshell ''type '+@data_file+' >> "'+@file_name+'"'''
print @sql
exec(@sql)
--Delete dummy file
set @sql= 'exec master..xp_cmdshell ''del '+@data_file+''''
print @sql
exec(@sql)
---------------- END DATA SECTION -------------------
END


- Dont forget to change sa password in script.
- Exported file will store in server side! Not client.

วันจันทร์ที่ 8 มีนาคม พ.ศ. 2553

Tracking dimensions member in specify time periods with filter function (MDX QUERY)

Tracking dimensions member in specify time periods with filter function(MDX Query)
- MDX ,Population sampling data
-

ตัวอย่างของการ Tracking ข้อมูลจาก Dimension Members
ในกรณีนี้คือการหาว่าสินค้าที่เคยขายได้ในเดือน January 2003
ยังคงขายได้ในเดือนถัดไป มีตัวเลขเป็นเท่าไร


Example data : AdventureWork
----------------------------------------------------------------------
WITH
SET [SellingProducts] as
Filter (
[Product].[Product].[Product].MEMBERS
,([Date].[Calendar].[Month].&[2003]&[1] /* January 2003*/
, [Measures].[Sales Amount]) >=1500
)


SELECT
{ [Measures].[Sales Amount]} ON Columns
,
NON EMPTY
[Product].[Category].[Category] *
[SellingProducts] *
{
[Date].[Calendar].[Month].&[2003]&[1], /* January 2003*/
[Date].[Calendar].[Month].&[2003]&[2], /* February 2003*/
[Date].[Calendar].[Month].&[2003]&[3] /* March 2003*/
} ON Rows
FROM [Adventure Works]

-------------------------------------------

MDX Description (คำอธิบาย MDX Query)
1. Create set of members for exampling. In this case I want to track which products has sold in January 2003
    and sales amount  is >= 80,000 US.
    -สร้าง Member set ที่ต้องการ Track ขึ้นมา โดยส่วนนี้ใช้ Filter ในการกรองข้อมูลที่ต้องการคื
     หาสินค้าที่ขายได้ตั้งแต่ 80,000 US ขึ้นไปในเดือน January 2003
2. Use defined member set in select section.

---------------- Result -------------------

Bikes Mountain-200 Silver, 38 January 2003 $106,885.25
Bikes Mountain-200 Silver, 38 February 2003 $127,185.17
Bikes Mountain-200 Silver, 38 March 2003 $116,828.07
Bikes Mountain-200 Silver, 42 January 2003 $85,451.03
Bikes Mountain-200 Silver, 42 February 2003 $125,528.03
Bikes Mountain-200 Silver, 42 March 2003 $91,556.75
Bikes Mountain-200 Silver, 46 January 2003 $83,271.07
Bikes Mountain-200 Silver, 46 February 2003 $131,328.01
Bikes Mountain-200 Silver, 46 March 2003 $120,970.91
Bikes Mountain-200 Black, 38 January 2003 $98,356.71
Bikes Mountain-200 Black, 38 February 2003 $180,320.64
Bikes Mountain-200 Black, 38 March 2003 $128,273.55
Bikes Mountain-200 Black, 42 January 2003 $106,962.92
Bikes Mountain-200 Black, 42 February 2003 $158,707.57
Bikes Mountain-200 Black, 42 March 2003 $100,815.63
Bikes Mountain-200 Black, 46 January 2003 $94,668.34
Bikes Mountain-200 Black, 46 February 2003 $135,650.30
Bikes Mountain-200 Black, 46 March 2003 $101,225.45
Bikes Road-250 Red, 44 January 2003 $84,539.91
Bikes Road-250 Red, 44 February 2003 $135,850.26
Bikes Road-250 Red, 44 March 2003 $111,416.76
Bikes Road-250 Red, 48 January 2003 $85,028.58
Bikes Road-250 Red, 48 February 2003 $152,465.04
Bikes Road-250 Red, 48 March 2003 $64,015.77
------------------------------------------------------------

วันพุธที่ 10 กุมภาพันธ์ พ.ศ. 2553

Impact of HierarchyUniqueNameStyle in Reporting Service parameters design

Determines how unique names are generated for hierarchies that are contained within the CubeDimension.

บทความนี้จะกล่าวถึงการ Design CUBE ในส่วนที่เกี่ยวกับ Dimension ซึ่งกระทบต่อการ Desing report
ในการ Desing Cube นั้นจะต้องมี Dimension เข้ามาเกี่ยวข้องด้วย ซึ่งจะมี Property ให้ Set มากมาย
แต่ปัญหาที่เคยพบเกี่ยวกับ Property ตัวหนึ่งคือ "HierarchyUniqueNameStyle"
Property ตัวนี้ประกอบด้วย
  • IncludeDimensionName (รวมชื่อ Dimension ใน Hierarchies)
  • ExcludeDimensionName (ไม่รวมชื่อ Dimension ใน Hierarchies)
ปกติแล้ว Default value คือ IncludeDimensionName อยู่แล้ว ซึ่งควรจะเป็นเช่นนั้น
ไม่แนะนำให้ Set เป็น ExcludeDimensionName เพราะจะมีปัญหาตามมาหากต้องการใช้
Front end tool ในการ Browse data
ตัวอย่างปัญหาที่เคยพบเกี่ยวกับ Property ตัวนี้ซึ่งกระทบกับ Report ที่เคย Design ไว้
- Cube ถูก Design ไว้ให้มีบาง Dimension property "HierarchyUniqueNameStyle" มีค่าเป็น ExcludeDimensionName
- การ Design Report มีการสร้าง Parameters ให้ User เลือก โดยมีการอ้างถึง Dimension เช่น Dimension Products (Dimension Products นั้นถูก Set ให้เป็น ExcludeDimensionName )
- มีความต้องการใช้ Front end tool (Analyzer 2007 : Strategy Companion) เชื่อมต่อกับ CUBE แต่เกิดปัญหาว่าไม่สามารถ Browse dimensions ที่มีการ Set property "HierarchyUniqueNameStyle" เป็นแบบ ExcludeDimensionName ได้
- เมื่อแก้ไข Cube property "HierarchyUniqueNameStyle" ให้เป็น IncludeDimensionName ปรากฎว่า Report ที่เคย Design ไว้นั้นทำงานผิดพลาดทั้งหมด กล่าวคือ Parameter ที่เคยอ้างถึง Dimension ที่เคยเป็น ExcludeDimensionName จะไม่แสดงข้อมูล
นอกจากนั้นความแตกต่างของ Property นี้จะเห็นได้จากตอนที่เขียน MDX Query
หาก Drag & Drop dimension attribue ลงใน Query plane text ตามตัวอย่าง

1 Cube Dimension property --> IncludeDimensionName
[Dim_Product].[Category].[Category]
2 Cube Dimension property --> ExcludeDimensionName
[Category].[Category]
How to set "HierarchyUniqueNameStyle" in cube designer
-Open cube structure
-Select dimension to set property
-Property appear in property pallet.

figure1 : HierarchyUniqueNameStyle in Cube Designer

MDX Query : Improve performance via "NonEmptyCrossjoin"

Returns a set that contains the cross product of one or more sets, excluding empty tuples and tuples without associated fact table data.

ค่าที่ได้จากการใช้ function นี้จะเป็นค่าที่ไม่รวมค่าว่างจากการ Cross join dimension
ซึ่งจะทำให้ QueryPerformance ดีขึ้นอย่างเห็นได้ชัด
ตัวอย่างจาก Adventure Works (OLAP)

Example 1 เป็นการทำ Dimension Cross ปกติ (Dimensions Crossjoin )

SELECT [Measures].[Order Count] ON COLUMNS,
CROSSJOIN([Sales Territory].[Sales Territory Country].[Sales Territory Country].ALLMEMBERS ,[Product].[Category].[Category].ALLMEMBERS)
ON ROWS
FROM [Adventure Works]



Figure 1 : Result for crossjoin


Example 2 เป็นการทำ NonEmptyCrossjoin Dimension

SELECT [Measures].[Order Count] ON COLUMNS,
NONEMPTYCROSSJOIN([Sales Territory].[Sales Territory Country].[Sales Territory Country].ALLMEMBERS ,[Product].[Category].[Category].ALLMEMBERS)
ON ROWS
FROM [Adventure Works]



Figure 2 : Result for NonEmptyCrossjoin

จากตัวอย่าง คือการ Query ข้อมูลการ Order สินค้า ตามประเทศ และตามประเภทสินค้า
จะเห็นได้ว่าใน Query แรกจะมีค่า Null ออกมาด้วย (ในกรอบสีแดง)
ซึ่งหมายความว่าใน Australia ไม่มีการสั่งสินค้าประเภท Motocycle นั่นเอง
เปรียบเทียบกับตัวอย่างที่สองที่จะไม่มีค่า Null ออกมา