how to move one table data to another table in sql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki123
    New Member
    • Mar 2010
    • 9

    how to move one table data to another table in sql server

    I have 2 table in sql server freetrial and freetrial_backu p, freetrial table contain startdate and enddate when enddate is over it should transfer all data to freetrial_backu p table pls help me(startdate is registrationdat e and enddate is 2days after registrationdat e)
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32661

    #2
    There is no MOVE feature as such. You would need two SQL strings. One to copy the selected data, and another to delete it from the source.
    Code:
    INSERT INTO [FreeTrial_Backup]
    SELECT *
    FROM   [FreeTrial]
    WHERE  DateAdd(day, 2, [RegistrationDate]) > 2
    Code:
    DELETE
    FROM   [FreeTrial]
    WHERE  DateAdd(day, 2, [RegistrationDate]) > 2

    Comment

    • viki123
      New Member
      • Mar 2010
      • 9

      #3
      thanks for answer but i have mention when enddate is over than only it should transfer the data (if i run above query it transfer all the data from one table to another table table??? enddate is after 2 days of registrationdat e of user)

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        That's because I left a very small, but critically important, bit out of the SQL (oops). It should have been comparing it with the current date (GETDATE()) rather than 2.
        Code:
        INSERT INTO [FreeTrial_Backup]
        SELECT *
        FROM   [FreeTrial]
        WHERE  DateAdd(day, 2, [RegistrationDate]) > GETDATE()
        Code:
        DELETE
        FROM   [FreeTrial]
        WHERE  DateAdd(day, 2, [RegistrationDate]) > GETDATE()
        Try that & let us know how it works.

        Comment

        • viki123
          New Member
          • Mar 2010
          • 9

          #5
          thanks 4 reply but it doesnt work i m explaining u what i need:-
          table has three column (name, registrationdat e, enddate)
          when some one fills form data is inserted in table freetrial
          registrationdat e is currentdate when user submits the form and enddate is plus 2days of registrationdat e)
          when enddate is over it should MOVE that record to freetrial_backu p table pls help me....

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32661

            #6
            Viki,

            I'm trying to help, but I can only work within the information provided.

            Firstly, there is no good reason to store [EndDate] if it is simply [RegistrationDat e] + 2 days. See Normalisation and Table structures for a full explanation of this.

            From what you've told me so far, the value returned by DateAdd(day, 2, [RegistrationDat e]) should be the same as your [EndDate] value anyway. It seems this doesn't work. Unfortunately that's all you've shared. If you could post what happens when you try this. including the code you've used to invoke this SQL and any associated error messages then I will happily give some more consideration to what may be going wrong for you.

            Comment

            • viki123
              New Member
              • Mar 2010
              • 9

              #7
              i have one form.asp page it inserts data in database
              in database i have table freetrial and freetrial_backu p
              freetrial table has startdate and enddate column (startdate = whn users submits form ) and enddate is plus 2 days of startdate
              when enddate is not equal to currentdate it should MOVE the record to freetrial_backu p table i think so it can be done by SQL JOB but i have not extact code 4 it pls help me.....

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32661

                #8
                I will try to help you when you have provided what I've requested.

                It's very hard to help if you don't pay attention to what I say.

                Comment

                Working...