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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:34:13+00:00 2026-05-30T13:34:13+00:00

Is there any way I can add a column to a table but I

  • 0

Is there any way I can add a column to a table but I want the heading to be a date, and every new column added will have a column heading for the next day hence the

SET @date1 = @date1 + 1

What I want the table to look like is, where the date on top is a new column for each day the script loops:

StoreID StoreName     02/01/12    03/01/12    04/01/12
1234  Coles1         7512       8574        
1235  Coles2         7210       8441
1236  Coles3         4845       5448

When I run the script I get the following error messages:

Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ‘@Column’.
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ‘@Column’.

Here is my script:

DECLARE @date datetime
DECLARE @date1 datetime
DECLARE @date2 datetime
DECLARE @Column varchar(8)
SET @date = '02 Jan 2012'
SET @date1 = '02 Jan 2012'
SET @date2 = '08 Jan 2012'
SET @Column = CONVERT(VARCHAR(8), @date1, 3)

IF NOT EXISTS (SELECT * FROM sysobjects WHERE xtype = 'U' AND name = '#vl_temp_trans') 
BEGIN
   CREATE TABLE #vl_temp_trans
      (StoreID INT,
       StoreName VARCHAR(100),
       @Column MONEY)                  ----> column name to be date "@Column)
END

WHILE (@date1 <= @date2)
BEGIN
   SET @Column =  CONVERT(VARCHAR(8), @date1, 3)

   ALTER table #vl_temp_trans
   ADD @Column MONEY     ----> column name to be date "@Column" 

   Insert into #vl_temp_trans (storeID, storeName, @Column)
      select storeId, storeName, TotalDailyTransactions
      from daily_trans t1 (nolock) 
      full outer join outlets t2 (nolock) on t1.StoreID = t2.StoreID 
      where DailyEnd = @date1 + 1

   SET @date1 = @date1 + 1
END 
  • 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-30T13:34:14+00:00Added an answer on May 30, 2026 at 1:34 pm

    I think the problem is that you can’t use a variable to define a column name.

    I used some of your code to test with. This first part executed fine.

    DECLARE @date1 datetime
    DECLARE @Column varchar(8)
    SET @date1 = '02 Jan 2012'
    SET @Column = CONVERT(VARCHAR(8), @date1, 3)
    select @Column 
    

    But when I added the CREATE TABLE statement and executed all of it at once, then I got the same error you did.

    CREATE TABLE #vl_temp_trans
    (StoreID INT,
    StoreName VARCHAR(100),
    @Column MONEY)                  ----> column name to be date "@Column)
    

    To do this you will need to build up the CREATE TABLE and ALTER TABLE statements as a string and then execute those using EXECUTE or sp_executesql. A search for “dynamic sql” will also give you some article that describe this too.

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

Sidebar

Related Questions

Is there any way I can add a static extension method to a class.
Is there any way I can add this to my plugin? $this->_helper->viewRenderer->setNoRender();
Is there any way I can set a formatter on models that will convert
I know that you can use keywords with labels, but is there any way
Is there any way can declare a bean in just like JSP UseBean in
Is there any way we can fetch X509 Public Cetrificates using c# from AD
Is there any way I can use JPA or hibernate annotations to specify the
Is there any way I can do Select TableName.SomeColumn As SomeAlias FROM TableName In
Is there any way I can use RegExp or Wildcard searches to quickly delete
Is there any way I can run class files (i.e. with main as the

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.