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
 Find the Table

Author  Topic 

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2011-03-09 : 07:43:08
hello everybody,
i have got a weird requirement,could anybody help me.
i have about 300 tables in my database.
i have column 'CREATED_BY' and 'CREATED_DATE' in every table.
now the requirement is i need to find the tables which have the value as 100 for 'CREATED_BY' and '2011-01-24 00:00:00.000' for 'CREATED_DATE'.


Thanx in advance.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-09 : 07:54:02
declare @sql varchar(max)
select @sql=isnull(@sql,'')
+ replace('if exists(select * from [ ! ] where Created_BY = 100 and Created_date = ''20110124'' )
INSERT INTO someTable Values( '' ! '') ',
' ! ', table_name)
from INFORMATION_SCHEMA.TABLES c
where table_type = 'BASE TABLE'


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2011-03-09 : 08:35:04
Iam unable to understand the script..could u please tell me do i need to change anything else in the script.
when i execute the script it shows this

The name 'if exists(select * from [INV_STOCK_TRANSFER_REQUEST_DETAIL] where Created_BY = 100 and Created_date = '20110124' )
INSERT INTO someTable Values( 'INV_STOCK_TRANSFER_REQUEST_DETAIL') if exists(select * from [INV_MATERIAL_STOCK] where Created_BY = 100 and Created_date = '20110124' )
INSERT INTO someTable Values( 'INV_MATERIAL_STOCK') if exists(select * from [RD_SIGNATORIES] where Created_BY = 100 and Created_date = '20110124' )
INSERT INTO someTable Values( 'RD_SIGNATORIES') if exists(select * from [OT_THEATRE_SURGICAL_DISCIPLINE_MAPPING] where Created_BY = 100 and Created_date = '20110124' )
INSERT INTO someTable Values( 'OT_THEA' is not a valid identifier.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-09 : 09:09:24
[code]declare @sql varchar(max)
select @sql=isnull(@sql,'')
+ replace('if exists(select * from [ ! ] where Created_BY = 100 and Created_date = ''20110124'' )
PRINT '' ! '' ',
' ! ', table_name) +char(10) + char(13)
from INFORMATION_SCHEMA.TABLES c
where table_type = 'BASE TABLE'

PRINT ( @SQL)[/code]
This will make it easier to read. If everything looks okay, change the PRINT @SQL to EXEC(@sql) or just copy the results and run it. Note: I removed the INSERT INTO statement and replaced it with print, as I don't really know your needs.

Jim

You may want to go thru tools\options\query results\Results To Text and expand how many characters are displayed in the results window.

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-03-09 : 10:49:28
see
http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -