用SQL完成條件式查詢

來源:互聯網
上載者:User

因為覺得每次要用條件式查詢很煩(如範例一),就想有沒有更方便的用法,試出一種方法還不錯,分享給大家。

測試環境

DBMS:Sql Server 2008 Express

DB:AdventureWorks

Table:Sales.SalesOrderDetail(Count:121,317)

 情境

對Sales.SalesOrderDetail的OrderId與ProduceId與SaleDate這三個欄位做查詢(如圖一),如果沒有輸入條件為顯示全部。

 

圖一 查詢畫面

 

範例一 傳統的條件式
   1:  StringBuilder sb = new StringBuilder();
   2:   
   3:  if (!string.IsNullOrEmpty(Order.Text))
   4:      sb.AppendFormat("SalesOrderID='{0}'", Order.Text);
   5:   
   6:  if (!string.IsNullOrEmpty(Produce.Text))
   7:  {
   8:      if (sb.Length > 0)
   9:          sb.Append(" AND ");
  10:   
  11:      sb.AppendFormat("ProductID='{0}'", Produce.Text);
  12:  }
  13:   
  14:  if (!string.IsNullOrEmpty(StartDate.Text))
  15:  {
  16:      if (sb.Length > 0)
  17:          sb.Append(" AND ");
  18:   
  19:      sb.AppendFormat("ModifiedDate > '{0}'", StartDate.Text);
  20:  }
  21:   
  22:  if (!string.IsNullOrEmpty(EndDate.Text))
  23:  {
  24:      if (sb.Length > 0)
  25:          sb.Append(" AND ");
  26:   
  27:      sb.AppendFormat("ModifiedDate < '{0}'", EndDate.Text);
  28:  }
  29:   
  30:  if (sb.Length > 0)
  31:      sb.Insert(0, " WHERE ");
  32:   
  33:  sb.Insert(0,"SELECT * FROM Sales.SalesOrderDetail");

如果是以前的我,我會這樣寫,落落長看的都累了,下一個範例展表直接在SQL中作掉。

 

範例二 更方便的寫法
   1:  string sql = string.Format("SELECT * FROM Sales.SalesOrderDetail WHERE" +
   2:                             "('{0}' = '' OR SalesOrderID = '{0}') AND " +
   3:                             "('{1}' = '' OR ProductID = '{1}') AND " +
   4:                             "('{2}' = '' OR ModifiedDate > '{2}') AND " +
   5:                             "('{3}' = '' OR ModifiedDate < '{3}')",
   6:                             Order.Text, Produce.Text, StartDate.Text, EndDate.Text);

這個方法是不是寫起來簡單,看起來乾淨,其作用為('' = '' OR SalesOrderID = '')參數是空的不查詢,('12345' = '' OR SalesOrderID = '12345')參數不是空的執行查詢。

 

應用一 SqlDataSource中使用
   1:  <asp:SqlDataSource ID="SqlDataSource" runat="server" CancelSelectOnNullParameter="false"
   2:  SelectCommand="SELECT * FROM Sales.SalesOrderDetail WHERE
   3:                (@Order IS NULL OR SalesOrderID = @Order) AND 
   4:                (@Produce IS NULL OR ProductID = @Produce) AND 
   5:                (@StartDate IS NULL OR ModifiedDate > @StartDate) AND 
   6:                (@EndDate IS NULL OR ModifiedDate < @EndDate)">
   7:  <SelectParameters>
   8:      <asp:ControlParameter ControlID="Order" Name="Order" ConvertEmptyStringToNull="true" />
   9:      <asp:ControlParameter ControlID="Produce" Name="Produce" ConvertEmptyStringToNull="true"/>
  10:      <asp:ControlParameter ControlID="StartDate" Name="StartDate" ConvertEmptyStringToNull="true"/>
  11:      <asp:ControlParameter ControlID="EndDate" Name="EndDate" ConvertEmptyStringToNull="true"/>
  12:  </SelectParameters>
  13:  </asp:SqlDataSource>

用這個方式,在DataSource中也可以做到DataSource條件式查詢,使用CancelSelectOnNullParameter=”false”,不管有沒有結果都要執行,因為設定ConvertEmptyStringToNull="true",所以SQL中也可以使用(@Order IS NULL OR SalesOrderID = @Order)

 

應用二 StoreProcedure中使用
   1:  CREATE PROCEDURE SearchSalesOrderDetail
   2:      @Order varchar(10) = null ,
   3:      @Produce varchar(10) = null,
   4:      @StartDate datetime = null,
   5:      @EndDate datetime = null
   6:  AS
   7:  BEGIN
   8:  SELECT * FROM Sales.SalesOrderDetail WHERE
   9:  (@Order IS NULL OR SalesOrderID = @Order) AND 
  10:  (@Produce IS NULL OR ProductID = @Produce) AND 
  11:  (@StartDate IS NULL OR ModifiedDate > @StartDate) AND 
  12:  (@EndDate IS NULL OR ModifiedDate < @EndDate)
  13:  END
  14:  GO

當然也可以寫成Store Procedure直接在資料庫操作。

 

效能測試

各位最擔心的還是效能,就測試結果來說效能比較差,不過這樣物件導向是一樣的,為了方便多少會犧生一些效能,能不能接受,就看各位了。

 

EXEC dbo.SearchSalesOrderDetail NULL,NULL,NULL,NULL

 

 

 

 

SELECT * FROM Sales.SalesOrderDetail

 

 

EXEC dbo.SearchSalesOrderDetail '43659','776'

 

SELECT * FROM Sales.SalesOrderDetail WHERE SalesOrderID = '43659' AND ProductID = '776'

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.