use [c6]
go/****** 物件: storedprocedure [dbo].[full_search] 指令碼日期: 08/11/2011 09:35:43 tzjly******/
set ansi_nulls on
goset quoted_identifier on
goalter proc [dbo].[full_search](@string varchar(50))
as begin
declare @tbname varchar(50)
declare tbroy cursor for select name from sysobjects
where xtype= 'u ' --第乙個游標遍歷所有的表
open tbroy
fetch next from tbroy into @tbname
while @@fetch_status=0
begin
declare @colname varchar(50)
declare colroy cursor for select name from syscolumns
where id=object_id(@tbname) and xtype in (
select xtype from systypes
where name in ( 'varchar ', 'nvarchar ', 'char ', 'nchar ') --資料型別為字元型的字段
) --第二個游標是第乙個游標的巢狀游標,遍歷某個表的所有字段
open colroy
fetch next from colroy into @colname
while @@fetch_status=0
begin
declare @sql nvarchar(1000),@j int
select @sql= 'select @i=count(1) from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%'''
exec sp_executesql @sql,n'@i int output',@i=@j output --輸出滿足條件表的記錄數
if @j> 0
begin
select 包含字串的表名=@tbname
--exec( 'select distinct '+@colname+' from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''')
endfetch next from colroy into @colname
end
close colroy
deallocate colroy
fetch next from tbroy into @tbname
end
close tbroy
deallocate tbroy
end
exec full_search '2011-08-16'
sql 遍歷所有表中 某項 值為已知數的查詢語句
輸出表名 注 把下面儲存過程寫入想查詢的資料庫儲存過程中,然後在查詢分析器中執行 exec full search 123 create proc full search string varchar 50 as begin declare tbname varchar 50 declare tbr...
sql查詢資料庫中包含某個值的所有表
統共分三步 第1步 建立乙個表,用來儲存包含該值的資料表和列名 create table dest table name varchar 60 column name varchar 60 第2步 拼接insert語句 select concat insert into dest select ta...
SQL 建立索引,遍歷資料庫所有表
檢視資料庫表占用的磁碟空間 執行儲存過程 exec sp spaceused tablename 建立聚簇索引 create clustered index indexname on tablename columnname 不允許有重覆記錄 create index clustered index...