Create Proc Proc_Find
--存储过程变量
@aName As Varchar(50)
As
--Set @aName='db_Pm'--要查找的字段名称
--游标变量
Declare @tableName As Varchar(50)
Declare cursorTablename Cursor
For(Select Name From DataBaseName..sysobjects Where xType='U')
Open cursorTablename
Fetch Next From cursorTablename
Into @tableName
While @@Fetch_Status=0
Begin
if exists(Select * From dbo.syscolumns Where Name= @aName And Id=Object_ID(@tableName))
Select @tableName
Else
Print '不存在'
Fetch Next From cursorTableName
Into @tableName
End
Close cursorTablename
Deallocate cursorTablename
--存储过程变量
@aName As Varchar(50)
As
--Set @aName='db_Pm'--要查找的字段名称
--游标变量
Declare @tableName As Varchar(50)
Declare cursorTablename Cursor
For(Select Name From DataBaseName..sysobjects Where xType='U')
Open cursorTablename
Fetch Next From cursorTablename
Into @tableName
While @@Fetch_Status=0
Begin
if exists(Select * From dbo.syscolumns Where Name= @aName And Id=Object_ID(@tableName))
Select @tableName
Else
Print '不存在'
Fetch Next From cursorTableName
Into @tableName
End
Close cursorTablename
Deallocate cursorTablename
本文介绍了一个使用SQL存储过程来查找指定字段名存在于哪些表中的方法。通过定义存储过程CreateProcProc_Find,利用游标遍历数据库中的所有用户表,并检查每个表是否包含指定的字段名称。

1028

被折叠的 条评论
为什么被折叠?



