游标与仓储过程
create proc getTopNumByBtype
@number int
as
declare @btype varchar(50)
declare book_master_cursor cursor for
select distinct btype from book_master
open book_master_cursor
FETCH NEXT FROM book_master_cursor
WHILE @@FETCH_STATUS = 0
BEGIN
--select top 10 * from book_master where btype =
FETCH NEXT FROM book_master_cursor into @btype
select top 10 * from book_master
where btype = @btype
order by viewcount desc
END
CLOSE book_master_cursor
DEALLOCATE book_master_cursor
@number int
as
declare @btype varchar(50)
declare book_master_cursor cursor for
select distinct btype from book_master
open book_master_cursor
FETCH NEXT FROM book_master_cursor
WHILE @@FETCH_STATUS = 0
BEGIN
--select top 10 * from book_master where btype =
FETCH NEXT FROM book_master_cursor into @btype
select top 10 * from book_master
where btype = @btype
order by viewcount desc
END
CLOSE book_master_cursor
DEALLOCATE book_master_cursor
---------------------------------------
存储过程
1 批量插入数据,适合于数据有规律
drop proc insertBat
create proc insertBat
@begin_index int,
@end_index int
as
while(@end_index-@begin_index > 0)
begin
insert into book_read_log values(@begin_index,@begin_index+1000,(convert(varchar(19),getdate(),120)))
set @begin_index = @begin_index +1
end
create proc insertBat
@begin_index int,
@end_index int
as
while(@end_index-@begin_index > 0)
begin
insert into book_read_log values(@begin_index,@begin_index+1000,(convert(varchar(19),getdate(),120)))
set @begin_index = @begin_index +1
end
---------------------------------------------------
细节:
1 在过程或是触发器中,不允许使用use命令。
2 在查询分析器中,结果列的字符长度默认是256个字符的。有些时候,我们以为查询的字段就只有256个字符,其实不是。
3 记排序的长降,down--desc
--------------------------------------------------------
常用的查询语句:
1 取得表的字段腳本
select * from dbo.syscolumns where id in ( select id from dbo.sysobjects where xtype='U' and name='BcRate' )
2 查看数据库中的触发器:
select * from sysobjects where xtype = 'TR' 可以查看sysobjects表中xtype字段的介绍来查询不同的信息。
3 查询服务器中,有多少个数据库。
exec sp_helpdb