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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:21:53+00:00 2026-06-02T02:21:53+00:00

I need to get the column names of a table that’s located in another

  • 0

I need to get the column names of a table that’s located in another database. The following script works for the active database but I need to run it against another database in the same server instance:

SELECT @ColumnList = 
    CASE
        WHEN @ColumnList IS NULL THEN name
        WHEN @ColumnList IS NOT NULL THEN @ColumnList + ',' + name
    END
FROM sys.columns
WHERE object_id = Object_Id(@TableName);

Here’s the issue… the database isn’t known at compile time. Its passed into a stored procedure at runtime. So I see no alternative but to use dynamic sql. In the past I’ve tried using Use [DBName] in a dynamic sql script but always ran into problems until I realized I could do this:

SET @SQL = 'SELECT Foo FROM Bar'
SET @sp_executesql = quotename(@DatabaseName) + '..sp_executesql'
EXECUTE @sp_executesql @SQL

But I’m having difficulty figuring out how to do this with the script I mentioned above. My first attempt looked like:

-- @DatabaseName and @TableName are parameters of the 
-- stored procedure containing this script

DECLARE @ColumnList nvarchar(max),
        @SQL nvarchar(max),
        @sp_executesql nvarchar(max) = quotename(@DatabaseName) + '..sp_executesql';
SET @SQL = 
'SELECT @ColumnList = 
    CASE
        WHEN @ColumnList IS NULL THEN name
        WHEN @ColumnList IS NOT NULL THEN @ColumnList + '','' + name
    END
FROM sys.columns
WHERE object_id = Object_Id(@TableName);'

EXECUTE @sp_executesql @SQL,
        N'@ColumnList = nvarchar(max) OUT, @TableName = sysname',
        @ColumnList, @TableName

But when it runs it doesn’t interpret @ColumnList as a valid variable. What am I missing?

  • 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-02T02:21:55+00:00Added an answer on June 2, 2026 at 2:21 am

    I’m not sure where you picked up that syntax, but here is how I would do it:

    -- I assume these are parameters, so declaring them separately:
    DECLARE 
        @DatabaseName SYSNAME = N'db_name',
        @TableName SYSNAME = N'table_name';
    
    DECLARE 
        @sql NVARCHAR(MAX), 
        @columnList NVARCHAR(MAX);
    
    SELECT @sql = N'SELECT @ColumnList = COALESCE(@ColumnList + '','', '''') 
        + c.name
        FROM [$db$].sys.columns AS c
        INNER JOIN [$db$].sys.tables AS o
        ON c.[object_id] = o.[object_id]
        WHERE o.name = @TableName;';
    
    SET @sql = REPLACE(@sql, '$db$', @DatabaseName);
    
    EXEC sp_executesql @sql, 
        N'@ColumnList NVARCHAR(MAX) OUTPUT, @TableName SYSNAME',
        @ColumnList OUTPUT, @TableName;
    
    SELECT @ColumnList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to query the database to get the column names , not to
I need to get column names and their tables in a SQLite database. What
I have a table with 11 columns, but I need to get only 2
I have a table that has column names similar to ADDRESS, OLD_ADDRESS, CITY, OLD_CITY,
I need to get the data type of a column using the column name.
I need to get the complete content of a column of two tables in
I need to get some data from a column. For example I have data
Just need get some vals located in application.ini(main ini) in the Controller plugin I
Is there is a way MySQL to get the column names given the value
I have a legacy database with a pretty evil design that I need to

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.