Trouble copying data from old table to new table

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Trouble copying data from old table to new table

    Hello all,

    I am working with a ColdFusion MX 6.1 frontend and a SQL Server 2000 backend -

    There is a "CUSTOMER_INFO" table for every month and year stretching back
    approximately 2 years, ie:

    CUSTOMER_INFO_052005 (May 2005)
    CUSTOMER_INFO_042005 (April 2005)
    etc.

    I don't really want to bore anybody with details as to why we have so many
    CUSTOMER_INFO tables (though I can ramble on for hours about it if you'd like
    me to).

    I'm creating a scheduled job which will run on the first of every month and
    copy last month's CUSTOMER_INFO table into a newly created CUSTOMER_INFO table
    for the current month.

    Creating the new table is not a problem. The problem is selecting data from
    the table belonging to the month that just ended and putting it into the new
    table.

    Please examine the following code. I would very much appreciate it if
    somebody could tell me what I am doing wrong:


    /* Get a two character string to represent last month */

    DECLARE @Old_Month char(2)
    SET @Old_Month = DATEPART((month), GETDATE() -1)

    IF @Old_Month < 10
    SET @Old_Month = '0' + @Old_Month
    GOTO Proc_Cont

    Proc_Cont:


    /* Get a four character string to represent whatever year last month was in */

    DECLARE @Old_Year char(4)
    SET @Old_Year = DATEPART((year), GETDATE()) -1)


    /* Add the @Old_Month and @Old_Year variables to get a six character string
    representing last month and whatever year last month was in */

    DECLARE @Old_MonthYear char(7)
    SET @Old_MonthYear = @Old_Month + @Old_Year


    /* Prepend "CUSTOMER_INFO_" to the front of my 6 character month/year variable
    */

    DECLARE @Old_CustomerInfo char(20)
    SET @Old_CustomerInfo = 'CUSTOMER_INFO_' + @Old_MonthYear


    /* Now why won't the following SELECT INTO statement work? (assuming that I
    were to
    run this code on 5/1/05) */

    INSERT INTO CUSTOMER_INFO_052005
    (CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED )
    SELECT CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED
    FROM @Old_CustomerInfo


    /*And I've also tried the following: */

    INSERT INTO CUSTOMER_INFO_052005
    (CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED )
    SELECT CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED
    FROM 'CUSTOMER_INFO_' + CAST(DATEPART((month), GETDATE()-1) AS CHAR(2)) +
    CAST(DATEPART((year), GETDATE()-1) AS CHAR(4))


    /*And I've also tried this: */

    INSERT INTO CUSTOMER_INFO_052005
    (CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED )
    SELECT CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED
    FROM (SELECT name FROM sysobjects WHERE name = @Old_CustomerInfo)



    Please keep in mind that right after I've declared and set my
    @Old_CustomerInfo variable, if I run a PRINT @Old_CustomerInfo statement from
    within Query Analyzer, the output is CUSTOMER_INFO_042005 which is correct
    because I am running my code on 5/1 ( I have my computer's calendar turned
    back).

    I am guessing that since @Old_CustomerInfo is a CHAR variable somehow this is
    causing me a problem but I'm really not sure what's going on. I would greatly
    appreciate any help with this.

    Thanks,
    Mike





    RelentlessMike Guest

  2. Similar Questions and Discussions

    1. How to take data out of table, restructure the table and then put the data back in
      Hi All Wonder if you could help, I have a bog standard table called STOCKPRICES that has served me well for a while, but now I need to change the...
    2. Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table.
      We are getting this error after clearing the web.config of database infomation - even after using the wizard to re-enter the information. I could...
    3. Forms...Copying data from one table to another
      Using Access 2000, I have four tables and two forms. The first form will update the table "Master" with three fields, one of which is StudyNumber,...
    4. Changing a html table when new data is entered into SQL table.
      Hi all, I was wondering if anyone has any ideas on how I would go about this task. What I have is a html table which is populated from 2 different...
    5. Beginner - copying data from one table to another
      Hi, I want to copy rows from one table to another table where column names are the same for example Tab1 ID CHAR(10) NAME CHAR(30)
  3. #2

    Default Re: Trouble copying data from old table to new table

    Your first insert statemen looks fine. You shouldn't need to do any convert
    statements.
    What error message are you receiving? You're completely sure that all the
    fields are the same on both tables and that you didn't accidentally add
    something like and identity column to your customerid

    jmj Guest

  4. #3

    Default Re: Trouble copying data from old table to new table

    "...you didn't accidentally add something like and identity column to your
    customerid "

    Doh! I can't double-check this from where I am right now, but you may have
    hit the nail on the head my friend.

    Thanks very much,
    Mike

    RelentlessMike Guest

  5. #4

    Default Re: Trouble copying data from old table to new table

    Solved!

    The problem actually was not related to an identity column. But after much
    swearing and twitching at my desk I have found a solution and would like to
    share it with anybody who is interested:

    Instead of using this INSERT INTO query...

    INSERT INTO CUSTOMER_INFO_052005
    (CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED )
    SELECT CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED
    FROM @Old_CustomerInfo

    ...I needed to run the query using dynamic SQL:

    DECLARE @Insert_Var char(200)
    SELECT @Insert_Var = 'INSERT INTO CUSTOMER_INFO_052005
    (CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED ) ' +
    'SELECT CUSTOMER_ID,CUSTOMER_NAME,BILLABLE,SIZE_ALLOCATED ' + 'FROM ' +
    @Old_CustomerInfo
    EXEC (@Insert_Var)

    At any rate, thanks again for your help jmj.

    Mike

    RelentlessMike Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139