SQL Server个人笔记(1)

SQL Server个人笔记1

s0320

–SQLServer 行转列(把自定义表的内容字段 先获取 再把数据储存在数据库指定的表中) t、d、a 为对应的虚拟表

// select * from Form_CustomCommon 
  SELECT *
        FROM 
        (
            select parent_ID,Name,StringValue
            from 
            (select * from 
                dbo.parseJSON('
                    {"Ability_A":[
                            {"A1":"1","A2":"确认工作程序","A3":"确认各成员的工作分配情况与工作安排,明确是否有效的进行分工合作。","A4":"4","A5":"1","A6":"5","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"2","A2":"推进情况的确认","A3":"经常确认业务是否进展顺利,并向上级商谈各种可能存在的隐患。","A4":"4","A5":"2","A6":"2","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"3","A2":"风险应对","A3":"在发现纠纷初期时,找到既定的解决方案并及时向上级汇报,接受指示做到尽早解决问题。","A4":"4","A5":"3","A6":"2","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"4","A2":"准确完成工作","A3":"依照公司规程与操作手册正确地推进业务,理解为什么使用这些方法能使工作进展顺利。","A4":"4","A5":"2","A6":"3","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"5","A2":"报告・联络・协商","A3":"对指示的内容进行正确记录的同时,做好报告,联络,协商。","A4":"4","A5":"2","A6":"2","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"6","A2":"发挥基本技能","A3":"根据基本的礼仪与常识,发挥必要的OA活用技术与文书写作技能。","A4":"4","A5":"3","A6":"3","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"7","A2":"理解所担当的业务","A3":"平常意识到担当者对组织以及社会的影响,明确按照规定执行业务的重要性。","A4":"4","A5":"3","A6":"3","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"8","A2":"检验活动成果","A3":"在上司建议的基础上,评价自己目标达成度的同时,找到自己所需要改善的地方并明确下次的课题。","A4":"4","A5":"1","A6":"1","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"9","A2":"提高工作完成水平","A3":"在学会业务上所必须的知识和技能的同时,提高自己的强项。","A4":"4","A5":"3","A6":"4","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""},
                            {"A1":"10","A2":"形成团队合作精神","A3":"把握成员的业务完成情况,积极主动地询问员工是否需要协助。","A4":"4","A5":"1","A6":"1","A7":"","A8":"","A9":"","A10":"","A11":"","A12":"","A13":""}
                            ],
                    "Ability_CurrentTotal":[
                    {"TotalScoreA5":2.1,"TotalScoreA6":2.6,"TotalScoreA7":null,"TotalScoreA8":null,"TotalScoreA9":null,"TotalScoreA10":null,"TotalScoreA11":null,"TotalScoreA12":null,"TotalScoreA13":null}
                    ],"Bark1":26,"Bark2":"","Bark3":"","Bark4":"","Bark5":"","Bark6":"","Bark7":"","Bark8":""}'
                    )
            ) t
                where name in('A1','A2','A3','A4','A5','A6','A7','A8','A9','A10','A11','A12','A13')
        ) d
    --行转列语句  
pivot( MAX(StringValue) FOR Name IN (A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13)) a 

查询-表里的某一字段且放在最前面

–select 字段1,* from 字段对应的表 ;

// select     PersonalEmail,* from hr_Staff_PersonalInfo

备份-某一字段

select * into 字段120190320 from 字段1

// select * into a1 from a 

更新-把表中的某一字段更新成自己想要的字段

update A1 set A1表中的字段= ‘xxx’ xxx代表你想把这一字段换成固定的一个值或者字段

// update hr_Staff_PersonalInfo set PersonalEmail= '[email protected]'

s0322 行转列

行转列实列仅提供参考

SELECT 
      s.StaffNo,s.StaffName,S.Grade,S.League,S.Category,S.Rank,H.Type,
      MAX(CASE LeaveBenefitCode WHEN 'SL' THEN TotalNoOfHours ELSE 0 END) AS 'SL',
      MAX(CASE LeaveBenefitCode WHEN 'UL' THEN TotalNoOfHours ELSE 0 END) AS 'UL',
      MAX(CASE LeaveBenefitCode WHEN 'NAL' THEN TotalNoOfHours ELSE 0 END) AS 'NAL',
      MAX(CASE LeaveBenefitCode WHEN 'OL' THEN TotalNoOfHours ELSE 0 END) AS 'OL',
      MAX(CASE LeaveBenefitCode WHEN 'CJJ' THEN TotalNoOfHours ELSE 0 END)AS 'CJJ',
      MAX(CASE LeaveBenefitCode WHEN 'LL' THEN TotalNoOfHours ELSE 0 END)AS 'LL',
      MAX(CASE LeaveBenefitCode WHEN 'CC' THEN TotalNoOfHours ELSE 0 END)AS 'CC',
      MAX(CASE LeaveBenefitCode WHEN 'PX' THEN TotalNoOfHours ELSE 0 END)AS 'PX',
      MAX(CASE LeaveBenefitCode WHEN 'ML' THEN TotalNoOfHours ELSE 0 END)AS 'ML',
      MAX(CASE LeaveBenefitCode WHEN 'GL' THEN TotalNoOfHours ELSE 0 END)AS 'GL',
      MAX(CASE LeaveBenefitCode WHEN 'LCJ' THEN TotalNoOfHours ELSE 0 END)AS 'LCJ',
      MAX(CASE LeaveBenefitCode WHEN 'PL' THEN TotalNoOfHours ELSE 0 END)AS 'PL',
      MAX(CASE LeaveBenefitCode WHEN 'FL' THEN TotalNoOfHours ELSE 0 END)AS 'FL'
FROM dbo.Leave_Transaction L
LEFT JOIN hr_staff s on s.StaffNo=L.StaffNo
LEFT JOIN Hr_Contact H ON H.StaffNo=s.StaffNo
GROUP BY s.StaffNo,s.StaffName,S.Grade,S.League,S.Category,S.Rank,H.Type

s0401

a.解决表中没有数据 字段翻译 问题 翻译后需要在web服务器端 结束W3w.exe 进程 (Ctrl+Shift+Delete 切记会影响正在进行中考勤计算,考勤分析等操作)

//select * from hr_Language  where StringID= 'STR_ViewShiftDetail'
insert into  hr_Language(StringID,Chinese,English) values('STR_ViewShiftDetail','查看排版明细','STR_ViewShiftDetail')

b.解决组织架构职位树人员被锁定的问题

先查找职位数表 然后按岗位编码查询

select * from Org_Position where Description = 'P007'
select * from Org_Position where IsLock=1
//update Org_Position set IsLock=0 where  Guid=
update Org_Position set IsLock=0 where IsLock=1

s0403

计算时间修订 --> 考勤时间明细 /* 用于解决菜单名称改动 */
–select * from 表名 where 字段名=‘部门A’(表中的)
–update 表名 set 字段名=‘更正后的名’ where 字段名=‘更正前的名’;

select * from Sys_Module where Menu_Chinese='计算时间修订';

update Sys_Module set Menu_Chinese='考勤时间明细'  where Menu_Chinese='计算时间修订';  

s0506

向另一个数据库的相同表中插入数据

insert into 表A1 select * from 数据库A.[dbo].A1
--insert into Pay_ReportHeader select * from V10_Product_Demo9020.[dbo].Pay_ReportHeader

遇到问题请参考:https://mp.csdn.net/mdeditor/90769800#

s0507

查询-出差-误餐补助/出差换休申请表对比(旨在对比数据库有无误餐补助/出差换休申请表)

select * from Workflow_Instance i
left join Form_TravelInfo f on i.id=f.TravelInstanceId
where FlowGuid='773C9D54-5AF6-4418-8ECD-D40546DA25DA' 
and Status=10
and f.Id is null 

查询出差申请表

select * from Form_TravelInfo 
    where LunchInstanceId='34122'

查询误餐补助/出差-误餐补助/出差换休申请表对比

//select * from Workflow_Instance where id in ('流程ID1','流程ID2','流程ID3',...)
select * from Workflow_Instance where id in ('35561','34122')

修改出差报表标题

update Workflow_Instance 
set Title_Chinese='',Title_English='', CurrentNodeStepId=-1,IsRollback=0 
where id='34122'

s0509

标题: 连接到服务器 ------------------------------ 无法连接到 DESKTOP-FBK0G1K
–解决方案:计算机–>右键-服务–>SQL Server 所有服务全部启动 特别是SQLSERVERAGENT SQL Server 代理 (MSSQLSERVER) 再次连接就好了

s0510

数据库显示行号
工具–>选项–>文本编辑器–>所有语言–>常规–>找到 行号 进行勾选–确认

s0515

appSettings
–解决企业微信端上传附件,PC端在流程中无法查看问题

<!--移动端站点的url,如:http://192.169.0.190:8000/ -->
<add key="WebApiUrl" value="http://192.169.0.190:8000/" />
<!-- 移动端流程附件的文件夹名字,先写fileUpload吧 -->
<add key="WFDownloadFolderName" value="fileUpload" />
--关于STD邮件配置
在配置过程中遇到的问题 及处理 :
    ​Q1:邮件测试失败:526:Authentication failure[0]A1:核查邮箱账号、密码是否有误。
    ​Q2:邮件测试失败:由于连接方在一段时间没有正确答或者连接的主机没有反应,连接尝试失败,192.168.0.192:25 .....A2:检查Smtp服务器是否有误。

s0604今天在处理数据时遇到这样一个错误

消息 8101,级别 16,状态 1,第 14 行
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表’Pay_TaxRate_Cur’中的标识列指定显式值。

遇到这个问题怎么去处理呢?

在有自增长的SQL表格里面插入指定ID的数据的时候,会禁止你操作,提示如题目,解决办法:

set identity_insert A1 on

insert into A1(Id,name,...) select * from DB_A.[dbo].A1

//把另一个数据库中相同的表内容复制到当前数据库对应的表中

set identity_insert A1 off

//注意,列名是不能省略的,而且执行完成之后,最好off一下

小技巧–chrome截全网页图

F12 ——> Ctrl+P ——> 输入: >capture 选择 Screenshot Capture full size screenshot

win10 系统microsoft edge怎么截图整个网页

win10 系统microsoft edge怎么截图整个网页
	点击浏览器右上角笔记样式的标志“做WEB笔记”,会弹出菜单栏。
	菜单栏中左边有“笔”“荧光笔”“橡皮擦”“添加键入的笔记”和“剪辑“,
	右边有“做WEB笔记”“共享”和“退出”。
	这里用截图功能,点击“剪辑”,会出现“拖动以复制区域”,同时鼠标变成“+”字样。
	右键点击鼠标,开始截图自己想要的区域。此时所选区域外面是虚线框。
	在这里重要的一点,常规的截图工具用这种手动截图,只能截图一屏。Edge浏览器自带的剪辑功能,可以拖动网页,一直往下选择。按着键盘的“↓”,就可以拖动加大截图范围。
	截图成功之后,涉及到保存的情况,只要停止拖动截图框,浏览器就默认截图成功,此时已经将截图保存到了粘贴板。然后粘贴到相应的位置就可以,比如WORD文档里面。
	截图完毕,点击右上角的退出,或者按两次Esc键,就可以恢复到之前的页面。

s0619

在数据库做-视图
Lagardere1

select * into Staffinfo 
from (
	select 
		s.StaffNo as '员工工号',s.StaffName as '员工姓名',s.PositionCode as '职位代码',o.Pname as '职位',s.Grade as '职级'
		,oupi.Code4 as 'Team代码',oupi.Name4 as 'Team'
		,oupi.Code5 as 'Store Code代码',oupi.Name5 as 'Store Code'
		,oupi.Code1 as '公司代码',oupi.Name1 as '公司名称'
		,oupi.Code2 as '区域代码',oupi.Name2 as '区域名称'
		,oupi.Code3 AS '部门代码',oupi.name3 as '部门名称'
		,s.StaffName_English as '英文姓名',s.Sex as '性别',p.MobilePhone1 as '手机号码'
		,p2.WorkingLocation_Chinese as '工作城市',s.Email as '公司邮箱',s.DateJoin as '入职时间',s.LastWorkingDate as '离职时间'
		,p2.EmploymentType as '员工类型',(substring(s.IDNumber,7,4)+'-'+substring(s.IDNumber,11,2)+'-'+substring(s.IDNumber,13,2)) as 生日
		,ss.SupervisorName as '直接主管', ss.SupervisorNo as '直接主管工号'

	from hr_Staff s
	left join Org_Staff os on s.StaffNo=os.StaffNo
	left join Org_Position op on os.PositionGuid=op.Guid
	left join Org_Unit ou on op.OrgUnitGuId=ou.Guid
	left join org_unitpathinfo oou on ou.Guid=oou.OrgUnitGuid

	left join org_export o on s.StaffNo=o.StaffNo and s.PositionCode=o.Pcode
	left join hr_Staff_PersonalInfo p on s.StaffNo=p.StaffNo
	left join hr_Staff_Policy p2 on s.StaffNo=p2.StaffNo
	left join hr_Staff_Supervisor ss on o.StaffNo=ss.StaffNo

	left join Org_Dictionary od on ou.CodeId=od.id 
	left join dbo.fun_Org_UnitPathInfo('Chinese') oupi on oupi.OrgUnitGuid = oou.OrgUnitGuid
	where oou.Code5 like '%s%')  as f 

组织架构 异动职位被锁定

Q1:组织架构异动被锁定怎么解决
A1:先去 组织架构>异动生效>组织异动审核> 进行审核 没有效果再去数据库操作

select * from Org_Position
select Enabled,* from Org_Position order by LastUpdateDateTime desc 

//根据对应的字段描述 进行比对  然后对应更改
select op.Enabled,od.Name_Chinese,op.Description,* from Org_Position  op
left join Org_Dictionary od on op.CodeId=od.Id
order by op.LastUpdateDateTime desc 

//先查出组织职位表 筛选出锁定需要改的字段
select Enabled,IsLock,* from Org_Position where LastUpdateUser = '员工编号'  
//一般是最后一个改动是子公司人事账号 IsLock锁定1 
update Org_Position set IsLock =0   where LastUpdateUser = '员工编号'
//where Description ='P018' 容易出现错误  切勿随便使用

本地架构初始化

select  StaffNo,StaffName,DateJoin,* from hr_Staff 

==Excel中写SQL语句直接在数据库中导入 ==

Excel中写SQL公式 实列如下:

//人员信息导入
="insert into hr_Staff(StaffNo,StaffName,DateJoin) values('"&A2&"','"&B2&"','"&C2&"')"
//org_export  组织架构信息导入
="insert into org_export values ('"&A2&"', '"&B2&"','"&C2&"','"&D2&"','"&E2&"','"&F2&"','"&G2&"','"&H2&"','"&I2&"','"&J2&"','"&K2&"','"&L2&"','"&M2&"','"&N2&"','"&O2&"','"&P2&"','"&Q2&"','"&R2&"','"&S2&"','"&T2&"','"&U2&"','"&V2&"','"&W2&"','"&X2&"','"&Y2&"','"&Z2&"', '"&AA2&"' ,'"&AB2&"', '"&AC2&"', '"&AE2&"','"&AF2&"')"

s0727_sql创建备份表和复制数据到备份表
1、复制表结构及数据到新表

CREATE TABLE 新表 SELECT * FROM 旧表
这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable;来删除。
不过这种方法的一个最不好的地方就是新表中没有了旧表的primary key、Extra(auto_increment)等属性。需要自己用"alter"添加,而且容易搞错。

2、只复制表结构到新表

CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2
或CREATE TABLE 新表 LIKE 旧表

3、复制旧表的数据到新表(假设两个表结构一样)

INSERT INTO 新表 SELECT * FROM 旧表

4、复制旧表的数据到新表(假设两个表结构不一样)

INSERT INTO 新表(字段1,字段2,…) SELECT 字段1,字段2,… FROM 旧表

5、可以将表1结构复制到表2

SELECT * INTO 表2 FROM 表1 WHERE 1=2

6、可以将表1内容全部复制到表2

SELECT * INTO 表2 FROM 表1

7、show create table 旧表;

这样会将旧表的创建命令列出。我们只需要将该命令拷贝出来,更改table的名字,就可以建立一个完全一样的表
或者:
create table bus_resposition_bf as select * from bus_resposition_bf where task_id =17110809432850

直接创建备份表,并复制一条数据到新表

/*绩效管理 */

绩效管理 BSC三张表 未发起人员查找、删除

select * from Performance_Period				
//各种周期表			周期表 绩效管理>绩效设置>考核周期设置>
select * from Performance_PeriodRoleGroup		
//周期对应的分组表		绩效管理>绩效设置>考核周期设置> 关联功能 表单属性设置>
select * from Performance_PeriodPartner		
//周期对应的人员表		绩效管理>绩效设置>考核分组设置>

查找到对应的周期表
select * from Performance_Period where  Name_Chinese='对应周期绩效'
select  *  from  Performance_PeriodPartner p  inner join   Performance_PeriodRoleGroup g on p.Group_id =g.Id where g.Period_id='对应周期ID'  and p.IsLocked=0

select * into Performance_PeriodPartner1 from Performance_PeriodPartner
insert into Performance_PeriodPartner select * from Performance_PeriodPartner1
//Performance_PeriodPartner1  备份表
//Performance_PeriodPartner2  根据IsLocked 查到的表

//未发起人员集合
select * from 
--delete 
Performance_PeriodPartner where Group_id in 
(
select id from Performance_PeriodRoleGroup where Period_id in 
(select id from Performance_Period where Name_Chinese='2019年上半期BSC绩效') 
) and IsLocked='0'

我的下属>下属员工排班 STR_UploadFile --> 文件上传

Q1:当你发现某一个页面的字段没有翻译时,又不知道该字段在那张表中,比如:我的下属>下属员工排班 导入-- STR_UploadFile --> 文件上传 ?
A1:一般字段没有翻译 先去 hr_Language 这张表查

如果有,用update 更新

update hr_Language set Chinese='上传文件' where StringID='STR_UploadFile'

如果没有可以考虑

insert into hr_Language(StringID,Chinese,English,Language1,Language2,Language3,CreateUser,CreateDateTime,LastUpdateUser,LastUpdateDateTime) 
	-- values('STR_UploadFile','上传文件','UploadFile','上传文件','上传文件','上传文件','0003','2019-07-24','0003','2019-07-24')

存储过程多参数s0809

 exec FI_GetCustomer 
	@CusNo=10100001,
	@Year=2019
//语法  参数之间用 ‘ , ’ 隔开
//exe后跟 过程名 ,@demo1,@demo2,@demo3,@demo4 皆是创建过程是定义的参数
 exec [过程名]   
	@demo1='',
	@demo2='',
	@demo3='',
	@demo4='',
	····

sql 查询表共多少列s0815

//sql 查询表共多少列
SELECT count(1) as col_Count FROM syscolumns WHERE id = OBJECT_ID('表名')

视图创建s0821

--判断用户自定义视图是否存在
if exists(select * from sysobjects where id=OBJECT_ID(N'v_Staffinfo') and objectproperty(id,N'IsView')=1)
drop view v_Staffinfo        --删除视图
go
//--alter  --适用于已创建视图 没有if判断表是否存在(存在就删除)的情况 往视图表追加信息
create view dbo.v_Staffinfo    --创建视图
as
select 
	s.StaffNo as '员工工号',s.StaffName as '员工姓名',s.PositionCode as '职位代码',o.Pname as '职位',s.Grade as '职级'
	,oupi.Code4 as 'Team代码',oupi.Name4 as 'Team'
	,oupi.Code5 as 'Store Code代码',oupi.Name5 as 'Store Code'
	,oupi.Code1 as '公司代码',oupi.Name1 as '公司名称'
	,oupi.Code2 as '区域代码',oupi.Name2 as '区域名称'
	,oupi.Code3 AS '部门代码',oupi.name3 as '部门名称'
	,s.StaffName_English as '英文姓名',s.Sex as '性别',p.MobilePhone1 as '手机号码'
	,p2.WorkingLocation_Chinese as '工作城市',s.Email as '公司邮箱',s.DateJoin as '入职时间',s.LastWorkingDate as '离职时间'
	,p2.EmploymentType as '员工类型',(substring(s.IDNumber,7,4)+'-'+substring(s.IDNumber,11,2)+'-'+substring(s.IDNumber,13,2)) as 生日
	,ss.SupervisorName as '直接主管', ss.SupervisorNo as '直接主管工号'

from hr_Staff s
left join Org_Staff os on s.StaffNo=os.StaffNo
left join Org_Position op on os.PositionGuid=op.Guid
left join Org_Unit ou on op.OrgUnitGuId=ou.Guid
left join org_unitpathinfo oou on ou.Guid=oou.OrgUnitGuid

left join org_export o on s.StaffNo=o.StaffNo and s.PositionCode=o.Pcode
left join hr_Staff_PersonalInfo p on s.StaffNo=p.StaffNo
left join hr_Staff_Policy p2 on s.StaffNo=p2.StaffNo
left join hr_Staff_Supervisor ss on o.StaffNo=ss.StaffNo

left join Org_Dictionary od on ou.CodeId=od.id 
left join dbo.fun_Org_UnitPathInfo('Chinese') oupi on oupi.OrgUnitGuid = oou.OrgUnitGuid
where s.staffType='Active'  --staffType判断是否在职  Active :在职  Inactive :离职
go

select * from v_Staffinfo        --使用视图查询

笔记整理日期s0821,内容仅供参考

你可能感兴趣的:(SQL,SQL,Server数据库,SQL,笔记)