從整個資料庫進行搜尋的儲存過程

2021-04-13 13:48:17 字數 3332 閱讀 8790

對整個資料庫全部表的字段進行文字查詢,返回的結果為欄位名、表名、出現的次數

create            procedure        search_tablename         

@strword    varchar(50)         

as         

begin         

set        nocount        on         

create        table        #result         

(         

oid    int        not        null        identity,         

ziduan        varchar(50)            null,         

tablename    varchar(50)            null,         

ziduanleixing                varchar(50)            null,         

icount                                        int        null         

)         

declare        @count    int         

,@ziduanname        varchar(50)         

,@tablename        varchar(50)         

,@ziduanleixing        varchar(50)         

,@sql        nvarchar(800)         

declare        cur_locationlist        cursor        fast_forward        for         

select        a.name        ,b.name,c.name            from        syscolumns        a         

inner        join            sysobjects        b             

on        a.id=b.id        and        b.xtype='u'        and        lower(a.name)        not        in('desc','order','top','left','bottom','right')         

inner        join        systypes        c             

on            a.xtype=c.xusertype             

and        c.name        in('varchar','char','nvarchar','nchar','decimal','float','int','money')                 

open        cur_locationlist         

fetch        next        from        cur_locationlist        into        @ziduanname,@tablename,@ziduanleixing         

while        @@fetch_status        =        0         

begin         

set        @count=0         

if        @ziduanleixing='money'         

set        @sql='select        @a=count(*)        from        ['+@tablename+']        where        convert(varchar(20),['+@ziduanname+'])        like       ''%'+@strword+'%'''         

else         

set        @sql='select        @a=count(*)        from        ['+@tablename+']        where        ['+@ziduanname+']        like       ''%'+@strword+'%'''         

--print        @sql         

exec        sp_executesql        @sql,n'@a        int        output',@count        output         

--print        @count         

if        @count>=1         

begin             

insert        into        #result(ziduan,tablename,icount,ziduanleixing)        values        (@ziduanname,@tablename,@count,@ziduanleixing)         

end         

fetch        next        from        cur_locationlist        into        @ziduanname,@tablename,@ziduanleixing         

end         

close        cur_locationlist         

deallocate        cur_locationlist         

select        ziduan        as        "欄位名",tablename        as        "表名",icount        as        "出現次數",ziduanleixing        as        "字段型別"        from        #result         

order        by        "表名","欄位名"         

drop        table        #result         

set        nocount        off         

endgo

exec search_tablename '欲查詢值'

對整個資料庫進行查詢

最近工作中遇到乙個需求,要對整個資料庫中每個表的每個字段進行條件查詢。最後寫了一段 實現遍歷資料庫的所有表並對每個字段進行條件查詢。下面的 檢查欄位的值是否包含 http 是則記錄 表名欄位名 環境 sql server 2008 use database database為目標資料庫名稱 decl...

Oracle 備份整個資料庫 和 匯入整個資料庫

備份 exp使用者名稱 密碼 本地服務名 file 目標位址 exp user pwd file file.dmp owner youruser1 使用者名稱 owner a 要備份a使用者的資料 匯入 imp 使用者名稱 密碼 本地服務名 file 檔案的位置 ignore y ignore y的...

匯出oracle整個資料庫

1 將資料庫baitest完全匯出,使用者名稱system 密碼manager 匯出du到d daochu.dmp中zhi exp system manager test file d daochu.dmp full y 2 將資料庫dao中system使用者與sys使用者的表匯出 exp syst...