触发器-当表1插入数据时将表1的数据插入表2

--触发器学习

ALTER trigger 触发器名 on 表1
for insert
as
begin
if (select count(1) from 表1)=0
print '未插入数据'
else
insert into 表2(字段 )select 对应字段 from inserted
end

--存储过程学习

(1)分页

ALTER procedure 存储过程名(
@pageIndex int,
@pageSize int
)
as
declare @startRow int, @endRow int
set @startRow = (@pageIndex - 1) * @pageSize +1
set @endRow = @startRow + @pageSize -1
select 字段名称 from (
select *, row_number() over (order by id asc) as number from 表名
) t
where t.number between @startRow and @endRow;

(2)从第几个开始取数据到第几个

ALTER proc [dbo].[pro_page]
@startIndex int,
@endIndex int
as
select count(*) from 表名;
select * from (
select row_number() over(order by id) as rowId, * from 表名
) temp
where temp.rowId between @startIndex and @endIndex

(3)显示表中用户最后一次操作数据

select a.*from test a
inner join (select username ,[time]=max(time)from test group by username)b
on a.username=b.username and a.time=b.time

原文地址:https://www.cnblogs.com/gqrbkw/p/5033224.html