您的位置:首页 > 数据库

sqlserver中使用查询分析器的一点小技巧

2007-09-01 14:13 316 查看
begin tran

DROP proc sp_alterProc

GO

/*

说明:修改某一存储过程,可以是视图(这两者用得最多,当然也可以扩展为其他的,如触发器)

用途:针对经常写存储过程的人,使用在查询分析器中,在查询分析器的“工具-〉自定义”中设一快捷键(我使用的Ctrl+9),存储过程设为"sp_alterProc"(注意:前后都不能有空格)

在查询分析器的编辑窗口中选中某一存储过程的名字,按设定的快捷键,可显示出该存储过程的完整代码,

这样可以以最快的方式打开并修改存储过程,提高修改存储过程的速度,不需要手动去找到某一存储过程,然后打开。(强烈推荐)

作者:yx

*/

CREATE proc sp_alterProc

@procName varchar(255)

as

set nocount on

create table #t(a nvarchar(255))

/*之所以要先删除存储过程是因为无法得到存储过程的修改时间,如果删除再创建就可以将创建时间当成是修改时间,这样就知道存储过程好久被修改过了*/

insert into #t(a)

select 'begin tran'

union

select case when exists(select * from sysobjects where name = @procName and xtype = 'V') then 'DROP view ' else 'DROP proc ' end + @procName

union

select 'GO'

insert into #t(a) exec sp_helptext @procName

insert into #t(a)

select 'GO'

union

select 'if @@error<>0 rollback tran else commit tran'

/*如果直接select * from #t将输出结果显示为表格,有的字符会被转化,比如TAB变成了空格,使用游标的目的是防止这些字符被转变,然后好拷贝、粘贴、修改*/

declare @sql nvarchar(255) /*不管一行代码多长都能显示出来。为什么要定义成nvarchar(255)可以查看sp_helptext的源代码*/

declare mycursor cursor for select a from #t

open mycursor

fetch next from mycursor into @sql

while @@fetch_status = 0 begin

print @sql

fetch next from mycursor into @sql

end

close mycursor

deallocate mycursor

drop table #t

set nocount off

GO

if @@error<>0 rollback tran else commit tran

begin tran

DROP proc sp_select

GO

/*

说明:显示某一表或视图的数据

用途:针对经常写存储过程的人,使用在查询分析器中,在查询分析器的“工具-〉自定义”中设一快捷键(我使用的Ctrl+7),

在查询分析器的编辑窗口中选中某一表的名字,按设定的快捷键,可显示出该表的所有数据,

这样可以以最快的方式打开表的数据,不需要手动去找到某表,然后再打开。(强烈推荐)

作者:yx

*/

create proc sp_select

@tableName varchar(255)

as

exec('select * from ' + @tableName)

GO

if @@error<>0 rollback tran else commit tran
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: