Sqlserver复杂查询
--联表修改
update xyzrb
set xyzrb.xy_card=tablsb.card
from xyzrb left join tablsb on xyzrb.xybh=tablsb.xybh
--取重复数据的各最大一条
select x.comment as comment,x.fidcoachid as fidcoachid from xcb_comment x
right join
(select max(id) as id,fidcoachid from xcb_comment group by fidcoachid) as mc
on mc.id=x.id;
--查询出多行数据合并成一行
SELECT s.*, B.name AS fidDModelName,
subjects = (stuff((SELECT ',' + h.name FROM xcb_coachattr t LEFT JOIN hrm_BaseData h ON h.id = t.fiddata AND t .code = 'subjects' WHERE t .fidcoachid = s.id FOR xml path('')), 1, 1, '')),
regions = (stuff((SELECT ',' + h.name FROM xcb_coachattr t LEFT JOIN hrm_BaseData h ON h.id = t.fiddata AND t .code = 'Region' WHERE t .fidcoachid = s.id FOR xml path('')), 1, 1, ''))
FROM xcb_coach s LEFT JOIN hrm_BaseData B ON S.fidDModel = B.id AND B.Code = 'DModel' WHERE 1 = 1
--高级多表联表查询
select
s.chrstuno as schrstuno,
s.chrname as schrname,
s.chrphone as schrphone,
B.name as fidDModelName,
c.chrname as cchrname,
c.chrphone as cchrphone,
c.carNo as ccarNo,
B1.name as fidsubjectname,
B2.name as fidRegionname,
CONVERT(varchar(10),o.orderdate,23) as orderdate,
case when o.paytype =1 then '现金支付'
when o.paytype = 2 then '课时支付'
end as paytype,
from xcb_order o
left join hrm_BaseData B on o.fidDModel=B.id and B.Code='DModel'
left join hrm_BaseData B1 on o.fidsubject=B1.id and B1.Code='subjects'
left join hrm_BaseData B2 on o.fidRegion=B2.id and B2.Code='Region'
left join xcb_students s on o.fidstudent=s.id
left join xcb_coach c on o.fidcoachid=c.id
where o.fidcorp=1 and o.statue in(1,2,3,4,5,6)
--生成Java实体对象
SELECT
case
when isnullable='' then '@NotNull'+CHAR(10)+CHAR(13)
else ''
end+
case
when xtype='' or xtype='' or xtype='' or xtype='' or xtype=''
then '@Size(max = '+convert(varchar(12),length)+')'+CHAR(13)+CHAR(10)+'@Column(name = "'+Name+'")'+CHAR(13)+CHAR(10)+'private String '
when xtype='' or xtype='' or xtype=''
then '@Column(name = "'+Name+'")'+CHAR(13)+CHAR(10)+'private int '
when xtype='' or xtype=''
then '@Column(name = "'+Name+'")'+CHAR(13)+CHAR(10)+'private BigDecimal '
when xtype='' or xtype=''
then '@Column(name = "'+Name+'")'+CHAR(13)+CHAR(10)+'@Temporal(TemporalType.TIMESTAMP)'+CHAR(13)+CHAR(10)+'private Date '
end+Name+';',xtype FROM SysColumns WHERE id=Object_Id('xcb_clglb')
--int类型字段拼接字符转换
select convert(varchar(30),id)+'' from xcb_students where id=11; --查询所有表名
select 'select * from '+ name+'; --' from sysobjects where xtype='u' order by name; --查询表所有字段
select name+',' FROM SysColumns WHERE id=Object_Id('xyfyb') ;
--查看表字段及说明
SELECT t.[name] AS 表名,c.[name] AS 字段名,cast(ep.[value] as varchar(100)) AS [字段说明]
FROM sys.tables AS t
INNER JOIN sys.columns AS c ON t.object_id = c.object_id
LEFT JOIN sys.extended_properties AS ep
ON ep.major_id = c.object_id AND ep.minor_id = c.column_id WHERE t.name='TableName'
--快速查询表数据结构
SELECT CASE WHEN col.colorder = 1 THEN obj.name ELSE '' END AS 表名,
col.colorder AS 序号 ,
col.name AS 列名 ,
ISNULL(ep.[value], '') AS 列说明 ,
t.name AS 数据类型 ,
col.length AS 长度 ,
ISNULL(COLUMNPROPERTY(col.id, col.name, 'Scale'), 0) AS 小数位数 ,
CASE WHEN COLUMNPROPERTY(col.id, col.name, 'IsIdentity') = 1 THEN '√' ELSE '' END AS 标识 ,
CASE WHEN EXISTS ( SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN dbo.sysobjects so ON so.name = si.name AND so.xtype = 'PK' WHERE sc.id = col.id AND sc.colid = col.colid ) THEN '√' ELSE '' END AS 主键 ,
CASE WHEN col.isnullable = 1 THEN '√' ELSE '' END AS 允许空 ,
ISNULL(comm.text, '') AS 默认值 FROM dbo.syscolumns col
LEFT JOIN dbo.systypes t ON col.xtype = t.xusertype
inner JOIN dbo.sysobjects obj ON col.id = obj.id AND obj.xtype = 'U' AND obj.status >= 0
LEFT JOIN dbo.syscomments comm ON col.cdefault = comm.id
LEFT JOIN sys.extended_properties ep ON col.id = ep.major_id AND col.colid = ep.minor_id AND ep.name = 'MS_Description'
LEFT JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id AND epTwo.minor_id = 0 AND epTwo.name = 'MS_Description' WHERE obj.name = 'cartab'--表名
ORDER BY col.colorder ;
--生成Java实体类(含字段说明)
SELECT
case
when c.is_nullable='' then '@NotNull'
else ''
end+
case
when c.user_type_id='' or c.user_type_id='' or c.user_type_id='' or c.user_type_id='' or c.user_type_id=''
then '@Size(max = '+convert(varchar(12),c.max_length)+')'+'@Column(name = "'+c.name+'")'+'private String '
when c.user_type_id='' or c.user_type_id='' or c.user_type_id=''
then '@Column(name = "'+c.name+'")'+'private int '
when c.user_type_id='' or c.user_type_id=''
then '@Column(name = "'+c.name+'")'+'private BigDecimal '
when c.user_type_id='' or c.user_type_id=''
then '@Column(name = "'+c.name+'")'+'@Temporal(TemporalType.TIMESTAMP)'+'private Date '
end+c.name+';//'+cast(ISNULL(ep.[value],'') as varchar(100)) FROM sys.tables AS t
INNER JOIN sys.columns AS c ON t.object_id = c.object_id
LEFT JOIN sys.extended_properties AS ep ON ep.major_id = c.object_id AND ep.minor_id = c.column_id
WHERE t.name='basedata';
Sqlserver复杂查询的更多相关文章
- SqlServer 一个查询语句以致tempdb增大55G (转载)
SqlServer 一个查询语句导致tempdb增大55G 今天操作着服务器,突然右下角提示“C盘空间不足”! 吓一跳!~ 看看C盘,还有7M!!!这么大的C盘空间怎么会没了呢?搞不好等下服务器会动不 ...
- SQLServer中查询的数字列前面补0返回指定长度的字符串
SQLServer中查询的数字列前面补0返回指定长度的字符串: 如: 角本如下: /****** Script for SelectTopNRows command from SSMS ******/ ...
- SQLServer分页查询存储过程
项目中用到的SQLServer分页查询存储过程. [存储过程] create PROCEDURE prcPageResult -- 获得某一页的数据 -- @currPage int = 1, ...
- SQLServer 分组查询相邻两条记录的时间差
原文:SQLServer 分组查询相邻两条记录的时间差 首先,我们通过数据库中表的两条记录来引出问题,如下图 以上为一个记录操作记录的表数据.OrderID为自增长列,后面依次为操作类型,操作时间,操 ...
- SqlServer 一个查询语句导致tempdb增大55G(转载)
SqlServer 一个查询语句导致tempdb增大55G 今天操作着服务器,突然右下角提示“C盘空间不足”! 吓一跳!~ 看看C盘,还有7M!!!这么大的C盘空间怎么会没了呢?搞不好等下服务器会动不 ...
- 图解SQLSERVER联合查询和连接查询的区别
相信很多人都会用SQLSERVER联合查询和连接查询,但是用起来不一定都得心应手,对于其中的原理可能就模糊不清了,要想很牢固地掌握和运用SQL联合查询和连接查询机制,必须对其根本原理有很清晰认识, ...
- SQLServer导出查询结果带表头(标题行)
SQLServer导出查询结果带表头(标题行) 平时我们经常会需要将SQLSERVER查询的结果复制到EXCEL文档中进行分析处理,但是有一件事很头痛,就是复制结果网格的数据到EXCEL之后,都是没有 ...
- SQLServer数据库查询语法
SQLServer数据库查询语法 前言: SQLServer数据库介绍: SQLServer数据库是微软公司推出的一款关系型数据库系统,SQL Server是一个可扩展的.高性能的.为分布式客户机/服 ...
- 高效的SQLSERVER分页查询的几种示例分析
Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...
- 真正高效的SQLSERVER分页查询(多种方案)
Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...
随机推荐
- VS2013+phread.h环境配置
原文链接:http://blog.csdn.net/qianchenglenger/article/details/16907821 本人使用的是windows7 旗舰版64位 目前用的是pthrea ...
- Linux 中安装JDK及配置环境
- 部署core
https://www.cnblogs.com/jasonduan/p/9193702.html
- boost pointer container
1. boost::ptr_vector #include <boost/ptr_container/ptr_vector.hpp> #include <iostream> i ...
- QT多线程学习
一.想要使用Qthread必须先创建,继承Qthread的类. #ifndef THREADTEST_H #define THREADTEST_H #include <QThread> # ...
- windows 下安装python 的requests模块
下载地址:https://codeload.github.com/requests/requests/legacy.zip/master 下载好后解压,进入目录执行下面命令 在cmd下:python ...
- QC浏览器登陆自动关闭
服务器(win2003 SP2)上装了个QC10,用了一段时间后突然登陆不上了.可以看见域及Project信息,但是登陆后加载几秒后突然自动关闭.局域网内的其他机器可以登陆. 解决方案:修改数据执行保 ...
- java %d %n \n
Java中,%d和%f分别用来表示输出时,替换整型输出和浮点型输出的占位符. 如: int a=28; float b = 13.0f; System.out.printf("整数是:%d% ...
- 阻抗匹配 及 SI9000 使用
1. 阻抗匹配 1. 波长 * 频率 = 光速(3*10^8) 2. PCB走线什么时候需要做阻抗匹配? 不主要看频率,而关键是看信号的边沿陡峭程度,即信号的上升/下降时间,一般认为如果信号的上升/下 ...
- css点击高亮
.btn-default:active:focus, .btn-default:active:hover { color: #333; background-color:lightseagreen; ...