码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-12-09 16:57:10      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

 

--触发器学习

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

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!