【機房重構】組合查詢--儲存過程
阿新 • • 發佈:2018-11-19
定義
儲存過程(Stored Procedure)是在大型資料庫系統中,一組為了完成特定功能的SQL 語句集,儲存在資料庫中,經過第一次編譯後再次呼叫不需要再次編譯,使用者通過指定儲存過程的名字並給出引數(如果該儲存過程帶有引數)來執行它。儲存過程是資料庫中的一個重要物件。
建立過程
- 開啟SQL Server
- 開啟資料庫
- 點選“可程式設計性”
- 新建“儲存過程”
機房重構的儲存過程程式碼
USE [YZY.Charge]
GO
/****** Object: StoredProcedure [dbo].[PROC_GroupCheck] Script Date: 18/9/23 17:49:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: 楊子潁
-- Create date: 2018-09-18
-- Description: 組合查詢進行資料互動
-- =============================================
ALTER PROCEDURE [dbo].[PROC_GroupCheck]
-- Add the parameters for the stored procedure here
/**** 需要傳遞的引數 ****/
@cmbFiled1 varchar(20),
@cmbFiled2 varchar(20),
@cmbFiled3 varchar(20),
@cmbMark1 varchar(20),
@cmbMark2 varchar(20),
@cmbMark3 varchar(20),
@cmbContext1 varchar(20),
@cmbContext2 varchar(20),
@cmbContext3 varchar(20),
@cmbRelationships1 varchar(20),
@cmbRelationships2 varchar(20),
@tableName varchar(20)
AS
declare @TempSql varchar(500) --臨時存放sql語句
--Char (32) 是空格 插入(39)是單引號
BEGIN
--一個條件的查詢
SET @TempSql='SELECT * FROM' [email protected]+'WHERE'[email protected][email protected]+char(39)[email protected]+char(39)
if(@cmbRelationships1!='')
BEGIN
--兩個條件的查詢
SET @[email protected][email protected]+char(32)[email protected][email protected]+char(39)[email protected]+char(39)
if(@cmbRelationships2!='')
BEGIN
--三個條件的查詢
SET @ [email protected][email protected]+char(32)[email protected][email protected]+char(39)[email protected]+char(39)
END
END
EXECUTE(@TempSql)
END
D層程式碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using Entity; using IDAL; namespace DAL { public class StuInfoDAL:IStuInfoIDAL { public DataTable SelectStudent(CombinedQuery student) { SQLHelper sqlhelper = new SQLHelper(); SqlParameter[] sqlParameter = { new SqlParameter("@cmbFiled1",student.CmbFiled1), new SqlParameter("@cmbFiled2",student.CmbFiled2), new SqlParameter("@cmbFiled3",student.CmbFiled3), new SqlParameter("@cmbMark1",student.CmbMark1), new SqlParameter("@cmbMark2",student.CmbMark2), new SqlParameter("@cmbMark3",student.CmbMark3), new SqlParameter("@cmbContext1",student.CmbContext1), new SqlParameter("@cmbContext2",student.CmbContext2), new SqlParameter("@cmbContext3",student.CmbContext3), new SqlParameter("@cmbRelationships1",student.CmbRelationships1), new SqlParameter("@cmbRelationships2",student.CmbRelationships2), new SqlParameter("@cmbtableName",student.CmbtableName) }; string sql = @"PROC_CombinedQuery"; DataTable dt = sqlhelper.ExecuteQuery(sql, sqlParameter, CommandType.StoredProcedure); return dt; } } }
優點
- 重複使用。
儲存過程可以重複使用,從而可以減少資料庫開發人員的工作量。 - 減少網路流量。
儲存過程位於伺服器上,呼叫的時候只需要傳遞儲存過程的名稱以及引數就可以了,因此降低了網路傳輸的資料量。 - 安全性。
引數化的儲存過程可以防止SQL注入式攻擊,而且可以將Grant、Deny以及Revoke許可權應用於儲存過程。
簡單講:
1.儲存過程只在創造時進行編譯,以後每次執行儲存過程都不需再重新編譯,而一般SQL語句每執行一次就編譯一次,所以使用儲存過程可提高資料庫執行速度。
2.當對資料庫進行復雜操作時(如對多個表進行Update,Insert,Query,Delete時),可將此複雜操作用儲存過程封裝起來與資料庫提供的事務處理結合一起使用。
3.儲存過程可以重複使用,可減少資料庫開發人員的工作量
4.安全性高,可設定只有某些使用者才具有對指定儲存過程的使用權