1. 程式人生 > >Entity SQL 初入

Entity SQL 初入

通過 creat case語句 eve ado ceiling 沖突 als 轉換

Entity SQL 是 ADO.NET 實體框架 提供的 SQL 類語言,用於支持 實體數據模型 (EDM)。Entity SQL 可用於對象查詢和使用 EntityClient 提供程序執行的查詢。

> 關鍵字

Value關鍵字

ESQL 提供了 SELECT VALUE 子句以跳過隱式行構造。SELECT VALUE 子句中只能指定一項。在使用這樣的子句時,將不會對 SELECT 子句中的項構造行包裝器,並且可生成所要形狀的集合,例如:SELECT VALUE it FROM NorthwindEntities.Customers as it

it關鍵字

it 出現在 ESQL 中, 查詢對象的別名默認值 "it" 改成其他字符串,例如:
"SELECT VALUE it FROM NorthwindEntities.Customers as it " 。

> 註釋:

Entity SQL 查詢可以包含註釋。註釋行以兩個短劃線 (--) 開頭。
"SELECT VALUE it FROM NorthwindEntities.Customers as it -- this a comment "

> Select查詢

例如:
SELECT VALUE it FROM NorthwindEntities.Customers as it

> 參數

參數是在esql之外定義的變量,每個參數都有名稱和類型,參數名稱在查詢表達式中定義,[email protected]
Select VALUE c from NorthwindEntities.Customers as c where [email protected]

/* */

> 聚合

Enity SQL不支持 * ,所以esql不支持count(*),而是使用count(0),例如:
Select count(0) from NorthwindEntities.Customers

> 分頁SKIP/LIMIT

可以通過在 ORDER BY 子句中使用 SKIP 和 LIMIT 子子句執行物理分頁。若要以確定的方式執行物理分頁,應使用 SKIP 和 LIMIT。如果您只是希望以非確定的方式限制結果中的行數,則應使用 TOP。TOP 和 SKIP/LIMIT 是互斥的
使用SKIP/LIMIT分頁,esql代碼如下:
Select value c from NorthwindEntities.Customers as c order by c.CustomerID skip 0 limit 10

> TOP

SELECT 子句可以在可選的 ALL/DISTINCT 修飾符之後具有可選的 TOP 子子句。TOP 子子句指定查詢結果中將只返回第一組行。esql代碼如下:
Select top(10) c.CustomerID from NorthwindEntities.Customers as c order by c.CustomerID

> NULL處理

Null 文本與 Entity SQL 類型系統中的任何類型都兼容,可以使用cast進行類型轉換,例如:
select cast(c.region as string) from NorthwindEntities.Customers as c order by c.CustomerID limit 10
其中, Nvarchar等可以成string,數字類型可以轉成int32,其他的類型轉換類似。如果無法完成轉換,則將報異常。還有可以處理的方法有treat。

> 標識符

Entity SQL 提供兩種標識符:簡單標識符和帶引號的標識符
簡單標識符:Entity SQL 中的簡單標識符是字母數字和下劃線字符的序列。標識符的第一個字符必須是字母字符(a-z 或 A-Z)。

帶引號的標識符:帶引號的標識符是括在方括號 ([]) 中的任何字符序列。帶中文的部分,請使用方括號包括起來,否則會報如下異常信息:“簡單標識符“中文”只能包含基本拉丁字符。若要使用UNICODE 字符,請使用轉義標識符”

正確的代碼如下:
Select c.CustomerID as [中文字符] from NorthwindEntities.Customers as c order by c.CustomerID skip 0 limit 10

> ROW

Esql可使用row來構建匿名的結構類型的紀錄。例如:
SELECT VALUE row(p.ProductID as ProductID,p.ProductName as ProductName) FROM NorthwindEntities.Products as p order by p.ProductID LIMIT 10

> Key

提取引用或實體表達式的鍵。如下esql語句,直接返回Customer表的主鍵:
string esql = "SELECT value key(c) FROM NorthwindEntities.Customers as c order by c.CustomerID LIMIT 10"

> CreateRef/ref/deref

CreateRef創建對實體集中的實體的引用。
ref返回對實體實例的引用,之後就可以當作實體來訪問其屬性,esql語句如下:
SELECT ref(c).CustomerID FROM NorthwindEntities.Customers as c order by c.CustomerID LIMIT 10
deref運算符取消引用一個引用值,並生成該取消引用的結果。

> CASE語句:

string esql = "using SqlServer;select case when len(trim(c.CustomerID))==0 then true else false end from NorthwindEntities.Customers as c order by c.CustomerID limit 10";

> 運算符

Esql支持的運算符有:加+、減-、乘*、除/、取模%、-負號。Esql語句如下:
select 100/2 as OP from NorthwindEntities.Customers as c order by c.CustomerID limit 10

> 比較運算符

Esql支持的比較運算符有:=,>,>=,IS [NOT] NULL,<,[NOT] BETWEEN,!=,<>,[NOT] LIKE。Esql語句如下:
select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 order by p.ProductID limit 10

> 邏輯運算符

Esql支持的邏輯運算符有:and(&&),not(!),or(||)。Esql語句如下:
select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 and p.UnitPrice<100 order by p.ProductID limit 10

select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 && p.UnitPrice<100 order by p.ProductID limit 10

> 字符串連接運算符

加號 (+) 是 Entity SQL 中可將字符串串聯起來的唯一運算符。Esql語句如下:
select c.CustomerID + c.ContactName from NorthwindEntities.Customers as c order by c.CustomerID limit 10

> 嵌套查詢

在 Entity SQL 中,嵌套查詢必須括在括號中,將不保留嵌套查詢的順序
select c1.CustomerID from( select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10) as c1

> 日期時間函數

Esql提供的日期時間函數有:CurrentDateTime()獲取當前服務器的日期時間,還有month,day,year,second, Minute ,Hour等。例如:
select CurrentDateTime() from NorthwindEntities.Customers as c order by c.CustomerID limit 10

> 字符串函數

Esql提供的字符串函數有:Concat,IndexOf,Left,Length,Ltrim,Replace,Reverse,Rtrim,SubString,Trim,ToLower,ToUpper.例如:
select Reverse(p.ProductName) as ProductName from NorthwindEntities.Products as p order by p.ProductID limit 10

> GUID

Esql提供newguid()函數,產生一個新的Guid。例如:
select newguid() from NorthwindEntities.Customers as c order by c.CustomerID limit 10

> 數學函數:

Abs,Ceiling,Floor,Round

> 統計函數:

Avg,BigCount,Count,Max,Min,StDev,Sum

> 位計算函數

如果提供 Null 輸入,則這些函數返回 Null。這些函數的返回類型與參數類型相同。如果函數采用多個參數,則這些參數必須具有相同的類型。若要對不同類型執行位運算,則需要顯式強制轉換為相同類型.
BitWiseAnd,BitWiseNot,BitWiseOr,BitWiseXor

> 命名空間

Entity SQL 引入命名空間以避免全局標識符(如類型名稱、實體集、函數等)出現名稱沖突。Entity SQL 中的命名空間支持與 .NET Framework 中的命名空間支持類似。
Entity SQL 提供兩種形式的 USING 子句:限定命名空間(其中,提供較短的別名以表示命名空間)和非限定命名空間,如下例所示:
USING System.Data;
USING tsql = System.Data;

例如:
string esql = "using System; select cast(p.UnitPrice as Int32) from NorthwindEntities.Products as p order by p.ProductID limit 10 ";
string esql = "using System;using SqlServer; select (cast(p.UnitPrice as Int32)),SqlServer.ltrim(p.ProductName) as nameLen from NorthwindEntities.Products as p order by p.ProductID limit 10 ";

最後,簡單說一下Esql與T-Sql的某些差異:

> Entity SQL 中的所有列引用都必須用表別名限定.

> Esql不支持Any,all限定運算符以及*運算

> Entity SQL 當前未提供對 DML 語句(insert、update、delete)的支持。

> Entity SQL 的當前版本未提供對 DDL 的支持。

Entity SQL 初入