(項目積累的)SQL數據庫點滴

最近的的系統用的數據庫是mssql,軟件mssql 2008 r2sql

一、存儲過程:後勤的綜合管理系統(後端內網訪問)三層架構配套用的是存儲過程,裏面列表展現的都是用存儲過程,以下:數據庫

1)數據庫腳本後端

 1 USE [ProjectDB]
 2 GO
 3 /****** Object:  StoredProcedure [dbo].[UP_NetworkBugInfo_List_select]    Script Date: 09/18/2016 21:44:33 ******/
 4 SET ANSI_NULLS ON
 5 GO
 6 SET QUOTED_IDENTIFIER ON
 7 GO  
10 -- =============================================
11 -- Author:類菌體
12 -- Create date: 2014-06-09
13 -- Description:    分頁查詢信息
14 -- =============================================
15 ALTER procedure [dbo].[UP_NetworkBugInfo_List_select]
16 @StartIndex int=1,--分頁開始序號
17 @EndIndex int=10,--分頁結束序號
18 @WhereSql nvarchar(2000)='',--分頁查詢條件
19 @RecordCount int=0 output --當前條件下的總記錄數
20 as
21 begin
22 declare @sql nvarchar(max)
23 declare @total int
24 
25 set @sql='select @n=count(1) from NetworkBugInfo a   where 1=1 '+@WhereSql
26 exec sp_executesql @sql, N'@n int output',@total output --@sql至關於procedure主體,N'@n int'至關於參數部分,至關於procedure調用
27 
28 set @RecordCount=@total
29 
30 set @sql='select * from
31             (select row_number() over(order by t1.NetworkBugId desc) as Row, t1.NetworkBugId,t1.InternetNumber,t1.Username,t1.BugType,t1.PhoneNumber,t1.ShortNumber,t1.Campus,t1.Building,t1.Room,t1.BugDetail,t1.SubmitTime,t1.State,t1.OnePrincipal,t1.SubmitIp,t1.OrderTimes,t1.UserNames,t1.bxfs from NetworkBugInfo t1
32             where 1=1 '+@WhereSql+'  ) a where Row between '+convert(varchar(100), @StartIndex)+' and '+convert(varchar(100), @EndIndex) 
33 exec(@sql) 
34 end

2)、主要操做數據庫的方法架構

 /// <summary>
        /// 根據條件分頁查詢信息
        /// </summary>
        /// <param name="pageIndex">當前頁碼</param>
        /// <param name="pageSize">每頁大小</param>
        /// <param name="whereSql">分頁查詢條件</param>
        /// <param name="recordCount">out參數,當前條件下的總記錄數</param>
        /// <returns>分頁查詢後的信息</returns>
        public DataTable GetList(int pageIndex, int pageSize, string whereSql, out int recordCount)
        {
            DataTable dt = null;
            int startIndex = pageIndex <= 1 ? 1 : ((pageIndex - 1) * pageSize + 1);
            int endIndex = startIndex + pageSize - 1;
            SqlParameter[] parms = { 
                                       new SqlParameter("@StartIndex",SqlDbType.Int),
                                       new SqlParameter("@EndIndex",SqlDbType.Int),
                                       new SqlParameter("@WhereSql",SqlDbType.NVarChar,2000),
                                       new SqlParameter("@RecordCount",SqlDbType.Int)
                                   };
            parms[0].Value = startIndex;
            parms[1].Value = endIndex;
            parms[2].Value = whereSql;
            parms[3].Direction = ParameterDirection.Output;
            dt = SQLHelper.GetDataTable(CommandType.StoredProcedure, "UP_NetworkBugInfo_List_select", parms);
            recordCount = Convert.ToInt32(parms[3].Value);
            return dt;
        }

 二、隨機查詢:有時候須要隨機選出一些數據。好比有一個後期回訪的活動,須要隨機抽取某些客戶的數據    sqlserver

order by newid()

select TOP 10 * from lybug where State=' 完成處理' order by   newid() ---含義表明隨機抽取10條記錄

三、查詢插入:主要使用好比通常從excel導入mssql都會直接導入造成一個表,導入方法可使用mssql2008r2,導進去記得轉換下數據類型造成一個表對象之後,能夠直接選擇該對象的字段插入到目標表記錄中ui

同步就業基地
insert into tb_sx_jdinfo(_mc,_jgm,_xxdz,_lxdh,_dwjj,_kfr,kfdw,jyjiid,sfzy,Cxqhzdq,compTypeID,compTradeID) select  b.compName,b.OrganizationCode,b.compAdress,b.compContactTel,b.compRemark,b.userCode,b.kfdw,b.compID,1,b.compCityID,b.compTypeID,b.compTradeID
from compJobState as t join newCompBasic as b on t.CompID=b.compID join newCompJob as j on t.jobID=j.jobID
where t.CountYear='2016' and b.IsBasic='1'

4、微軟sql server 儘可能使用sqlserver 2008r2版本,備份,導出數據等能夠選擇知導出結構,導出數據,導出結構和數據,在查詢窗體下查詢結果能夠選擇複製(帶表頭)spa

五、查詢重複,刪除重複 (只保留一條)excel

select * from tb_sx_jdinfo where _mc in (select   _mc from   tb_sx_jdinfo group by   _mc having count(_mc) > 1)
delete from tb_sx_jdinfo where _mc in (select   _mc from tb_sx_jdinfo group by   _mc   having count (_mc) > 1) and _dm not in (select min(_dm) from   tb_sx_jdinfo group by _mc having count(_mc)>1)

 六、查詢帳戶不爲空code

select loginid as 工號,lastname as  姓名 from HrmResource  where  isnull(loginid,'')<>''
相關文章
相關標籤/搜索