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

  • SEARCH
  • Home
  • 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 548869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:07:31+00:00 2026-05-13T11:07:31+00:00

Brief history: I’m writing a stored procedure to support a legacy reporting system (using

  • 0

Brief history:
I’m writing a stored procedure to support a legacy reporting system (using SQL Server Reporting Services 2000) on a legacy web application.
In keeping with the original implementation style, each report has a dedicated stored procedure in the database that performs all the querying necessary to return a “final” dataset that can be rendered simply by the report server.

Due to the business requirements of this report, the returned dataset has an unknown number of columns (it depends on the user who executes the report, but may have 4-30 columns).

Throughout the stored procedure, I keep a column UserID to track the user’s ID to perform additional querying. At the end, however, I do something like this:

UPDATE #result
SET Name = ppl.LastName + ', ' + ppl.FirstName
FROM #result r
LEFT JOIN Users u ON u.id = r.userID
LEFT JOIN People ppl ON ppl.id = u.PersonID

ALTER TABLE #result
DROP COLUMN [UserID]

SELECT * FROM #result r ORDER BY Name

Effectively I set the Name varchar column (that was previously left NULL while I was performing some pivot logic) to the desired name format in plain text.

When finished, I want to drop the UserID column as the report user shouldn’t see this.

Finally, the data set returned has one column for the username, and an arbitrary number of INT columns with performance totals. For this reason, I can’t simply exclude the UserID column since SQL doesn’t support “SELECT * EXCEPT [UserID]” or the like.

With this known (any style pointers are appreciated but not central to this problem), here’s the problem:

When I execute this stored procedure, I get an execution error:

Invalid column name 'userID'.

However, if I comment out my DROP COLUMN statement and retain the UserID, the stored procedure performs correctly.

What’s going on? It certainly looks like the statements are executing out of order and it’s dropping the column before I can use it to set the name strings!

[Edit 1]
I defined UserID previously (the whole stored procedure is about 200 lies of mostly irrelevant logic, so I’ll paste snippets:

    CREATE TABLE #result ([Name] NVARCHAR(256), [UserID] INT);

Case sensitivity isn’t the problem but did point me to the right line – there was one place in which I had userID instead of UserID. Now that I fixed the case, the error message complains about UserID.

My “broken” stored procedure also works properly in SQL Server 2008 – this is either a 2000 bug or I’m severely misunderstanding how SQL Server used to work.

Thanks everyone for chiming in!

For anyone searching this in the future, I’ve added an extremely crude workaround to be 2000-compatible until we update our production version:

DECLARE @workaroundTableName NVARCHAR(256), @workaroundQuery NVARCHAR(2000)
SET @workaroundQuery = 'SELECT [Name]';
DECLARE cur_workaround CURSOR FOR
SELECT COLUMN_NAME FROM [tempdb].INFORMATION_SCHEMA.Columns WHERE TABLE_NAME LIKE '#result%' AND COLUMN_NAME <> 'UserID'
OPEN cur_workaround;
FETCH NEXT FROM cur_workaround INTO @workaroundTableName
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @workaroundQuery = @workaroundQuery + ',[' + @workaroundTableName + ']'
    FETCH NEXT FROM cur_workaround INTO @workaroundTableName
END
CLOSE cur_workaround;
DEALLOCATE cur_workaround;
SET @workaroundQuery = @workaroundQuery + ' FROM #result ORDER BY Name ASC'
EXEC(@workaroundQuery);

Thanks everyone!

  • 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-13T11:07:31+00:00Added an answer on May 13, 2026 at 11:07 am

    This works for me:

    CREATE TABLE #temp_t
     (
      myInt int,
      myUser varchar(100)
     )
    
    INSERT INTO #temp_t(myInt, myUser) VALUES(1, 'Jon1')
    INSERT INTO #temp_t(myInt, myUser) VALUES(2, 'Jon2')
    INSERT INTO #temp_t(myInt, myUser) VALUES(3, 'Jon3')
    INSERT INTO #temp_t(myInt, myUser) VALUES(4, 'Jon4')
    
    ALTER TABLE #temp_t
    DROP Column myUser
    
    SELECT * FROM #temp_t
    
    DROP TABLE #temp_t
    

    It says invalid column for you. Did you check the spelling and ensure there even exists that column in your temp table.

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

Sidebar

Related Questions

brief history. I have a little reporting tool that I am creating that uses
In brief: Is it ever excusable to have assembly names and class names stored
From a brief look using Reflector, it looks like String.Substring() allocates memory for each
Very Brief Background: I am using Jquery Autocomplete to lookup the the value of
My Problem is in brief below: This is done using PHP, JS, Smarty and
Brief Summary: We are using Tridion 2009 SP1, however we never used .NET templating,
Very brief background: We are making use of CLR stored procedures to apply access
In brief i'm writing a plugin for a Rails app and I want to
- Bref history: - I was using MapServer, everything was working fine except that
Brief question What command can I use to make my DataSet refresh it's connection

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.