Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I've a SQL table of Data which doesn't have any specific column for Row updated date or time. Now i want know when the row was inserted.!!

Thanx in Advance.
Posted

Try to use this function,

SQL
SELECT *,fn_TablesLastUpdateDate(0) as InsertedDate from yourtable


SQL
CREATE FUNCTIOn fn_TablesLastUpdateDate(@Date NVARCHAR(20))

RETURNS @table TABLE(TableName NVARCHAR(40), LastUpdated Datetime)

AS

BEGIN


IF(@Date='') OR (@Date Is Null) OR (@Date='0')

    BEGIN
        INSERT INTO @table
        SELECT TOP 100 PERCENT TABLENAME,LASTUPDATED FROM 
        (
            SELECT  B.NAME AS 'TABLENAME', MAX(STATS_DATE (ID,INDID)) AS LASTUPDATED
            FROM    SYS.SYSINDEXES AS A
                    INNER JOIN SYS.OBJECTS AS B ON A.ID = B.OBJECT_ID
            WHERE   B.TYPE = 'U'  AND STATS_DATE (ID,INDID) IS NOT NULL 
            GROUP BY B.NAME
        ) AS A
        ORDER BY LASTUPDATED DESC
    END
ELSE

    BEGIN
        INSERT INTO @table
        SELECT TOP 100 PERCENT TABLENAME,LASTUPDATED FROM 
        (
            SELECT  B.NAME AS 'TABLENAME', MAX(STATS_DATE (ID,INDID)) AS LASTUPDATED,
                    CONVERT(VARCHAR, MAX(STATS_DATE (ID,INDID)), 103) as Date
            FROM    SYS.SYSINDEXES AS A
                    INNER JOIN SYS.OBJECTS AS B ON A.ID = B.OBJECT_ID
            WHERE   B.TYPE = 'U'  AND STATS_DATE (ID,INDID) IS NOT NULL 
            GROUP BY B.NAME
        ) AS A
        WHERE Date=@Date
        ORDER BY LASTUPDATED DESC
    END
RETURN

END
 
Share this answer
 
v3
Comments
Sandeep Mogaveer 21-Oct-13 7:29am    
I've execute the function first and it came under Table-Valued Functions. But when called select query(SELECT *,dbo.fn_TablesLastUpdateDate(0) as InsertedDate from LCODE) it's throwing Error as Below..


Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fn_TablesLastUpdateDate", or the name is ambiguous.
hi,
if you want to check updated date and time of the sql row, use the query as below:
SQL
SELECT top 10 col.name 'ColumnName', MAX(STATS_DATE (ID,INDID)) 'DateTime'
    FROM    SYS.SYSINDEXES as Ind
INNER JOIN SYS.columns as Col ON Ind.ID = Col.OBJECT_ID
WHERE STATS_DATE (ID,INDID) IS NOT NULL and Col.name like '%ColummnName%'
GROUP BY Col.name


hope it helps.
 
Share this answer
 
Comments
Sandeep Mogaveer 21-Oct-13 7:31am    
Thank you. But Looking for create Date and Time!!
Tom Marvolo Riddle 21-Oct-13 7:38am    
Then use getdate().
Sandeep Mogaveer 21-Oct-13 8:53am    
Row insert Datetime , not current date and time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900