(项目积累的)SQL数据库点滴
最近的的系统用的数据库是mssql,软件mssql 2008 r2
1、存储过程:后勤的综合管理系统(后端内网访问)三层架构配套用的是存储过程,里面列表展示的都是用存储过程,如下:
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; }
2、随机查询:有时候需要随机选出一些数据。比如有一个后期回访的活动,需要随机抽取某些客户的数据
order by newid() select TOP 10 * from lybug where State=' 完成处理' order by newid() ---含义代表随机抽取10条记录
3、查询插入:主要使用比如一般从excel导入mssql都会直接导入形成一个表,导入方法可以使用mssql2008r2,导进去记得转换下数据类型形成一个表对象以后,可以直接选择该对象的字段插入到目标表记录中
同步就业基地 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版本,备份,导出数据等可以选择知导出结构,导出数据,导出结构和数据,在查询窗体下查询结果可以选择复制(带表头)
5、查询重复,删除重复 (只保留一条)
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)
6、查询账户不为空
select loginid as 工号,lastname as 姓名 from HrmResource where isnull(loginid,'')<>''