2010年6月27日 星期日

SQL Server MINUS and INTERSECT

SQL Server 2005 後,提供EXCEP 和INTERSECT, 功能和ORACLE的MINUS, INTERSECT相同

EXCEP 找出存在於第一個子集,但不存在於第二個子集的筆數
INTERSECT 找出皆存在於二個子集中的筆數

這二個方法很方便查找有多個欄位的二個子集差異


以下是SQL 2008 ONLINE HELP說明
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/b1019300-171a-4a1a-854f-e1e751de3565.htm

The following query returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand.

USE AdventureWorks;
GO
SELECT ProductID
FROM Production.Product
INTERSECT
SELECT ProductID
FROM Production.WorkOrder ;
--Result: 238 Rows (products that have work orders)


The following query returns any distinct values from the query to the left of the EXCEPT operand that are not also found on the right query.

USE AdventureWorks;
GO
SELECT ProductID
FROM Production.Product
EXCEPT
SELECT ProductID
FROM Production.WorkOrder ;
--Result: 266 Rows (products without work orders)

以前沒有這種比較的需求, 也沒想過會有EXCEPT,INTERSECT這種語法想了一下,使用left outer join , right outer join  ,inner join 或exists 可以做出同樣的結果嗎???

簡單的測試....., select 的欄位中,含有NULL 值, 則預期結果大不相同

--測試資料
select * into #test1 from
(select  'A11' as col1 , cast(null  as varchar(10))  col2 ,'A31'  col3
union all
select 'A21', null ,'A33'
) a
select * into #test2 from
(select  'A11' as col1 , 'A12'  col2 ,'A31'  col3
union all
select 'A21', null ,'A33'
) a

--EXCEPT
select * from #test1
except
select * from #test2

--回傳結果找出一筆不同
--col1    col2    col3
--A11    NULL    A31

--LEFT OUTER JOIN
select *
from #test1 a left outer join #test2  b
   on a.col1 = b.col1
and a.col2 = b.col2
and a.col3 = b.col3

--回傳結果找出二筆 ,若過濾 掉 b.col1 is null, 則無符合筆數
--A11    NULL    A31    NULL    NULL    NULL
--A21    NULL    A33    NULL    NULL    NULL

轉貼 SQL Server Hints

MSSQLTIPS
Using Hints To Test Indexes in SQL Server

強迫使用某一index查看查詢效能變化, 主要是觀察execution plan 中, query cost是否有顯著的下降

使用方式:

SELECT NationalIDNumber
FROM HumanResources.Employee
WITH (INDEX (PK_Employee_EmployeeID))
WHERE Title = 'Stocker'

2010年6月24日 星期四

安裝 SQL Server 2008 R2 的硬體和軟體需求

唉......
永遠都搞不清楚微軟各項產品的版本適合安裝在什麼樣的作業系統

關於SQL SERVER 2008 R2 各版本可安裝的作業系統,可參考 MSDN 說明

我關注這點

SQL Server 2008 R2 Enterprise (64 位元) x64
處理器類型:
•最小值:AMD Opteron、AMD Athlon 64、具有 Intel EM64T 支援的 Intel Xeon、具有 EM64T 支援的 Intel Pentium IV

處理器速度:
•最小值:1.4 GHz
•建議值:2.0 GHz 以上

作業系統
Windows Server 2003 SP2 64 位元 x64 Datacenter
Windows Server 2003 SP2 64 位元 x64 Enterprise
Windows Server 2003 SP2 64 位元 x64 Standard
Windows Server 2003 SP2 64 位元 x64 Web
Windows Server 2008 SP2 64 位元 x64 Datacenter
Windows Server 2008 SP2 64 位元 x64 Datacenter (不含 Hyper-V)
Windows Server 2008 SP2 64 位元 x64 Enterprise
Windows Server 2008 SP2 64 位元 x64 Enterprise (不含 Hyper-V)
Windows Server 2008 SP2 64 位元 x64 Standard
Windows Server 2008 SP2 64 位元 x64 Standard (不含 Hyper-V)
Windows Server 2008 SP2 64 位元 x64 Web
Windows 2008 R2 64 位元 x64 Datacenter
Windows 2008 R2 64 位元 x64 Enterprise
Windows 2008 R2 64 位元 x64 Standard
Windows 2008 R2 64 位元 x64 Web
Windows Server 2008 R2 x64 for Windows Essential Server Solutions

關於SQL Server 2008 R2各版本提供的功能差異,請參考MSDN說明

版本真夠多的。。。。 

2010年6月21日 星期一

tar指令

windows telnet 環境下,無法使用zip指令
切換回ssh 模式,改用tar來產生壓縮檔

tar -cvf filename.tar file.bak

其中 filename.tar 為要產生的壓縮檔名
file.bak 為被壓縮的原始檔名

AS 2000 ,KB 901200

利用OPENQUERY執行Analysis Server 2000 MDX select 時,會有類似下列的錯誤訊息

伺服器: 訊息 7358 層級 16,狀態 1 行 1



無法執行查詢。OLE DB 提供者 'MSOLAP' 未提供適當的介面來存取文字、 ntext 或 image 資料行 '[MSOLAP] [儲存] 儲存區 [縣/市] [MEMBER_CAPTION]'。


OLE DB 錯誤追蹤 [非介面時發生錯誤: 無法取得儲存體介面 BLOB 的資料行: ProviderName = 'MSOLAP' 表格名稱 = '[MSOLAP]' ColumnName = ' [儲存] 儲存區 [縣/市] [MEMBER_CAPTION] ']。

詳細內容參考 KB 901200, http://support.microsoft.com/kb/901200

如KB所言,此問題在 安裝 Microsoft SQL Server 2000 Service Pack 4 (SP 4) 之後會發生

更新後SQL2000版本為
Microsoft SQL Server  2000 - 8.00.2148 (Intel X86)   Jul  7 2005 20:33:10   Copyright (c) 1988-2003 Microsoft Corporation  Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)

2010年6月8日 星期二

SSIS dtexec 參數指令

dtsexec /?
command line 列出常用的選項

選項不區分大小寫。連字號 (-) 可以用來取代斜線 (/)。
/Conf[igFile]       Filespec (把同一個project會用到的設定都維護在.dtsConfig)
/De[crypt]          Password (package設EncryptSensitiveWithPassword,執行時再指定解密密碼)
/F[ile]             Filespec 
/Set                PropertyPath;Value


my command line:

DTEXEC /FILE "C:\Test.dtsx" /DECRYPT "test2010" /CONFIGFILE "C:\test.dtsConfig"   /SET "\package.variables[gsGuid].Value";"54A2A383-28A6-4227-AA9F-21A8BDFEB853" /SET "\package.variables[gsType].Value";"typeI"




其中,gsGuid是用來寫入sql server uniqueidentifier欄位, 試了一下, 參數前後加大括弧也OK


DTEXEC /FILE "C:\Test.dtsx" /DECRYPT "test2010" /CONFIGFILE "C:\test.dtsConfig"   /SET "\package.variables[gsGuid].Value";"{54A2A383-28A6-4227-AA9F-21A8BDFEB853}" /SET "\package.variables[gsType].Value";"typeI"

不過.....如果這個gsGuid值,要同時給不同DB使用, 則在傳入時,就不要傳入有加大括弧的參數值,以免非SQL SERVER DB資料庫寫入時,發生截斷的問題....因為長度就會超過varchar(36)了.







SSIS 字串轉型成GUID

在SSIS dataflow task 的 drived column  ,將字串轉型為GUID時,前後要加大括弧

(DT_GUID)("{" + @[User::gsGuid] + "}")

VS CODE離線安裝套件方式-續

去年 vscode離線安裝 powershell nupkg 今年在一台無外網的 windwos 2025上安裝時遇到了powershell版本一直讀取到了5.1的版本,鬼打牆了一下午,在VM還原重測測了好幾回,應該是可行了。 將下載到的免安裝powershell 解壓縮到...