SQL Sever 学习系列之三
SQL Sever 学习系列之三
五、经理今天刚谈到with的用法(with的类似用法在delphi中有所体现),在资料文档看到了这一段如下主要体现一个关键字pivot(这是首次在武汉三佳医疗有限公司面试开发岗时遇到的,虽过去一段时间还是记忆犹新),摘抄于此,备查:

1 use master 2 --九九乘法表 3 with cte1 as 4 ( 5 select top 9 ROW_NUMBER()over(order by getdate())as RN from sysobjects 6 ) 7 ,cte2 as 8 ( 9 select A.RN,B.Va,ROW_NUMBER()over(PARTITION by RN order by getdate())as RC 10 from 11 (select * from cte1)as A 12 outer apply(select case when A.RN<=RN 13 then 14 (ltrim(A.RN)+'X'+ltrim(RN)+'='+LTRIM(A.RN*RN)) 15 else '' end as Va 16 from cte1 ) as B 17 ) 18 select * from cte2 a 19 pivot 20 ( 21 max(Va) 22 for RN in([1],[2],[3],[4],[5],[6],[7],[8],[9]) 23 )as B
结果如图:
六、以下程序:1)比较select和print的区别;
2)看出用isnull的好处(确切地说保证计算的正确性——将“未赋值”(不是指赋空值)转换为相应的值)(类似还有一个关键字coalesce)

1 declare 2 @a int, 3 @w decimal(2,1), 4 @m decimal(2,1), 5 @q char(3) 6 set @a=null 7 set @w=3.12 8 set @q=null 9 set @m=3.15 10 print @m --3.2 11 select @m --3.2 12 select @a,@w,@q --null 3.1 null 13 print @a --无 14 select @a+@w --null 15 select ISNULL(@a,0)+ISNULL(@w,0) --3.1 16 print @a+@w --null 17 print ISNULL(@a,0)+ISNULL(@w,0) --3.1
结果如图:
七、领悟count(*)和count(字段)的含义,null和‘’还是很有区别的。
1 select COUNT(*) from mchk where psfx not like '' --9564 2 select COUNT(*) from mchk --12182 -----三数相差637(psfx为null) 3 select COUNT(*) from mchk where psfx is null --637 4 select COUNT(*) from mchk where psfx is not null --11545 5 select COUNT(*) from mchk --12182 6 7 select COUNT(psfx) from mchk where psfx like '' --1981 8 select COUNT(psfx) from mchk where psfx not like '' --9564 9 select COUNT(psfx) from mchk --11545 10 11 select COUNT(psfx) from mchk where psfx is null --0 12 select COUNT(psfx) from mchk where psfx is not null --11545 13 select COUNT(psfx) from mchk --11545
上述说明psfx没有赋任何值的有637(12182-11545)条记录。这个结果验证可以用isnull()或者coalesce()函数。(截图略)
八、sql语句查看一个数据库的位置或者查看有多少实例?

1 --sql语句查看一个数据库的位置 2 select name,filename,crdate,cmptlevel,version 3 from master.dbo.sysdatabases
作者: 天堂的鸽子
出处:https://www.cnblogs.com/zhangbc/p/3433143.html
版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际」知识共享许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述