Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how do we check data on all dbs and fields

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-11 : 10:56:52
Is there anyway to find the data on the database using cursor, i have more than 30 databases, i know the table name but i am not sure which fields has that value. Should i check the all databases and all fields? (I am sure, we have that record on our database, but i don't know in which database and which field)


USE Master
GO

DECLARE @name VARCHAR(50)
DECLARE db_cursor CURSOR FOR

SELECT [name]
FROM sys.Databases WHERE [name] NOT IN ('master','model','msdb','TempDb') order by [name]
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name

WHILE @@FETCH_STATUS = 0

BEGIN
Print 'Database : '+ @name + ' processing'
EXEC('Use [' + @Name +']
Select ID from tbl1 WHERE Field1=''XXXYYYZZZ''
')
FETCH NEXT FROM db_cursor INTO @name
END

CLOSE db_cursor
DEALLOCATE db_cursor

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-11 : 11:01:06
loop over databases and use below to search on all tables

http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-11 : 11:02:11
thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-11 : 11:05:52
welcome
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-11 : 11:38:08
If i create the "SearchAllTables" stored procedure on each db i can get the result. But i created that stored procedure on master table and execute above procedure....i do not get the result.
do i have create "SearchAllTables" for each db?
Or how to execute "stored procedure" which is created on master database. I tried to execute this way:

BEGIN
Print 'Database : '+ @name + ' processing'
EXEC('Use [' + @Name +']
EXEC master.SearchAllTables ''computer''
')

FETCH NEXT FROM db_cursor INTO @name
END

I got the error is :

Could not find stored procedure 'master.SearchAllTables'
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-06-11 : 11:56:50
Hi Rudba,

You could change the stored proc to use 3 part notation

I.E
<db_name>.information_schema.tables etc etc.

However, you'd then have to rewrite the sp using dynamic sql.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -