(项目积累的)SQL数据库点滴

最近的的系统用的数据库是mssql,软件mssql 2008 r2

1、存储过程:后勤的综合管理系统(后端内网访问)三层架构配套用的是存储过程,里面列表展示的都是用存储过程,如下:

1)数据库脚本

 USE [ProjectDB]
GO
/****** Object: StoredProcedure [dbo].[UP_NetworkBugInfo_List_select] Script Date: 09/18/2016 21:44:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:类菌体
-- Create date: --
-- Description: 分页查询信息
-- =============================================
ALTER procedure [dbo].[UP_NetworkBugInfo_List_select]
@StartIndex int=,--分页开始序号
@EndIndex int=,--分页结束序号
@WhereSql nvarchar()='',--分页查询条件
@RecordCount int= output --当前条件下的总记录数
as
begin
declare @sql nvarchar(max)
declare @total int set @sql='select @n=count(1) from NetworkBugInfo a where 1=1 '+@WhereSql
exec sp_executesql @sql, N'@n int output',@total output --@sql相当于procedure主体,N'@n int'相当于参数部分,相当于procedure调用 set @RecordCount=@total set @sql='select * from
(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
where = '+@WhereSql+' ) a where Row between '+convert(varchar(100), @StartIndex)+' and '+convert(varchar(100), @EndIndex)
exec(@sql)
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 <= ? : ((pageIndex - ) * pageSize + );
int endIndex = startIndex + pageSize - ;
SqlParameter[] parms = {
new SqlParameter("@StartIndex",SqlDbType.Int),
new SqlParameter("@EndIndex",SqlDbType.Int),
new SqlParameter("@WhereSql",SqlDbType.NVarChar,),
new SqlParameter("@RecordCount",SqlDbType.Int)
};
parms[].Value = startIndex;
parms[].Value = endIndex;
parms[].Value = whereSql;
parms[].Direction = ParameterDirection.Output;
dt = SQLHelper.GetDataTable(CommandType.StoredProcedure, "UP_NetworkBugInfo_List_select", parms);
recordCount = Convert.ToInt32(parms[].Value);
return dt;
}

2、随机查询:有时候需要随机选出一些数据。比如有一个后期回访的活动,需要随机抽取某些客户的数据

order by newid()

select TOP  * 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,,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='' and b.IsBasic=''

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) > )
delete from tb_sx_jdinfo where _mc in (select   _mc from tb_sx_jdinfo group by   _mc   having count (_mc) > ) and _dm not in (select min(_dm) from   tb_sx_jdinfo group by _mc having count(_mc)>)

6、查询账户不为空

select loginid as 工号,lastname as  姓名 from HrmResource  where  isnull(loginid,'')<>''
上一篇:mac3.0环境搭建


下一篇:Excel催化剂开源第13波-VSTO开发之DataGridView控件几个小坑