SQL SERVER 语句杂记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/marko_zheng/article/details/85274519

SQL SERVER 语句杂记


/****** Script for SelectTopNRows command from SSMS  ******/
SELECT TOP (1000) [id]
      ,[topicName]
      ,[topicAnswer]
      ,[state]
      ,[memo]
      ,[intime]
  FROM [MyTest].[dbo].[Topic];
  
select count(*) from Topic        --查询某张表中数据的总行数

  insert into 
  [dbo].[Topic] (topicName,topicAnswer,memo) values ('问题3','{A:的333,B:的,C:的,D:的}','备注');
+---------------------------------------------------------------------------------------------+
|  注意 不可使用双引号                                                                          
+---------------------------------------------------------------------------------------------+
	
  delete from [dbo].[Topic];--删除

 	 /*删除某条数据之后,让id 重新,从0开开始*/
  dbcc checkident(Topic,RESEED,0)

	/*变量的命名 if else while 的用法*/
	declare @num int=5
	
	if @num <6
		begin
			print @num
		end
	else
		begin
			print @num+1
		end
	
	while @num < 10
		begin
			set @num = @num+1  --给变量赋值
			print @num
		end
		

变量的赋值方式–declare

declare @str varchar(500) --注意:varchar类型后面要跟长度
set @str = 'Test'  --set 赋值方式
select @str = topicName from Topic where id = 1  --select 赋值方式
print @str

字符串拼接 --concat()

declare @All int,@num int = 1
select @All=count(*) from Topic --查询某张表中数据的行数
while @num < @All
begin
	print concat('----',@num ,'----')  --C#强类型,int和string不能直接拼在一起,需要使用 concat()
	set @num = @num+1
end

模糊查询

  select * from table like '%a'

  like '%A'      --搜索以A结尾的所有字符串
  like 'A%'      --搜索以A开头的所有字符串
  like '%A%'     --搜索任何含有A的字符串
  like '[a-f]'   --搜索a-f之间任何单个字符
  like '[^a-f]'   --搜索不在a-f之间任何单个字符

-----------------------------------------------------------------------12-27-------------------------------------------------------------------------

/*通过循环修改数据表中某些字段*/
declare @idNum int = 7
  while @idNum >0
  begin
	update Topic set topicName = concat('问题',@idNum) where id=@idNum
	update Topic set memo = concat('备注',@idNum) where id=@idNum
	set @idNum = @idNum - 1
  end

-----------------------------------------------------------------------12-28-----------------------------------------------------------------------

定义JSON批量插入

  /*定义数组批量插入*/
  declare @str varchar(max)='[
  {"name":"赵六","title":"我是赵六"},
  {"name":"粥吧","title":"我是粥吧"},
  {"name":"五五","title":"我是五五"},
  {"name":"版本","title":"我是版本"},
  {"name":"超出","title":"我是超出"}
  ]'
  
  insert into test1 (name,title)    --插入的写法
  select JSON_VALUE(value,'$.name') as name,JSON_VALUE(value,'$.title') as title from openjson(@str)

Left Join 以左表为主表,右表没有的数据显示null

在这里插入图片描述

Right Join 以右表为主表,左表没有的数据显示null

Inner Join 查出两张表共有的数据

在这里插入图片描述

删除某张表

	drop table Stu,Stu1,Stu2

delete和truncate 删除

  • delete可以删除表中的一条或多条数据,也可以删除全部数据;truncate只能将表中的全部数据删除。
  • delete删除表数据后,标识字段不能复用。即把id=10的数据删除了,不可能再插入id=10的数据
  • truncate删除表数据后,标识重新恢复初始状态。id可以从1开始,

-----------------------------------------------------------------------01-02-----------------------------------------------------------------------

COMPUTE 或 COMPUTE BY

GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。
在这里插入图片描述

不加group by
不加group by
GROUP BY子句有个缺点,就是返回的结果集中只有合计数据,而没有原始的详细记录。
如果想在SQL SERVER中完成这项工作,可以使用COMPUTE BY子句。

WITH AS 的用法

with as 短语,又称子查询部分,可以定义sql片段,会被整个sql语句用到

这是一个嵌套查询

查询Topic表中memo字段含有 1 的所有数据
select * from Topic where memo in
	(select memo from Topic where memo like '%1%')

运行结果如下
在这里插入图片描述

将上以上的嵌套查询替换成with as 写法

with
fn as         
(
	select memo from Topic where memo like '%1%'
)
select * from Topic where memo in (select * from fn)    --fn是一个公用表达式(CTE)
  • CTE后面也可以跟其他的CTE,但只能使用一个with,多个CTE中间用逗号(,)分隔
with
cte1 as
(
    select * from table1 where name like 'abc%'
),
cte2 as
(
    select * from table2 where id > 20
),
cte3 as
(
    select * from table3 where price < 100
)
select a.* from cte1 a, cte2 b, cte3 c where a.id = b.id and a.id = c.id

数据类型转换

cast()

CAST() 函数语法如下:
CAST (<expression> AS <data_ type>[ length ])

CONVERT()

CONVERT() 函数语法如下:
CONVERT (<data_ type>[ length ], <expression> [, style])

----------------------------------------------01-07------------------------------------------------------
在SQL SERVER中新建一张表,添加成功后编写SQL语句,执行成功,但是会有红色的提示信息,
如图:
在这里插入图片描述
虽然不影响执行,但是在总归怪怪的,这里有两种解决方案
一,修改sql语句

select * from [MyTest].[dbo].[stu]        --加上数据库名

二、使用use关键字

  use MyTest
  select * from stu

USE 在SQL SERVER 中的意思 将下面的数据库指定为某一数据库
数据库名称必须符合标识符的规则

猜你喜欢

转载自blog.csdn.net/marko_zheng/article/details/85274519