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.
Author |
Topic |
RajJol
Starting Member
17 Posts |
Posted - 2010-09-24 : 08:40:38
|
0Hi,I have a database called 'Company' that holds around 100 tables. Each of these tables have a number of different columns with a number of different rows.I need to be able to check all tables within the database that holds a column called 'Date' (should only be in about 75 of these tables). Then I need to check, in each table, that the date matches today's date (2010-09-24).Can someone help me with this please??Any help will be much appreciated.Thank You. |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-24 : 10:41:19
|
use these two(apply join between the tables on object_id)SELECT * FROM sys.all_objectsSELECT * FROM sys.all_columnsquote: Originally posted by RajJol 0Hi,I have a database called 'Company' that holds around 100 tables. Each of these tables have a number of different columns with a number of different rows.I need to be able to check all tables within the database that holds a column called 'Date' (should only be in about 75 of these tables). Then I need to check, in each table, that the date matches today's date (2010-09-24).Can someone help me with this please??Any help will be much appreciated.Thank You.
|
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-28 : 10:29:24
|
quote: Originally posted by rohitvishwakarma use these two(apply join between the tables on object_id)SELECT * FROM sys.all_objectsSELECT * FROM sys.all_columnsquote: Originally posted by RajJol 0Hi,I have a database called 'Company' that holds around 100 tables. Each of these tables have a number of different columns with a number of different rows.I need to be able to check all tables within the database that holds a column called 'Date' (should only be in about 75 of these tables). Then I need to check, in each table, that the date matches today's date (2010-09-24).Can someone help me with this please??Any help will be much appreciated.Thank You.
OP wants to search for a particular date value tooMadhivananFailing to plan is Planning to fail |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-28 : 11:21:09
|
Here is the complete query get the columns having date as column name and whether they contains today's date or not.CREATE TABLE #temp(id INT IDENTITY(1,1),tableName VARCHAR(150),columnName VARCHAR(150),contains_todays_date bit)INSERT INTO #temp (tableName , columnName , contains_todays_date)SELECT O.name AS tableName,C.name AS columnName,0 FROM sys.all_objects O INNER JOIN sys.all_columns C ON O.object_id =C.object_idWHERE C.name like '%date%'--SELECT * FROM #tempDECLARE @count INTDECLARE @count_max INTSET @count = 1 SELECT @count_max = MAX(id) FROM #tempWHILE(@count < @count_max)BEGIN DECLARE @table_name VARCHAR(150) DECLARE @column_name VARCHAR(150) DECLARE @sql VARCHAR(1000) SELECT @table_name = tableName, @column_name = columnName FROM #temp WHERE id = @count SET @sql = 'IF EXISTS(SELECT 1 FROM ' SET @sql = @sql + @table_name + ' WHERE ' + @column_name + ' = ' +''''+ CAST(GETDATE() AS VARCHAR(20)) + ''')' SET @sql = @sql + ' BEGIN ' SET @sql = @sql + ' UPDATE #temp ' SET @sql = @sql + ' SET contains_todays_date = 1 ' SET @sql = @sql + ' WHERE id = ' + CAST(@count AS VARCHAR(100)) SET @sql = @sql + ' END ' --PRINT @sql EXEC(@sql) SET @count = @count + 1ENDSELECT * FROM #temp ThanksRohit |
|
|
RajJol
Starting Member
17 Posts |
Posted - 2010-10-04 : 06:34:48
|
quote: Originally posted by madhivanan You can make use of thishttp://beyondrelational.com/blogs/madhivanan/archive/2009/12/14/search-a-value-in-character-column-of-all-tables.aspxMadhivananFailing to plan is Planning to fail
Hi Madhivanan,I used this link you sent me but am still having a few problems. I modified the code slightly with success but it only works when I specify the table with the date column. If I specify a table which does not have the column it returns an error message. Please see my modified code belowdeclare @sql varchar(max),@search varchar(100) set @search='date' select @sql=@sql+'select ''Sector'' ''Source'', '''+ t.TABLE_NAME+''' ''TableName'', CONVERT(CHAR(10), InsertDate, 120) ''InsertDate'', DATEDIFF (d,InsertDate, GETDATE()) ''DaysOld'', COUNT(*) ''Count'' FROM'+' ['+t.TABLE_NAME+']'+' GROUP BY CONVERT(CHAR(10), InsertDate, 120), DATEDIFF (d,InsertDate, GETDATE()) union all 'FROM information_schema.tables as twheret.table_type='BASE TABLE' and t.TABLE_NAME in ('x_Updates') --data_type like '%char%' set @sql=left(@sql,len(@sql)-10) exec(@sql) |
|
|
|
|
|
|
|