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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:07:20+00:00 2026-05-25T12:07:20+00:00

Please guide me how to create table variables dynamically. I know it can be

  • 0

Please guide me how to create table variables dynamically. I know it can be like this:

DECLARE @people TABLE 
( 
    id INT, 
    name VARCHAR(32) 
);

How I will create if dont know columns and data types. Scenario is I have to create table variable as per a physical tables and selct data into them, use them in SP and then return data in table variables to C# program.

For example I have a table Employees. I want create a table variable with same structure as Employyes have. But mentioning columns take a lot time (as Employees have about 100 columns)

Please advice.

  • 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-25T12:07:21+00:00Added an answer on May 25, 2026 at 12:07 pm

    So here is how you can build the DECLARE TABLE statement dynamically:

    DECLARE @sql NVARCHAR(MAX);
    SET @sql = N'';
    
    
    SELECT @sql = @sql + ',
    ' + c.name + ' ' + t.name
     + CASE
      WHEN t.name LIKE '%char' OR t.name LIKE '%binary' THEN
       '(' + CASE WHEN c.max_length = -1 THEN 'MAX' ELSE
       CONVERT(VARCHAR(4), c.max_length/CASE WHEN t.name LIKE 'n%'
       THEN 2 ELSE 1 END) END + ')'
      WHEN t.name IN ('float') THEN
          '(' + CONVERT(VARCHAR(4), c.precision) + ')'
      WHEN t.name IN ('decimal', 'numeric') THEN
       '(' + CONVERT(VARCHAR(4), c.precision) + ','
       + CONVERT(VARCHAR(4), c.scale) + ')'
      ELSE ''
     END
     FROM sys.columns AS c
     INNER JOIN sys.types AS t
     ON c.system_type_id = t.system_type_id
     AND c.user_type_id = t.user_type_id
     WHERE c.[object_id] = OBJECT_ID('dbo.Employees')
     ORDER BY c.column_id;
    
    SET @sql = 'DECLARE @people TABLE (' + STUFF(@sql, 1, 1, '') + '
    );';
    
    SELECT @sql;
    

    But now what? You can’t insert into it from outside the scope of dynamic SQL:

    EXEC sp_executesql @sql;
    INSERT @people(id, name) SELECT 1,'foo';
    

    Yields this error:

    Msg 1087, Level 15, State 2, ...
    Must declare the table variable "@people".
    

    Once again, a scoping issue – @people only exists in the dynamic SQL and ceases to exist as soon as it’s finished. So while you could proceed and append to the @sql variable:

    SET @sql = @sql + 'INSERT @people ...; SELECT id, name FROM @people;';
    

    …this will get out of hand very quickly. And I still have no idea how your C# code can be aware of all the columns and data types involved, but it’s too hard to write the SQL to do so… you know you can drag the columns node from Object Explorer onto a query window and it will produce a nice comma-separated list of column names for you, right?

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

Sidebar

Related Questions

Can you please guide me what is session and session variables? I don't need
Can some one please guide me how to create an Oscillation Animation on an
Can some one please guide me to understand which jar file i need to
Please guide me if I'm on right track. I'm trying to create database schema
Please help me correct this. I used template table with inline event handler. e.g.
Please guide what is meaning by Web is stateless in simple words ? Kindly
Please recommend some websites with a quick guide. MSDN is good but it's hard
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
CREATE TABLE titlexplan( id INTEGER NOT NULL, title_id INTEGER NOT NULL , plan_id INTEGER
I am trying to create a UIWebView in UITableView footer. The table is grouped.

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.