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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:11:59+00:00 2026-05-28T14:11:59+00:00

I have a table in SQL Server 2005 with a variable number of columns,

  • 0

I have a table in SQL Server 2005 with a variable number of columns, the first two being the only ones which are constant.
I’ve managed to get the number of columns in the table, so I would have what to count up to, but I don’t really know how to select a variable number of columns.

SELECT
DISTINCT
COUNT(*)
FROM
INFORMATION_SCHEMA.[COLUMNS]
WHERE
TABLE_NAME = 'SOME_TABLE'
AND
(COLUMN_NAME <> 'STATIC_COL1' AND COLUMN_NAME <> 'STATIC_COL2')

I’m guessing I need to use a while loop to get each column.
Any help would be appreciated.
Thanks.

Edit:
I ended up using @aF’s suggestion of dynamic SQL (see below)

DECLARE @CMD  AS VARCHAR(MAX)
SET @CMD = 'SELECT '
DECLARE @COL AS VARCHAR(MAX)

DECLARE COLUMN_CURSOR CURSOR FOR
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.[COLUMNS]
WHERE
TABLE_NAME = 'SOME_TABLE'

OPEN COLUMN_CURSOR
FETCH NEXT FROM COLUMN_CURSOR
INTO @COL
WHILE @@FETCH_STATUS = 0
BEGIN

IF @COL = 'STATIC_COL1'
BEGIN
SET @CMD = @CMD + @COL
END

FETCH NEXT FROM COLUMN_CURSOR
INTO @COL

SET @CMD = @CMD + ', CAST(' + @COL +' AS INT) AS ' + @COL

END
CLOSE COLUMN_CURSOR
DEALLOCATE COLUMN_CURSOR

SET @CMD = @CMD + ' FROM SOME_TABLE'

EXEC (@CMD)
  • 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-28T14:12:00+00:00Added an answer on May 28, 2026 at 2:12 pm

    One way to do it:

    The following select gives you the list of columns:

    SELECT
    column_name
    FROM
    INFORMATION_SCHEMA.[COLUMNS]
    WHERE
    TABLE_NAME = 'SOME_TABLE'
    AND
    (COLUMN_NAME <> 'STATIC_COL1' AND COLUMN_NAME <> 'STATIC_COL1')
    

    With that, you can construct the intended query and use dynamic SQL to execute it.

    Dynamic SQL is something like this:

    declare @cmd = 'select * from some_table'
    exec(@cmd)
    

    In you case, you need to construct the @cmd with the columns from the first select.

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

Sidebar

Related Questions

I have a table in SQL Server 2005 which has three columns: id (int),
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have a table in SQL Server 2005 which has a date time field.
I have a table in sql server 2005 which holds an ip range and
Using SQL Server 2005, I have a table where certain events are being logged,
Environment: SQL Server 2005/2008, pubs database I have inserted into a table variable a
I have a table in SQL Server 2005 SP4; in which one of the
I have a table in my sql server 2005 database which contains about 50
I have a table in SQL Server 2005 database that has following columns: Id,ProductName,Year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
I have a table on SQL Server 2005 that was about 4gb in size.

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.