Common Table Expression 神奇的WITH()
在語法中可以用 WITH() 模擬成一個TABLE給另一段語法查詢
---------------------------------------------------------------------------------------------------
WITH TopSales (SalesID,numsales) AS
( select persionid,count(*) from salses group by persionID)
select * from TopSales where SalesID is not null
order by numsales
---------------------------------------------------------------------------------------------------
如果只是這樣會覺這有什麼好神奇的
但是如果撘配其他新的語法可以做到遞迴、分頁查詢等許多之前做不到的功能
遞迴範例
假設我有一個TABLE 是GROUUP 裡面有3個欄位
GroupID,ParentGroupID,groupName
0 null Root
1 0 sub1
2 1 sub11
3 0 sub2
4 2 sub21
語法如下:
---------------------------------------------------------------------------------------------------
With org AS
(select groupName,groupID ,1 as Level from group
where ParentGroupID is null
UNION ALL
select groupName,groupID , org.Level +1 as Level
from group
INNER JOIN org ON group.ParentGroupID= org .groupID
)
seelct * from org order by Level
---------------------------------------------------------------------------------------------------
上面的語法可以一次把所有GROUP以樹狀的方式一次列出來
結果如下
GroupID,groupName,Level
0 Root 1
1 sub1 2
3 sub2 2
2 sub11 3
4 sub21 3
下一集[如何用 WITH 來達成資料分頁的效果]
----------------------- To be continue -------------------------------
丫烈客 發表在 痞客邦 留言(1) 人氣(254)
今天有 5個業務 10月份的業績是
甲:15萬
乙:20萬
丙:30萬
丁:10萬
戊:15萬
老闆要找出業績前3名的業務
一般直覺的想法是
select top 3 業務名稱,金額 from 業績 order by 金額 desc
會得到
丙 30
乙 20
甲 15
也有可能會得到
但是今天有可能有n個第3名
所以用 top 3 應該不符合需求
如果改用
select top 3 WITH TIES 業務名稱,金額 from 業績 order by 金額 desc
會得到4筆資料
WITH TIES 就是用在這個時候 ,需撘配 TOP n 還有 ORDER BY
會讓 ORDER BY 最後一筆的結果一樣的DATA也出來
以上面的例子
因為 ORDER BY 金額 desc
所以最後一筆 是15 ,所以金額=15的也都會出現
而產生 top 3 但是會有 4筆資料的結果
丫烈客 發表在 痞客邦 留言(4) 人氣(4,493)
當我們要找出 A TABLE 的DATA 存在於 B TABLE 的資料 時
當A TABLE的KEY 只有 一個時通常都會
select * form A where APKColumn in (select APKColumn from B )
但是如果 A TABLE 的KEY 有兩個的時候就很難用上面的語法來找出來
這時候 EXISTS 就派上用場
select * from A
where exists (select * from B where B.APKColumn1 =A.APKColumn1 AND B.APKColumn2 =A.APKColumn2 )
丫烈客 發表在 痞客邦 留言(1) 人氣(1,516)
解決了檔案上傳的問題,接著來的是檔案下載
ASP.NET 如果檔案太大時,會佔用很多的記憶體,甚至會掛掉,或者在 下載時沒有發生錯誤,但是在完成後確 發生 The page cannot be displayed 之類的錯誤
參考作法 來自
Optimizing the Downloading of Large Files in ASP.NET using( Stream s = new FileStream( fileName, FileMode.Open,
FileAccess.Read, FileShare.Read, bufferSize ) )
{
byte[] buffer = new byte[bufferSize];
int count = 0;
int offset = 0;
while( (count = s.Read( buffer, offset, buffer.Length ) ) > 0 )
{
ctx.Response.OutputStream.Write( buffer, offset, count );
}
}
或者 Microsoft 的KB
http://support.microsoft.com/kb/812406/EN-US/不過也有可能是 BUG (已經在 .Net Framework 1.1 SP1 中修覆)
http://support.microsoft.com/kb/823409/EN-US/ 丫烈客 發表在 痞客邦 留言(6) 人氣(432)
丫烈客 發表在 痞客邦 留言(0) 人氣(159)
記得在某次教育訓練時,講師有提到
SQL connection 的處理 一般人都不會注意 connection 會因為程式錯誤而造成沒有close 的問題
當時建議
用 try finally 來解決.
try{
connection.open();
}
finally
{
connection.close();
}
現在發現更簡單的語法, 用 using 也可以達成同樣的效果
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.open();
}
底下說明來自MSDN
C# 程式語言的 using 陳述式會藉由簡化您必須為建立和清除物件而撰寫的程式碼,以較為自動的方式呼叫 Dispose 方法。Using 陳述式會取得一項或多項資源、執行您指定的陳述式,然後處置該物件。請注意,using 陳述式對於物件之存留期不超過建構該物件之方法的物件才有用。下列程式碼範例會建立並清除 ResourceWrapper 類別的執行個體,一如之前在 C# 實作 Dispose 方法範例中所提供的說明。
class myApp
{
public static void Main()
{
using (ResourceWrapper r1 = new ResourceWrapper())
{
// Do something with the object.
r1.DoSomething();
}
}
}
前面含有 using 陳述式的程式碼,就相當於下列情形。
class myApp
{
public static void Main()
{
ResourceWrapper r1 = new ResourceWrapper();
try
{
// Do something with the object.
r1.DoSomething();
}
finally
{
// Check for a null resource.
if (r1 != null)
// Call the object's Dispose method.
r1.Dispose();
}
}
}
丫烈客 發表在 痞客邦 留言(0) 人氣(6,544)