Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6552661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:29:14+00:00 2026-05-25T12:29:14+00:00

Hi all i have been struggling this for a while now! I have some

  • 0

Hi all i have been struggling this for a while now! I have some data files with a date in them, these are dumped into a staging table. What i would like my script/function to do is to read the date for each record in the staging table and move to the appropriate table. Now i know i could do this very easily just using some static inserts, for example

INSERT INTO TABLE_2011_08
WHERE Datafields = 2011_08

However i want it to be dynamic, so i was thinking something along the lines of a function/stored procedure to pass in the date for each record. However my brain is melting a bit with this!

The data records in the staging table could be something like this:-

RecordA 2011-08-30 Data Data Data
RecordB 2011-08-31 Data Data Data
RecordC 2011-09-01 Data Data Data
RecordD 2011-09-02 Data Data Data
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:29:15+00:00Added an answer on May 25, 2026 at 12:29 pm

    The table T is similar to your table, I populated it with test data close to yours, the tables you populate will be created if they don’t exist.

    An attempt to recreate your table

    CREATE TABLE T(name varchar(10), date datetime)
    
    insert t values('RecordA','2011-08-30')
    insert t values('RecordB','2011-08-31')
    insert t values('RecordC','2011-09-01')
    insert t values('RecordD','2011-09-02')
    

    This syntax will if needed create and populate tables like TABLE_YYYY_MM. YYYY and MM is any combination found it table T

    Declare @tablename varchar(64)
    Declare @sql as varchar(max)
    Declare @d as datetime
    Declare dCursor CURSOR FAST_FORWARD FOR
    SELECT DISTINCT dateadd(month, datediff(m, 0, date), 0) date from t
    OPEN dCursor
    FETCH NEXT FROM dCursor
    INTO @d
    WHILE @@FETCH_STATUS = 0
    BEGIN
    SET @tablename = '[TABLE_'+replace(CONVERT(VARCHAR(7), @d, 121), '-', '_') + ']'
    SET @SQL = 
    'if OBJECT_ID('''+@tablename+''', ''U'') is null 
    BEGIN
    Declare @sql2 varchar(max)
    SET @sql2 = ''SELECT * INTO '+@tablename+'
    FROM t WHERE 1 = 2''
    EXEC(@sql2)
    END
    INSERT INTO '+ @tablename+'
    SELECT * FROM t
    WHERE datediff(m, 0, date)=' + CAST(datediff(m, 0, @d) AS VARCHAR(10))
    
    EXEC(@SQL)
    FETCH NEXT FROM dCursor
    INTO @d
    
    END
    CLOSE dCursor
    DEALLOCATE dCursor
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iv been struggling with this for a while now an I have exhausted all
I have been struggling with this for a while now. I have a Master
I have been struggling with this for some time now scanning the net for
Good Morning All. I've been struggling with this issue for a while now, and
Okay, I've been struggling with this for a while now. I have a standard
I have been struggling with this all day. I have an html table in
I've been struggling with Zend_Navigation all weekend, and now I have another problem, which
I have been trying all day to get some data properly formatted in a
I've been struggling with this issue for a while now. Maybe you can help.
I've been struggling for this for a while now. I'm trying to gear up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.