MS SqlServer中少用但是好用的SQL语句

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
简介: 代码 /*-- 2010-02-26 -- 布朗-- QQ:156298979*/ -- with ties可以附加与排序字段相同值的多个行select  top 3  with ties * from hrEmployee order by shortName ascset rowcou...
img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
/*
-- 2010-02-26 
-- 布朗
-- QQ:156298979
*/  

--  with ties可以附加与排序字段相同值的多个行
select    top   3    with  ties  *   from  hrEmployee  order   by  shortName  asc

set   rowcount   3   -- 设置全局变量,使每次返回的行数都为3行
select   *   from  hrEmployee  order   by  shortName  asc

set   rowcount   0   -- 设置全局变量,使每次返回的行数为所有行


--  select ...where 符合代替if语句
declare   @m   int  , @n   int  , @i   int
set   @m = 4  
set   @n = 1
select   @i =   ceiling ( @m / @n where   @m > @n
select   @i

-- 服务器环境信息
select   serverproperty( ' Edition ' )

-- 字符串函数 
--
将一个字符串指定位置开始指定长度的内容替换成新串的值
select   stuff ( ' abcedefg ' , 3 , 2 , ' 1234 ' )

-- 搜索子串在父串的位置
select   CharIndex ( ' c ' , ' abcdefg ' , 1 )
select   PatIndex ( ' %[cd]% ' , ' abcdefg ' )

-- 发音相似的词或名称
select   soundex ( ' lfc ' )
-- 返回两个字符串的差异程度
select   Difference ( ' abcd ' , ' abce ' )


inner   join   -- 内连接
left   outer   join   -- 左外连接
right   outer   join   -- 右外连接
full   outer   join   -- 全外连接
cross   outer   join   -- 交叉连接 A表4条记录,B表5条记录,交叉后生成20条记录(笛卡尔乘积)


-- 两个表合并
select   *   from  hrEmployeeA 
union   all  
select   *   from  hrEmployeeB

-- 两个表相交
select   distinct (UserName) 
from  
(
select   distinct (UserName)  from  hrEmployeeA 
union   all  
select   distinct (UserName)  from  hrEmployeeB
) Emp
group   by  UserName

-- 两个表关系除
--
两个表关系差(集合差)

 

-- 全文索引 
exec  sp_fulltext_database  ' enable '   -- 启用全文检索 'disable'禁用
create  fulltext catalog nofc2  as   default -- 为数据库创建全文目录
create  fulltext  index   on  hrEmployee( [ Name ] , [ ShortName ] , [ Description ] -- 创建针对数据库中某个表的一列或多列的全文索引。每个表只允许有一个全文索引
  Key   Index  PK_hrEmployee
-- 全文检索
select   *   from  hrEmployee  where   Contains (hrEmployee. * , ' ' and   Contains (hrEmployee. * , ' ' -- (where Contains类似 where in())
--
全文检索
select   *   from   ContainsTable  (hrEmployee, * , ' ' )
-- 临近的词,屈折变体,

-- 全文检索-模糊查询
select   *   from  hrEmployee  where   FreeText ( * , ' ' )
select   *   from   FreeTextTable (hrEmployee, * , ' ' )

drop  fulltext  index   on  hrEmployee
drop  fulltext catalog nofc2
exec  sp_fulltext_database  ' disable '


-- 视图加密
create   view  Vabc  as
(
select   *   from  hrEmployee
)
with  Encryption
-- WITH CHECK OPTION / SCHEMABINDING / VIEW_METADATA


-- Insert语句
--
Insert/values
--
Insert/select
--
Insert/exec   --插入存储过程的结果集
insert   into  hrEmployee (Name,shortName,Description)  exec  sp_hrGetEmployee 
-- insert Default --插入表的默认值
insert   into  hrEmployee  default   values


-- Update语句连接多个表
update  hrEmployee 
set  Salary  =  Salary  *  ( 1   +   2 *   case   when  t1.EnterTime  >   ' 2000-01-01 '   then   1   else   0   end  )   from  hrEmployee t1  join  zj_Dept t2  on  t1.DeptID  =  t2.DeptID


-- Delete语句 连接多个表
delete   from  zj_Operation_Log
from  zj_Operation_Log t1 
join  zj_Dept t2  on  t1.DeptNo  =  t2.DeptNo
where  t2.DeptName = ' 行政部 '

 

-- 标识值
select   @@identity     -- 全局变量所有表最所生成的最近一个标识值
select   Scope_identity ()   -- 批处理或者最近的作用域中所生成的最近一个标识值
select  ident_current( ' hrEmployee ' -- 指定表名的表所生成的最近一个标识值

-- 全局唯一标识符
select   newid ()

 

目录
相关文章
|
3月前
|
SQL Web App开发 安全
SQL Server 2025 年 8 月更新 - 修复 CVE-2025-49759 SQL Server 特权提升漏洞
SQL Server 2025 年 8 月更新 - 修复 CVE-2025-49759 SQL Server 特权提升漏洞
319 2
SQL Server 2025 年 8 月更新 - 修复 CVE-2025-49759 SQL Server 特权提升漏洞
|
2月前
|
SQL Web App开发 安全
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
145 0
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
|
3月前
|
SQL 容灾 安全
云时代SQL Server的终极答案:阿里云 RDS SQL Server如何用异地容灾重构系统可靠性
在数字化转型的浪潮中,数据库的高可用性已成为系统稳定性的生命线。作为经历过多次生产事故的资深开发者,肯定深知传统自建SQL Server架构的脆弱性——直到遇见阿里云 RDS SQL Server,其革命性的异地容灾架构彻底改写了游戏规则。
|
4月前
|
SQL Web App开发 安全
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
376 0
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
|
10月前
|
SQL Java 数据库连接
如何在 Java 代码中使用 JSqlParser 解析复杂的 SQL 语句?
大家好,我是 V 哥。JSqlParser 是一个用于解析 SQL 语句的 Java 库,可将 SQL 解析为 Java 对象树,支持多种 SQL 类型(如 `SELECT`、`INSERT` 等)。它适用于 SQL 分析、修改、生成和验证等场景。通过 Maven 或 Gradle 安装后,可以方便地在 Java 代码中使用。
3293 11
|
SQL 数据库
执行 Transact-SQL 语句或批处理时发生了异常。 (Microsoft.SqlServer.ConnectionInfo)之解决方案
执行 Transact-SQL 语句或批处理时发生了异常。 (Microsoft.SqlServer.ConnectionInfo)之解决方案
1439 0
|
Java 应用服务中间件 Maven
从零到英雄:一步步构建你的首个 JSF 应用程序,揭开 JavaServer Faces 的神秘面纱
【8月更文挑战第31天】JavaServer Faces (JSF) 是一种强大的 Java EE 标准,用于构建企业级 Web 应用。它提供了丰富的组件库和声明式页面描述语言 Facelets,便于开发者快速开发功能完善且易于维护的 Web 应用。本文将指导你从零开始构建一个简单的 JSF 应用,包括环境搭建、依赖配置、Managed Bean 编写及 Facelets 页面设计。
307 0
|
SQL 数据库 索引
Sqlserver与access数据库sql语法十大差异
ACCESS结构简单容易处理,而且也能满足多数的网站程序要求,也是初学者的试牛刀。ACCESS是小型数据库,既然是小型就有他根本的局限性: 1)、数据库过大,一般ACCESS数据库达到50M左右的时候性能会急剧下降! 2)、网站访问频繁,经常超过100人的在线时,处理速度会有影响! 3)、记录数过多,一般记录数达到10万条左右的时候性能就会急剧下降!微软公司为了与ACCESS高低搭配的一种高端方案:改用了Sqlserver,但语法会有一些差异。
1130 0
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
525 13