热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SQLServer常用SQL总结

SQLServer常用SQL总结orderbyNAMEcollateChinese_PRC_Stroke_CS_AS_KS_WS*sqlserver分组不能以text,ntext,image类型的字段作为分组依据*--强制查询使用索引:selectidfromtable_namewith(index(索引名))wherenum@num--全文检索(namelik

SQL Server 常用SQL总结 order by NAME collate Chinese_PRC_Stroke_CS_AS_KS_WS /*sqlserver分组不能以text,ntext,image类型的字段作为分组依据*/ --强制查询使用索引: select id from table_name with(index(索引名)) where num=@num --全文检索(name lik

  SQL Server 常用SQL总结

  order by NAME collate Chinese_PRC_Stroke_CS_AS_KS_WS

  /*sqlserver分组不能以text,ntext,image类型的字段作为分组依据*/

  --强制查询使用索引:

  select id from table_name with(index(索引名)) where num=@num

  --全文检索(name like '%abc%')(substring(cal_name ,1,3)='abc')

  select id from t where charindex('abc',cal_name ) > 0

  --查看对象的定义

  sp_helptext name

  --中文汉字笔画从少到多排列

  select * from table_name order by cal_name collate chinese_prc_stroke_ci_as

  --任意前10条记录

  select top 10 * from table_name order by newid()

  --添加序号时必须有into语句

  select identity(int,1,1) id, * into #table_name from table_name

  --钱10~20行数据

  select top 10 * from (select top 20 * from #table_name order by id asc) as new_tab_name order by id desc

  --随机取出10条数据

  select top 10 * from table_name order by newid()

  --将字串值重复指定的次数。

  SELECT REPLICATE ( 'K' ,5 ) --KKKKK

  --统计有多少个汉字

  select datalength('kk中国123')-len('kk中国123')

  select nullif('kk','kk') --相等为null,否则取第一个

  select isnull(null,'kk') --第一个值不为空取第一个,否则为第二个

  select coalesce(null,null,'kk','中国') --返回第一个非空值

  --小数取整

  select CEILING(12.7) --[13];取大于12.7的最小整数

  select CEILING(12.2) --[13];取大于12.2的最小整数

  select FLOOR(12.7) --[12];取小于12.7的最大整数

  select FLOOR(12.2) --[12];取小于12.2的最大整数

  select round(12.77,0) --[13.00];四舍五入,0位小数

  select round(12.24,1) --[12.20];四舍五入,1位小数

  --按位置替换

  select STUFF ( 'ABCDEFG' , 2 , 0 ,'-' ) --[A-BCDEFG];第二个位置,取字符长度为0,替换为-

  select STUFF ( 'ABCDEFG' , 2 , 1 ,'b' ) --[AbCDEFG];第二个位置,取字符长度为1,替换为b

  select STUFF ( 'ABCDEFG' , 2 , 2 ,'*' ) --[A*DEFG];第二个位置,取字符长度为2,替换为*

  --按相同字符替换

  select REPLACE('ABCDEFG','B','b') --[AbCDEFG];将所有B对应替换为b

  select REPLACE('ABCDEFG-Bc','BC','*') --[A*DEFG-*];将所有BC对应替换为一个*,不区分大小写

  --判断某字符存在

  select CHARINDEX('456','123456789')

  select CHARINDEX('1','235694526') --[0];判断1是否存在

  select CHARINDEX('1','12314510215985') --[1];1出现的位置

  select CHARINDEX('1','12314510215985',8) --[10];从第八个字符查找,1在字符串中出现的位置

  --起始位置,支持匹配表达式

  select patindex('456', '123456789') --[0];

  select patindex('456%', '123456789') --[0];

  select patindex('%456%', '123456789') --[4];

  select patindex('12%', '123456789') --[1];

  select patindex('__3%', '123456789') --[1];

  select patindex('%[js]%','hsdjgjsrgsdgfjt')--返回j或s中第一个字符出现的位置

  select patindex('%[^js]%','ssjjgjsrgsdgfjt')--返回不是j和s外第一个字符出现的位置

  print '开始执行输出'

  go

  waitfor delay '00:00:05' --5秒后执行

  print '延时执行输出'

  go

  waitfor time '12:00:00' --12点执行

  print '定时执行输出'

  go

  --远程连接

  select * from openrowset('sqloledb','servername';'username';'password',dbname.dbo.tablename)

  select * from opendatasource('sqloledb','data source=ip(or servername);user id=username;password=password')。dbname.dbo.tablename

  --当前日期加一个月

  select convert(varchar(10),dateadd(m,1,getdate()),120)

  select dateadd(m,1,getdate())

  --取得当前年月的最后一天

  select dateadd(d,-1,convert(datetime,convert(varchar(7),dateadd(m,1,getdate()),120) + '-01'))

  --取得当前年月第一日

  select convert(datetime,convert(varchar(7),getdate(),120)+ '-01',120)

  --取得今年第一个月

  select convert(varchar(4),getdate(),120)+'-01'

  --取得当前年月的前一个月

  select convert(varchar(7),dateadd(m,-1,getdate()),120)

  --当前季度的第一天

  select dateadd(quarter, datediff(quarter,0,getdate()), 0)

  --本年的最后一天

  select dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate())+1, 0))

  --判断是否闰年

  select case day(dateadd(mm, 2, dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate()), 0)))) when 28 then '平年' else '闰' end

  select case datediff(day,datename(year,getdate())+'-02-01',dateadd(mm,1,datename(year,getdate())+'-02-01')) when 28 then '平年' else' 闰年' end

  /*查看对象或表是否存在*/

  --查看与表相关的所有其他对象

  select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%tb%'

  --查看当前数据库所有表

  select name from sysobjects where xtype='u' and status>=0

  select * from information_schema.tables where table_type='base table'

  --查看指定表的所有数据列

  select name from syscolumns where id=object_id('tablename')

  select column_name from information_schema.columns where table_name='tablename'

  --查看当前数据库所有视图

  select * from information_schema.views --(有定义 )

  select * from dbo.sysobjects where objectproperty(id, n'isview') = 1

  select * from dbo.sysobjects where type='v'

  -- 判断数据库是否存在

  if exists(select 1 from master.dbo.sysdatabases where name=n'ghhg')

  select * from mastersysdatabases --数据库

  -- 判断临时表是否存在

  if object_id(n'tempdb[#temp_table]') is not null

  if exists (select * from tempdb.dbo.sysobjects where id = object_id(n'[tempdb].[dbo].[#temp_table]'))

  -- 判断作业是否存在

  if exists (select job_id from msdb.dbo.sysjobs_view where name ='jobname')

  order by NAME collate Chinese_PRC_Stroke_CS_AS_KS_WS

  /*sqlserver分组不能以text,ntext,image类型的字段作为分组依据*/

  --强制查询使用索引:

  select id from table_name with(index(索引名)) where num=@num

  --全文检索(name like '%abc%')(substring(cal_name ,1,3)='abc')

  select id from t where charindex('abc',cal_name ) > 0

  --查看对象的定义

  sp_helptext name

  --中文汉字笔画从少到多排列

  select * from table_name order by cal_name collate chinese_prc_stroke_ci_as

  --任意前10条记录

  select top 10 * from table_name order by newid()

  --添加序号时必须有into语句

  select identity(int,1,1) id, * into #table_name from table_name

  --钱10~20行数据

  select top 10 * from (select top 20 * from #table_name order by id asc) as new_tab_name order by id desc

  --随机取出10条数据

  select top 10 * from table_name order by newid()

  --将字串值重复指定的次数。

  SELECT REPLICATE ( 'K' ,5 ) --KKKKK

  --统计有多少个汉字

  select datalength('kk中国123')-len('kk中国123')

  select nullif('kk','kk') --相等为null,否则取第一个

  select isnull(null,'kk') --第一个值不为空取第一个,否则为第二个

  select coalesce(null,null,'kk','中国') --返回第一个非空值

  --小数取整

  select CEILING(12.7)--[13];取大于12.7的最小整数

  select CEILING(12.2)--[13];取大于12.2的最小整数

  select FLOOR(12.7)--[12];取小于12.7的最大整数

  select FLOOR(12.2)--[12];取小于12.2的最大整数

  select round(12.77,0)--[13.00];四舍五入,0位小数

  select round(12.24,1)--[12.20];四舍五入,1位小数

  --按位置替换

  select STUFF ( 'ABCDEFG' , 2 , 0 ,'-' )--[A-BCDEFG];第二个位置,取字符长度为0,替换为-

  select STUFF ( 'ABCDEFG' , 2 , 1 ,'b' )--[AbCDEFG];第二个位置,取字符长度为1,替换为b

  select STUFF ( 'ABCDEFG' , 2 , 2 ,'*' )--[A*DEFG];第二个位置,取字符长度为2,替换为*

  --按相同字符替换

  select REPLACE('ABCDEFG','B','b')--[AbCDEFG];将所有B对应替换为b

  select REPLACE('ABCDEFG-Bc','BC','*')--[A*DEFG-*];将所有BC对应替换为一个*,不区分大小写

  --判断某字符存在

  select CHARINDEX('456','123456789')

  select CHARINDEX('1','235694526')--[0];判断1是否存在

  select CHARINDEX('1','12314510215985')--[1];1出现的位置

  select CHARINDEX('1','12314510215985',8)--[10];从第八个字符查找,1在字符串中出现的位置

  --起始位置,支持匹配表达式

  select patindex('456', '123456789')--[0];

  select patindex('456%', '123456789')--[0];

  select patindex('%456%', '123456789')--[4];

  select patindex('12%', '123456789')--[1];

  select patindex('__3%', '123456789')--[1];

  select patindex('%[js]%','hsdjgjsrgsdgfjt')--返回j或s中第一个字符出现的位置

  select patindex('%[^js]%','ssjjgjsrgsdgfjt')--返回不是j和s外第一个字符出现的位置

  print '开始执行输出'

  go

  waitfor delay '00:00:05' --5秒后执行

  print '延时执行输出'

  go

  waitfor time '12:00:00' --12点执行

  print '定时执行输出'

  go

  --远程连接

  select * from openrowset('sqloledb','servername';'username';'password',dbname.dbo.tablename)

  select * from opendatasource('sqloledb','data source=ip(or servername);user id=username;password=password')。dbname.dbo.tablename

  --当前日期加一个月

  select convert(varchar(10),,dateadd(m,1,getdate()),120)

  select dateadd(m,1,getdate())

  --取得当前年月的最后一天

  select dateadd(d,-1,convert(datetime,convert(varchar(7),dateadd(m,1,getdate()),120) + '-01'))

  --取得当前年月第一日

  select convert(datetime,convert(varchar(7),getdate(),120)+ '-01',120)

  --取得今年第一个月

  select convert(varchar(4),getdate(),120)+'-01'

  --取得当前年月的前一个月

  select convert(varchar(7),dateadd(m,-1,getdate()),120)

  --当前季度的第一天

  select dateadd(quarter, datediff(quarter,0,getdate()), 0)

  --本年的最后一天

  select dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate())+1, 0))

  --判断是否闰年

  select case day(dateadd(mm, 2, dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate()), 0)))) when 28 then '平年' else '闰' end

  select case datediff(day,datename(year,getdate())+'-02-01',dateadd(mm,1,datename(year,getdate())+'-02-01')) when 28 then '平年' else' 闰年' end

  /*查看对象或表是否存在*/

  --查看与表相关的所有其他对象

  select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%tb%'

  --查看当前数据库所有表

  select name from sysobjects where xtype='u' and status>=0

  select * from information_schema.tables where table_type='base table'

  --查看指定表的所有数据列

  select name from syscolumns where id=object_id('tablename')

  select column_name from information_schema.columns where table_name='tablename'

  --查看当前数据库所有视图

  select * from information_schema.views --(有定义 )

  select * from dbo.sysobjects where objectproperty(id, n'isview') = 1

  select * from dbo.sysobjects where type='v'

  -- 判断数据库是否存在

  if exists(select 1 from master.dbo.sysdatabases where name=n'ghhg')

  select * from mastersysdatabases --数据库

  -- 判断临时表是否存在

  if object_id(n'tempdb[#temp_table]') is not null

  if exists (select * from tempdb.dbo.sysobjects where id = object_id(n'[tempdb].[dbo].[#temp_table]'))

  -- 判断作业是否存在

  if exists (select job_id from msdb.dbo.sysjobs_view where name ='jobname')

  [sql] view plaincopyprint?

  /*

  DBCC FREEPROCCACHE --清除执行计划缓存

  DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS --清除缓冲区

  set statistics profile on

  set statistics io on

  set statistics time on

  set statistics profile off

  set statistics io off

  set statistics time off

  */

  /*

  DBCC FREEPROCCACHE --清除执行计划缓存

  DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS --清除缓冲区

  set statistics profile on

  set statistics io on

  set statistics time on

  set statistics profile off

  set statistics io off

  set statistics time off

  */

  [sql] view plaincopyprint?

  --已知表名查数据库名

  declare @Table table(DB sysname,TabName sysname)

  insert @Table

  exec sp_msforeachdb 'select ''?'' as DB,Name as 表名 from [?]sysobjects where type=''U'' and Name =''fjda'''

  select * from @Table

  --已知表名查数据库名

  declare @Table table(DB sysname,TabName sysname)

  insert @Table

  exec sp_msforeachdb 'select ''?'' as DB,Name as 表名 from [?]sysobjects where type=''U'' and Name =''fjda'''

  select * from @Table

推荐阅读
  • 推荐一个ASP的内容管理框架(ASP Nuke)的优势和适用场景
    本文推荐了一个ASP的内容管理框架ASP Nuke,并介绍了其主要功能和特点。ASP Nuke支持文章新闻管理、投票、论坛等主要内容,并可以自定义模块。最新版本为0.8,虽然目前仍处于Alpha状态,但作者表示会继续更新完善。文章还分析了使用ASP的原因,包括ASP相对较小、易于部署和较简单等优势,适用于建立门户、网站的组织和小公司等场景。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • Echarts图表重复加载、axis重复多次请求问题解决记录
    文章目录1.需求描述2.问题描述正常状态:问题状态:3.解决方法1.需求描述使用Echats实现了一个中国地图:通过选择查询周期&#x ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Python字典推导式及循环列表生成字典方法
    本文介绍了Python中使用字典推导式和循环列表生成字典的方法,包括通过循环列表生成相应的字典,并给出了执行结果。详细讲解了代码实现过程。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • “你永远都不知道明天和‘公司的意外’哪个先来。”疫情期间,这是我们最战战兢兢的心情。但是显然,有些人体会不了。这份行业数据,让笔者“柠檬” ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
author-avatar
Christy-1221
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有