热门文章
 
sql求两字符串相似度的自定
在 Access 中使用“存
ASP调用带参数存储过程
ASP与存储过程
实用的存储过程1
实用的存储过程2
SQL Server编写存储
SQL Server编写存储
在ASP存储过程的使用方法实
asp使用存储过程初步技巧
 推荐文章
 
asp使用存储过程初步技巧
ASP调用带参数存储过程
sql求两字符串相似度的自定
在 Access 中使用“存
ASP与存储过程
实用的存储过程1
实用的存储过程2
SQL Server编写存储
数据分页的存储过程
在ASP存储过程的使用方法实
存储过程入门到精通
asp调用orcle存储过程
存储过程介绍 和它在asp中
ASP和MSSQL存储过程的
系统存储过程sp_depen
asp:SQL存贮过程样板
 
你现在的位置:您现在的位置是: 中国ASP>>数据库>>存储过程
实用的存储过程2

笔者工作的公司采用的是SQLServer数据库,每天都要处理大量的数据,由于笔者进公司的时间比较晚,公司现有的大部分的程序都是以前的程序员留下的,因为他们没有相关的文档,笔者对于后台数据库的很多表的结构和数据都不甚了解,给日常的维护造成了很大的麻烦。

在对后台数据库进行研究的过程中,我需要得到数据库的某些相关信息,比如,公司的数据库中有几个表存放笔者的个人资料,像人事表、工资表、部门表等等,但具体是哪些表,就不是很清楚了,如果要一个一个表地找,可能天亮了也找不完,所以我决定做一个通用的存储过程,能对当前数据库所有字符型字段进行遍历,找出精确匹配含有要查找字符串的表和字段,并且罗列出来。比如,人事表的Name字段,工资表的Salary_Name字段,部门表的Employe_Name字段都有笔者的名字,我希望能把这些找出来。存储过程如下:

IF EXISTS (SELECT name FROM sysobjects

WHERE name = 'searchname' AND type = 'P')

DROP PROCEDURE searchname

Go

create procedure searchname @sname varchar(10)

As

begin

create table #TableList(

tablename char(200),

colname char(200)

)



declare @table varchar(200)

declare @col varchar(200)



set nocount on

declare curTab scroll cursor for select name from sysobjects where xtype='u'

open curTab

fetch next from curTab into @table

while @@FETCH_STATUS=0

begin

declare curCol scroll cursor for select name from syscolumns where (xtype=175 or xtype=167) and (id in (select id from sysobjects where name=@table))

open curCol

fetch next from curCol into @col

while @@FETCH_STATUS=0

begin

execute('insert into #TableList select ''+@table+'',''+@col+'' from '+@table+' where '+@col+'=''+@sname+'')

fetch next from curCol into @col

end

close curCol

deallocate curCol

fetch next from curTab into @table

end

close curTab

deallocate curTab

set nocount off

select distinct * from #TableList

drop table #tablelist

end


相关信息:

数据库查询语言sql(2)
数据库查询语言SQL(1)
sql求两字符串相似度的自定义函数
在 Access 中使用“存储过程”
ASP调用带参数存储过程
ASP与存储过程
实用的存储过程1
实用的存储过程2
SQL Server编写存储过程小工具

 

中国ASP技术 ASP.ORG.CN 版权所有 2004-2008