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

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 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 ()

 

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
3月前
|
SQL 存储 关系型数据库
关系型数据库SQLserver基本 SQL 操作
【7月更文挑战第28天】
33 4
|
2月前
|
SQL 运维 监控
SQL Server 运维常用sql语句(二)
SQL Server 运维常用sql语句(二)
25 3
|
2月前
|
SQL XML 运维
SQL Server 运维常用sql语句(三)
SQL Server 运维常用sql语句(三)
13 1
|
2月前
|
SQL 关系型数据库 MySQL
SQL数据库和 SQLserver数据库
【8月更文挑战第19天】SQL数据库和 SQLserver数据库
47 2
|
2月前
|
SQL 关系型数据库 MySQL
SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
137 0
|
2月前
|
SQL 运维 Oracle
SQL Server 项目中 SQL 脚本更新、升级方式,防止多次重复执行
SQL Server 项目中 SQL 脚本更新、升级方式,防止多次重复执行
32 0
|
16天前
|
SQL 数据库
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
SQL Server附加数据库出现错误823,附加数据库失败。数据库没有备份,无法通过备份恢复数据库。 SQL Server数据库出现823错误的可能原因有:数据库物理页面损坏、数据库物理页面校验值损坏导致无法识别该页面、断电或者文件系统问题导致页面丢失。
82 12
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
|
2月前
|
SQL 数据库 数据安全/隐私保护
SQL Server数据库Owner导致事务复制log reader job无法启动的解决办法
【8月更文挑战第14天】解决SQL Server事务复制Log Reader作业因数据库所有者问题无法启动的方法:首先验证数据库所有者是否有效并具足够权限;若非,使用`ALTER AUTHORIZATION`更改为有效登录名。其次,确认Log Reader使用的登录名拥有读取事务日志所需的角色权限。还需检查复制配置是否准确无误,并验证Log Reader代理的连接信息及参数。重启SQL Server Agent服务或手动启动Log Reader作业亦可能解决问题。最后,审查SQL Server错误日志及Windows事件查看器以获取更多线索。
|
11天前
|
SQL 关系型数据库 MySQL
创建包含MySQL和SQLServer数据库所有字段类型的表的方法
创建一个既包含MySQL又包含SQL Server所有字段类型的表是一个复杂的任务,需要仔细地比较和转换数据类型。通过上述方法,可以在两个数据库系统之间建立起相互兼容的数据结构,为数据迁移和同步提供便利。这一过程不仅要考虑数据类型的直接对应,还要注意特定数据类型在不同系统中的表现差异,确保数据的一致性和完整性。
22 4
|
25天前
|
SQL 存储 数据管理
SQL Server数据库
SQL Server数据库
41 11
下一篇
无影云桌面