Database SQL Advanced Usage

Table of contents

1. Usage of INSERT INTO SELECT

2. Logical control statement

 Three, the formula table expression

4. Stored program

5. Trigger


1. Usage of INSERT INTO SELECT

The INSERT INTO SELECT statement copies data from a table and then inserts the data into an existing table .

1. Copy all columns from one table and insert into another existing table

INSERT INTO 表2
SELECT * FROM 表1;

example

INSERT INTO student2
SELECT * FROM student;

The above SQL statement copies  the data in the   "student" table and inserts it into "student2"  .

2. Copy only the desired columns from one table and insert them into another existing table

INSERT INTO 表2
(列名1,列名2)
SELECT 列名1,列名2 
FROM 表1;

example

INSERT INTO student2
(ID,name)
SELECT ID,name 
FROM student;

The above SQL statement only copies  the "ID" and "name"  columns   in  "student" to "student2"  .

Reference: Advanced usage of database SQL (4) - Programmer Sought

2. Logical control statement

  if-else statement

--语法结构:
if( 条件表达式 )
	begin
		命令行或程序块
	end
else
	begin
	 	命令行或程序块
	end

   while-continue-break statement

--语法结构:
while(条件表达式)
	begin
		命令行或程序块
		[break]	→→ 跳出循环
		[continue] →→ 可以让程序跳过continue命令之后的语句
		命令行或程序块
	end

case statement

--语法结构:
case	
		when 条件表达式 then 运算式
		when 条件表达式 then 运算式
		[else 运算式]
	end

Example code:

--用法一
select case when Line='B01' then '00' else Line end from [ALL-Line_place]
--用法二
select case Line when 'B01' then '00' else Line end from [ALL-Line_place]

 Three, the formula table expression

  Treat a common table expression (CET) as a temporary result set, defined within the execution scope of a select, insert, update, or  statementdelete create view

--CET基本语法结构:
	with 公用表表名 (字段列表)
	as	( 命令行或程序块 )
	select * from 公用表表名

Example code:

;WITH TRR0 AS (select * from Line_Machine_tx),
 TRR1 AS(select * from Machine_tx),
 TRR2 AS(select * from [ALL-Line_place])
 select * from TRR0,TRR1,TRR2

4. Stored program

Creation location:

 

Create a stored procedure

USE [D_total]
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[proc_search_alm_machine_total]
   
	@linn varchar(255),@Time_Num varchar(255),@Startt varchar(255),@Endt varchar(255)	
AS
BEGIN
    --set @linn ='C02'
    --set @Time_Num ='Num'
    --set @Startt ='2021-08-20 08:00:00'
    --set @Endt='2021-08-26 20:00:00'
	declare @we varchar(8000)
	--declare @dj varchar(2000)
	declare @alm varchar(2000)
	declare @alm1 varchar(2000)	
	if @Time_Num ='Time'
	begin 
	set @alm ='BETWEEN '''+@Startt+''' and '''+@Endt+''' '
	set @we='执行代码'
	end
	else --num
	begin 
	set @alm ='条件'
    set @we='执行代码'
	end	
	EXEC(@we)
	--print(@we)
END
GO


Execution code:

exec proc_search_alm_machine_total @linn ='B11',@Time_Num ='Time',@Startt ='2021-12-06 08:00:00',@Endt='2021-12-06 20:59:59'

5. Trigger

Where the trigger is created:

 

create insert trigger

USE [D_total]
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[B01_cfq_after]
ON [dbo].[B01_adress_record]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN 
		insert into [B01_ALM_Data_Record]([Line],[Machine],[Adress],[Adress_num],[UpStart_time],[UpEnd_time],[Data],[Model])select INSERTED.Line,INSERTED.Machine,INSERTED.Adress,INSERTED.Adress_num,INSERTED.Up_time,INSERTED.Up_time,INSERTED.Data,INSERTED.Model  from INSERTED  
		where (INSERTED.Adress='M' OR INSERTED.Adress='WX') and INSERTED.Data=1;
		
END
GO

ALTER TABLE [dbo].[B01_adress_record] ENABLE TRIGGER [B01_cfq_after]
GO


Create a delete trigger

USE [D_total]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE TRIGGER [dbo].[B01cfq_alm_de]
ON [dbo].[B01_adress_record]
WITH EXECUTE AS CALLER
FOR DELETE
AS
BEGIN
IF(SELECT MIN(DELETED.Data) FROM DELETED WHERE ( DELETED.Adress='M' OR DELETED.Adress='WX'))=1
 BEGIN 
	DELETE  [B01_ALM_Data_Record] from [B01_ALM_Data_Record] inner join DELETED on [B01_ALM_Data_Record].Line=DELETED.Line and [B01_ALM_Data_Record].Machine=DELETED.Machine and [B01_ALM_Data_Record].Adress=DELETED.Adress and [B01_ALM_Data_Record].Adress_num=DELETED.Adress_num  and [B01_ALM_Data_Record].UpStart_time=DELETED.Up_time 
	where  [B01_ALM_Data_Record].Data=1  
	END
END
GO

ALTER TABLE [dbo].[B01_adress_record] ENABLE TRIGGER [B01cfq_alm_de]
GO

Create an update trigger

USE [D_total]
GO

/****** Object:  Trigger [dbo].[B01CFQ_upm]    Script Date: 2023/1/3 下午 03:58:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[B01CFQ_upm]
ON [dbo].[B01_adress_record]
WITH EXECUTE AS CALLER
INSTEAD OF INSERT
AS
BEGIN 
INSERT INTO B01_ALM_Data_Record([Line],[Machine],[Adress],[Adress_num],[UpStart_time] ,[UpEnd_time],[Data],[Model])
SELECT  INSERTED.Line,INSERTED.Machine,INSERTED.Adress,INSERTED.Adress_num,INSERTED.Up_time,INSERTED.Up_time,INSERTED.Data,INSERTED.Model 
from INSERTED 
LEFT JOIN Machine_adress ON Machine_adress.Machine=inserted.Machine AND Machine_adress.Adress=inserted.Adress AND Machine_adress.Adress_num=inserted.Adress_num
LEFT JOIN B01_ALM_Data_Record ON B01_ALM_Data_Record.Machine=inserted.Machine AND B01_ALM_Data_Record.Adress=inserted.Adress AND B01_ALM_Data_Record.Adress_num=inserted.Adress_num AND  B01_ALM_Data_Record.Data=inserted.Data 
WHERE inserted.Data=1 and Machine_adress.Data_Type='M2' AND B01_ALM_Data_Record.Machine IS NULL 
;
UPDATE B01_ALM_Data_Record SET B01_ALM_Data_Record.UpEnd_time=inserted.Up_time,B01_ALM_Data_Record.Data=inserted.Data 
FROM B01_ALM_Data_Record 
INNER JOIN inserted 
ON inserted.Machine=B01_ALM_Data_Record.Machine AND B01_ALM_Data_Record.Adress=inserted.Adress AND B01_ALM_Data_Record.Adress_num=inserted.Adress_num 
INNER JOIN Machine_adress 
ON Machine_adress.Machine=inserted.Machine AND Machine_adress.Adress=inserted.Adress AND Machine_adress.Adress_num=inserted.Adress_num
 WHERE inserted.Data=0 and Machine_adress.Data_Type='M2' and B01_ALM_Data_Record.Data=1

END
GO

ALTER TABLE [dbo].[B01_adress_record] ENABLE TRIGGER [B01CFQ_upm]
GO


Summary: This article mainly explains INSERT INTO SELECT , logic control statements, formula table expressions, stored procedures, trigger usage and simple examples of SQL database.

Guess you like

Origin blog.csdn.net/qq_42711010/article/details/128530671