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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:49:45+00:00 2026-06-10T20:49:45+00:00

is it possible to do dynamic declarations? I will explain: I have a table

  • 0

is it possible to do dynamic declarations?
I will explain: I have a table COLUMNAMES:

ID|Name
 1|Country
 2|City
 3|District
 4|Neighbourhood

For each record in that table I would like to do something like:

declare @i int = 1
declare @number int
set @number = (SELECT count(*) FROM COLUMNNAMES)

While @i <= @number
BEGIN
Execute ('Declare column' + @i +'varchar(25)')
Execute ('set column' + @i +' = (Select NAME from COLUMNAMES where id = ' + @i)
set @i = @i + 1
END

The idea is that I get a list of variables (strings) that I can use to create SELECT statements with dynamic table-aliases:

 Execute ('Select SOMECOLUMN as ' + @columname +  @i +', ANOTHERCOLUMN as ' + @columname +  @i +', ATHIRDCOLUMN as ' + @columname +  @i + ' FROM SOMETABLE')

Can this be done? If so, how?

  • 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-10T20:49:47+00:00Added an answer on June 10, 2026 at 8:49 pm

    This is not complete solution but a direction . you need to get the value ‘SomeColumn’ dynamically someway ,likely the way you are getting the aliases in the below solution.

    declare @i int = 1
    declare @number INT
    DECLARE @ColName VARCHAR(25)
    DECLARE @SQL VARCHAR(4000)=''
    DECLARE @ColumnsWithAlias VARCHAR(4000) = ''
    set @number = (SELECT count(*) FROM COLUMNNAMES)
    
       While @i <= @number
      BEGIN
    
    Select @ColName= NAME from COLUMNAMES where id =  @i)
    
    SET @ColumnsWithAlias =@ColumnsWithAlias + 'SomeColumn'+ ' AS '+ @ColName + ' , '
    
       set @i = @i + 1
       END
    
       SET @SQL= 'SELECT '@ColumnsWithAlias+' FROM  TableName'
    
      EXECUTE(@SQL) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: MySQL dynamic cross tab I have student_record table ------------------------------------------------------------------- student_id | semester
Possible Duplicate: Dynamic object property name I have an object like this var localAppConfig
Possible Duplicate: dynamic sql pivot in sql server I have a table called Col_values
Possible Duplicate: What’s with the love of dynamic Languages I have already read this
Is it possible or makes sense to have static dynamic variables on a class,
Possible Duplicate: Dynamic array keys I have an array $Values which is set up
Possible Duplicate: Dynamic object property name I want to dynamically generate access to an
Possible Duplicate: Dynamic object property name $.ajax({ url: ranktonumber.json, dataType: json, success: function (data)
Is it possible to add dynamic colors to a TextBlock ..i.e. have one character
I am new to jaspersoft reports .Is it possible to have dynamic tree view

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.