jo***************@yahoo.com (JTWhaler) wrote in message news:<ed*************************@posting.google.c om>...
I'm looking for an efficient t-sql script to loop through all user
tables in a db and determine/print the value of each row having a
datetime field (including cases where there are multiple datetime
fields per row)
Help greatly appreciated..
Hi Jonah
The attached T-sql will create a database table that contains an entry
for each datetime column in the database
drop table datetime_column
create table datetime_column
(
table_name varchar(200)
,column_name varchar(200)
,datetime_value datetime null
)
declare @table_schema as varchar(200)
declare @table_name as varchar(200)
declare @column_name as varchar(200)
declare @query as nvarchar(1000)
DECLARE column_cursor CURSOR FOR
SELECT
table_name, column_name
FROM
INFORMATION_Schema.columns
where
data_type = 'datetime'
and table_name != 'datetime_column'
open column_cursor
FETCH NEXT FROM
column_cursor
INTO
@table_name, @column_name
while @@fetch_status = 0
begin
select @query = ' insert into datetime_column select ' + '''' +
@table_name + '''' + ',' + '''' + @column_name + '''' + ',' +
@column_name + ' from ' + @table_name
--select @query
exec sp_executesql @query
FETCH NEXT FROM
column_cursor
INTO
@table_name, @column_name
end
close column_cursor
deallocate column_cursor
select * from datetime_column