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 8882189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:30:52+00:00 2026-06-14T20:30:52+00:00

I have a temporary table that hold all items available #TableA ItemId ItemName ItemVer

  • 0

I have a temporary table that hold all items available

#TableA

ItemId  ItemName    ItemVer
1       Name1       2
2       Name2       9
3       Name3       5
.
.
.
500     Name500     3

I then have another that holds all the dates a Snapshot was taken along with the SnapshotId

CREATE TABLE #tmpSnapshot_Dates(
[SnapshotId] [uniqueidentifier] NOT NULL,
[DateTaken] datetime NOT NULL) 

INSERT INTO #tmpSnapshot_Dates SELECT SnapshotId, DateTaken From Snapshot_Info Where DateTaken <= @EndDate

Now for each date in the #tmpSnapshot_Dates table I want to get a count that relates it each Item

SELECT ItemId, Count(*) From Items Where SnapshotId = @SnapshotId

And then update #TableA by adding a column

DECLARE @ColName VARCHAR(100)
SET @ColName = 'Installed ' + CONVERT(Varchar, @DateA, 6)

exec('ALTER TABLE #TableA ADD ['+@ColName+']  int NOT NULL CONSTRAINT [DF_#TableA_Installed on] DEFAULT 0')

And then insert the data into the new column. I then need to repeat this for each date in #tmpSnapshot_Dates

giving a resulting #TableA that looks something like

#TableA

ItemId  ItemName    ItemVer    Installed 01 Jan 12    Installed 07 Feb 12 
1       Name1       2          34                     33
2       Name2       9          56                     59
3       Name3       5          12                     26
.
.
.
500     Name500     3          98                     106

My questions are

  1. Am I creating a nightmare here by approaching it the wrong way?
  2. How am I best to loop through #tmpSnapshot_Dates and then get the data to create the next column? I always try to avoid cursors but is this one of those situation I need to use one?

There could be 12 columns to add and not every item will be returned for each date.

  • 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-06-14T20:30:53+00:00Added an answer on June 14, 2026 at 8:30 pm

    Definitely it won’t be nice, but yes, you need to use cursors and iterate over the #tmpSnapshot_Dates, but if you have the option i suggest you to use a reporting software with the capability to create pivot grids.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to select all the rows from a temporary table that have a
I have a temporary table that I'm using and I have a unique column,
I have a temporary table that contains a part category and the associated part
I have read that creating a temporary table is best if the number of
I have a stored procedure inside which I create a temporary table that typically
So i have a temporary table that I need to populate with the records
I have a temporary table I'm creating in a sproc that houses my user
I have one temporary table that contains userID and taskID . It is called
I have a temporary table (or, say, a function which returns a table of
I have problems filling a temporary table with the resultset from a procedure call

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.