Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Update new date in database

Update new date in database

Scheduled Pinned Locked Moved Database
databasequestiondebuggingannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fu Manchu
    wrote on last edited by
    #1

    Hi all, just got to find an easier way, can any tell me if you can update the entire database with a new time in the timestamp field. i've got this project but the data is 14month old, it would be good if the data could be 3 or 4 days old just to debug or test a few things. Anyway, ive been using this:- select dateadd(d,430,timestamp)from alarm_log this brings back the original table plus 430 days. So how do i update this data back into the table, cause i have nothing in the table to generate a whereclause. secondly, once ive got one table updated... i could use sysobjects to find all the tables in the database and then loop through them...but how! - im sure i read somewhere that sql doesn't store a row number, there is no such thing, but surely there must be a way to interate through a table list? Cheers Andy

    M E 2 Replies Last reply
    0
    • F Fu Manchu

      Hi all, just got to find an easier way, can any tell me if you can update the entire database with a new time in the timestamp field. i've got this project but the data is 14month old, it would be good if the data could be 3 or 4 days old just to debug or test a few things. Anyway, ive been using this:- select dateadd(d,430,timestamp)from alarm_log this brings back the original table plus 430 days. So how do i update this data back into the table, cause i have nothing in the table to generate a whereclause. secondly, once ive got one table updated... i could use sysobjects to find all the tables in the database and then loop through them...but how! - im sure i read somewhere that sql doesn't store a row number, there is no such thing, but surely there must be a way to interate through a table list? Cheers Andy

      M Offline
      M Offline
      mr_lasseter
      wrote on last edited by
      #2

      1. If you are updating ever row in the table you don't need a where clause update alarm_log set timestamp = dateadd(d,430,timestamp) 2. Use a cursor. See Google or help in Query Analyzer for more info.

      Mike Lasseter

      1 Reply Last reply
      0
      • F Fu Manchu

        Hi all, just got to find an easier way, can any tell me if you can update the entire database with a new time in the timestamp field. i've got this project but the data is 14month old, it would be good if the data could be 3 or 4 days old just to debug or test a few things. Anyway, ive been using this:- select dateadd(d,430,timestamp)from alarm_log this brings back the original table plus 430 days. So how do i update this data back into the table, cause i have nothing in the table to generate a whereclause. secondly, once ive got one table updated... i could use sysobjects to find all the tables in the database and then loop through them...but how! - im sure i read somewhere that sql doesn't store a row number, there is no such thing, but surely there must be a way to interate through a table list? Cheers Andy

        E Offline
        E Offline
        Eric Dahlvang
        wrote on last edited by
        #3

        It is a strange practice to name your field timestamp and then fill them with a datetime or smalldatetime data type. I'm assuming that your timestamp field is in fact a datetime or smalldatetime, because the timestamp data type is binary - and would not update without error using the dateadd as you show. Plus, the timestamp data type is updated automaticaly when you change the value of a field. Anyway, if your timestamp fields are in fact dates, this should work:

        DECLARE @cSQL varchar(100)
        DECLARE @cTableName varchar(100)

        SELECT @cSQL = ''

        DECLARE TableNames CURSOR FOR
        SELECT DISTINCT table_name
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE column_name = 'timestamp'

        OPEN TableNames
        FETCH NEXT FROM TableNames INTO @cTableName
        WHILE @@FETCH_STATUS = 0
        BEGIN
        EXEC('UPDATE ' + @cTableName + ' SET timestamp = DATEADD(d,430,timestamp)')

        FETCH NEXT FROM TableNames INTO @cTableName 
        

        END

        CLOSE TableNames
        DEALLOCATE TableNames

        --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups