Select Column as Column names of values in another table in SQL SERVER
I need help on a getting my column names as values in another table in SQL 2008.
For Example
Table A
Type Col1 Col2 Col3 Col4 Col5
101 1 2 3 2 5
102 4 2 3 2 0
103 2 1 0 0 5
103 7 2 0 0 5
105 8 3 0 0 0
Table B
ColID ColName
Col1 Math
Col2 English
Col3 French
Col4 Fine Arts
Col5 Biology
So basically the Table B holds the column name to be used when selecting from Table A
Such that, if i want to do a sample code like
Select Col1,Col2,Col3 from TableA
It should look like
Select Col1 as Math, Col2 as English, Col3 as French from Table A........
Did all i could but could not get any code giving me what i want....
Need help.
Note*
Reason i designed the table like this is because the column names can change very frequently by user and instead of having to alter code everytime, this should work no matter the changes made to column names when i export to excel.
sql-server tsql sql-server-2008
add a comment |
I need help on a getting my column names as values in another table in SQL 2008.
For Example
Table A
Type Col1 Col2 Col3 Col4 Col5
101 1 2 3 2 5
102 4 2 3 2 0
103 2 1 0 0 5
103 7 2 0 0 5
105 8 3 0 0 0
Table B
ColID ColName
Col1 Math
Col2 English
Col3 French
Col4 Fine Arts
Col5 Biology
So basically the Table B holds the column name to be used when selecting from Table A
Such that, if i want to do a sample code like
Select Col1,Col2,Col3 from TableA
It should look like
Select Col1 as Math, Col2 as English, Col3 as French from Table A........
Did all i could but could not get any code giving me what i want....
Need help.
Note*
Reason i designed the table like this is because the column names can change very frequently by user and instead of having to alter code everytime, this should work no matter the changes made to column names when i export to excel.
sql-server tsql sql-server-2008
You will have to use dynamic here.
– Sean Lange
Nov 15 '18 at 17:06
Or if possible redesign this so the Col values are down (rows) not across (columns)
– Alex K.
Nov 15 '18 at 17:07
add a comment |
I need help on a getting my column names as values in another table in SQL 2008.
For Example
Table A
Type Col1 Col2 Col3 Col4 Col5
101 1 2 3 2 5
102 4 2 3 2 0
103 2 1 0 0 5
103 7 2 0 0 5
105 8 3 0 0 0
Table B
ColID ColName
Col1 Math
Col2 English
Col3 French
Col4 Fine Arts
Col5 Biology
So basically the Table B holds the column name to be used when selecting from Table A
Such that, if i want to do a sample code like
Select Col1,Col2,Col3 from TableA
It should look like
Select Col1 as Math, Col2 as English, Col3 as French from Table A........
Did all i could but could not get any code giving me what i want....
Need help.
Note*
Reason i designed the table like this is because the column names can change very frequently by user and instead of having to alter code everytime, this should work no matter the changes made to column names when i export to excel.
sql-server tsql sql-server-2008
I need help on a getting my column names as values in another table in SQL 2008.
For Example
Table A
Type Col1 Col2 Col3 Col4 Col5
101 1 2 3 2 5
102 4 2 3 2 0
103 2 1 0 0 5
103 7 2 0 0 5
105 8 3 0 0 0
Table B
ColID ColName
Col1 Math
Col2 English
Col3 French
Col4 Fine Arts
Col5 Biology
So basically the Table B holds the column name to be used when selecting from Table A
Such that, if i want to do a sample code like
Select Col1,Col2,Col3 from TableA
It should look like
Select Col1 as Math, Col2 as English, Col3 as French from Table A........
Did all i could but could not get any code giving me what i want....
Need help.
Note*
Reason i designed the table like this is because the column names can change very frequently by user and instead of having to alter code everytime, this should work no matter the changes made to column names when i export to excel.
sql-server tsql sql-server-2008
sql-server tsql sql-server-2008
edited Nov 15 '18 at 17:13
Tobyy Damian
asked Nov 15 '18 at 16:55
Tobyy DamianTobyy Damian
64
64
You will have to use dynamic here.
– Sean Lange
Nov 15 '18 at 17:06
Or if possible redesign this so the Col values are down (rows) not across (columns)
– Alex K.
Nov 15 '18 at 17:07
add a comment |
You will have to use dynamic here.
– Sean Lange
Nov 15 '18 at 17:06
Or if possible redesign this so the Col values are down (rows) not across (columns)
– Alex K.
Nov 15 '18 at 17:07
You will have to use dynamic here.
– Sean Lange
Nov 15 '18 at 17:06
You will have to use dynamic here.
– Sean Lange
Nov 15 '18 at 17:06
Or if possible redesign this so the Col values are down (rows) not across (columns)
– Alex K.
Nov 15 '18 at 17:07
Or if possible redesign this so the Col values are down (rows) not across (columns)
– Alex K.
Nov 15 '18 at 17:07
add a comment |
2 Answers
2
active
oldest
votes
To get this done with your current design you will have to use dynamic SQL. I wont go into the pros and cons of dynamic SQL here. For more info you can go here. I highly suggest reading up on it if you are not familiar with dynamic SQL.
CREATE TABLE #TableA (Type int, Col1 int, Col2 int, Col3 int, Col4 int, Col5 int);
CREATE TABLE #TableB (ColId varchar(50), ColName varchar(50));
INSERT INTO #TableA VALUES
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0);
INSERT INTO #TableB VALUES
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology');
DECLARE @Sql nvarchar(MAX);
/* Build SELECT */
SET @Sql = 'SELECT ';
SELECT @Sql = @Sql + ColId + ' AS ''' + ColName + ''', ' FROM #TableB;
/* Remove trailing comma */
SET @Sql = (SELECT LEFT(@Sql, LEN(@Sql)-1));
/* Add FROM */
SET @Sql = @Sql + ' FROM #TableA';
/* Output query */
SELECT @Sql;
/* Execute query */
EXEC sp_executesql @Sql;
DROP TABLE #TableA;
DROP TABLE #TableB;
thanks so much. Using that code within SQL seemed to work, but i could only verify if it would work for me from VB6. How do i make this code work within VB6 ?
– Tobyy Damian
Nov 16 '18 at 8:15
???? I am still stuck in trying this code within VB6, Pls help
– Tobyy Damian
Nov 16 '18 at 11:52
@TobyyDamian This is SQL specific question which has been anwered. You may want to now post a new question with your issues within VB6. I cannot help you with VB6. My only other suggestion would be to turn this into a stored procedure that you can call. That would make implementation into your app easier most likely.
– Chris Albert
Nov 16 '18 at 14:25
add a comment |
You can execute dynamic query, in which you are replacing the aliases with the configured column names. You can do it with code like this:
use [tempdb]
go
drop table if exists TableA
drop table if exists TableB
create table TableA([Type] int, [Col1] int, [Col2] int, [Col3] int, [Col4] int, [Col5] int)
insert into TableA values
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0)
create table TableB(ColID sysname, ColName sysname)
insert into TableB values
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology')
declare @sql nvarchar(max) = N'select [Type], [Col1] as [{Col1}], [Col2] as [{Col2}], [Col3] as [{Col3}], [Col4] as [{Col4}], [Col5] as [{Col5}] from TableA'
-- Execute unmodified query
exec sp_executesql @sql
-- Replace aliases with column names
select @sql = REPLACE(@sql, CONCAT(N'{', ColID, N'}'), ColName)
from TableB
-- Execute query with replaced aliases
exec sp_executesql @sql
-- Cleanup
drop table if exists TableA
drop table if exists TableB
OP is using SQL Server 2008 soDROP TABLE IF EXISTS
will not work.
– Chris Albert
Nov 15 '18 at 20:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324385%2fselect-column-as-column-names-of-values-in-another-table-in-sql-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To get this done with your current design you will have to use dynamic SQL. I wont go into the pros and cons of dynamic SQL here. For more info you can go here. I highly suggest reading up on it if you are not familiar with dynamic SQL.
CREATE TABLE #TableA (Type int, Col1 int, Col2 int, Col3 int, Col4 int, Col5 int);
CREATE TABLE #TableB (ColId varchar(50), ColName varchar(50));
INSERT INTO #TableA VALUES
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0);
INSERT INTO #TableB VALUES
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology');
DECLARE @Sql nvarchar(MAX);
/* Build SELECT */
SET @Sql = 'SELECT ';
SELECT @Sql = @Sql + ColId + ' AS ''' + ColName + ''', ' FROM #TableB;
/* Remove trailing comma */
SET @Sql = (SELECT LEFT(@Sql, LEN(@Sql)-1));
/* Add FROM */
SET @Sql = @Sql + ' FROM #TableA';
/* Output query */
SELECT @Sql;
/* Execute query */
EXEC sp_executesql @Sql;
DROP TABLE #TableA;
DROP TABLE #TableB;
thanks so much. Using that code within SQL seemed to work, but i could only verify if it would work for me from VB6. How do i make this code work within VB6 ?
– Tobyy Damian
Nov 16 '18 at 8:15
???? I am still stuck in trying this code within VB6, Pls help
– Tobyy Damian
Nov 16 '18 at 11:52
@TobyyDamian This is SQL specific question which has been anwered. You may want to now post a new question with your issues within VB6. I cannot help you with VB6. My only other suggestion would be to turn this into a stored procedure that you can call. That would make implementation into your app easier most likely.
– Chris Albert
Nov 16 '18 at 14:25
add a comment |
To get this done with your current design you will have to use dynamic SQL. I wont go into the pros and cons of dynamic SQL here. For more info you can go here. I highly suggest reading up on it if you are not familiar with dynamic SQL.
CREATE TABLE #TableA (Type int, Col1 int, Col2 int, Col3 int, Col4 int, Col5 int);
CREATE TABLE #TableB (ColId varchar(50), ColName varchar(50));
INSERT INTO #TableA VALUES
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0);
INSERT INTO #TableB VALUES
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology');
DECLARE @Sql nvarchar(MAX);
/* Build SELECT */
SET @Sql = 'SELECT ';
SELECT @Sql = @Sql + ColId + ' AS ''' + ColName + ''', ' FROM #TableB;
/* Remove trailing comma */
SET @Sql = (SELECT LEFT(@Sql, LEN(@Sql)-1));
/* Add FROM */
SET @Sql = @Sql + ' FROM #TableA';
/* Output query */
SELECT @Sql;
/* Execute query */
EXEC sp_executesql @Sql;
DROP TABLE #TableA;
DROP TABLE #TableB;
thanks so much. Using that code within SQL seemed to work, but i could only verify if it would work for me from VB6. How do i make this code work within VB6 ?
– Tobyy Damian
Nov 16 '18 at 8:15
???? I am still stuck in trying this code within VB6, Pls help
– Tobyy Damian
Nov 16 '18 at 11:52
@TobyyDamian This is SQL specific question which has been anwered. You may want to now post a new question with your issues within VB6. I cannot help you with VB6. My only other suggestion would be to turn this into a stored procedure that you can call. That would make implementation into your app easier most likely.
– Chris Albert
Nov 16 '18 at 14:25
add a comment |
To get this done with your current design you will have to use dynamic SQL. I wont go into the pros and cons of dynamic SQL here. For more info you can go here. I highly suggest reading up on it if you are not familiar with dynamic SQL.
CREATE TABLE #TableA (Type int, Col1 int, Col2 int, Col3 int, Col4 int, Col5 int);
CREATE TABLE #TableB (ColId varchar(50), ColName varchar(50));
INSERT INTO #TableA VALUES
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0);
INSERT INTO #TableB VALUES
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology');
DECLARE @Sql nvarchar(MAX);
/* Build SELECT */
SET @Sql = 'SELECT ';
SELECT @Sql = @Sql + ColId + ' AS ''' + ColName + ''', ' FROM #TableB;
/* Remove trailing comma */
SET @Sql = (SELECT LEFT(@Sql, LEN(@Sql)-1));
/* Add FROM */
SET @Sql = @Sql + ' FROM #TableA';
/* Output query */
SELECT @Sql;
/* Execute query */
EXEC sp_executesql @Sql;
DROP TABLE #TableA;
DROP TABLE #TableB;
To get this done with your current design you will have to use dynamic SQL. I wont go into the pros and cons of dynamic SQL here. For more info you can go here. I highly suggest reading up on it if you are not familiar with dynamic SQL.
CREATE TABLE #TableA (Type int, Col1 int, Col2 int, Col3 int, Col4 int, Col5 int);
CREATE TABLE #TableB (ColId varchar(50), ColName varchar(50));
INSERT INTO #TableA VALUES
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0);
INSERT INTO #TableB VALUES
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology');
DECLARE @Sql nvarchar(MAX);
/* Build SELECT */
SET @Sql = 'SELECT ';
SELECT @Sql = @Sql + ColId + ' AS ''' + ColName + ''', ' FROM #TableB;
/* Remove trailing comma */
SET @Sql = (SELECT LEFT(@Sql, LEN(@Sql)-1));
/* Add FROM */
SET @Sql = @Sql + ' FROM #TableA';
/* Output query */
SELECT @Sql;
/* Execute query */
EXEC sp_executesql @Sql;
DROP TABLE #TableA;
DROP TABLE #TableB;
answered Nov 15 '18 at 19:56
Chris AlbertChris Albert
1,50221622
1,50221622
thanks so much. Using that code within SQL seemed to work, but i could only verify if it would work for me from VB6. How do i make this code work within VB6 ?
– Tobyy Damian
Nov 16 '18 at 8:15
???? I am still stuck in trying this code within VB6, Pls help
– Tobyy Damian
Nov 16 '18 at 11:52
@TobyyDamian This is SQL specific question which has been anwered. You may want to now post a new question with your issues within VB6. I cannot help you with VB6. My only other suggestion would be to turn this into a stored procedure that you can call. That would make implementation into your app easier most likely.
– Chris Albert
Nov 16 '18 at 14:25
add a comment |
thanks so much. Using that code within SQL seemed to work, but i could only verify if it would work for me from VB6. How do i make this code work within VB6 ?
– Tobyy Damian
Nov 16 '18 at 8:15
???? I am still stuck in trying this code within VB6, Pls help
– Tobyy Damian
Nov 16 '18 at 11:52
@TobyyDamian This is SQL specific question which has been anwered. You may want to now post a new question with your issues within VB6. I cannot help you with VB6. My only other suggestion would be to turn this into a stored procedure that you can call. That would make implementation into your app easier most likely.
– Chris Albert
Nov 16 '18 at 14:25
thanks so much. Using that code within SQL seemed to work, but i could only verify if it would work for me from VB6. How do i make this code work within VB6 ?
– Tobyy Damian
Nov 16 '18 at 8:15
thanks so much. Using that code within SQL seemed to work, but i could only verify if it would work for me from VB6. How do i make this code work within VB6 ?
– Tobyy Damian
Nov 16 '18 at 8:15
???? I am still stuck in trying this code within VB6, Pls help
– Tobyy Damian
Nov 16 '18 at 11:52
???? I am still stuck in trying this code within VB6, Pls help
– Tobyy Damian
Nov 16 '18 at 11:52
@TobyyDamian This is SQL specific question which has been anwered. You may want to now post a new question with your issues within VB6. I cannot help you with VB6. My only other suggestion would be to turn this into a stored procedure that you can call. That would make implementation into your app easier most likely.
– Chris Albert
Nov 16 '18 at 14:25
@TobyyDamian This is SQL specific question which has been anwered. You may want to now post a new question with your issues within VB6. I cannot help you with VB6. My only other suggestion would be to turn this into a stored procedure that you can call. That would make implementation into your app easier most likely.
– Chris Albert
Nov 16 '18 at 14:25
add a comment |
You can execute dynamic query, in which you are replacing the aliases with the configured column names. You can do it with code like this:
use [tempdb]
go
drop table if exists TableA
drop table if exists TableB
create table TableA([Type] int, [Col1] int, [Col2] int, [Col3] int, [Col4] int, [Col5] int)
insert into TableA values
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0)
create table TableB(ColID sysname, ColName sysname)
insert into TableB values
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology')
declare @sql nvarchar(max) = N'select [Type], [Col1] as [{Col1}], [Col2] as [{Col2}], [Col3] as [{Col3}], [Col4] as [{Col4}], [Col5] as [{Col5}] from TableA'
-- Execute unmodified query
exec sp_executesql @sql
-- Replace aliases with column names
select @sql = REPLACE(@sql, CONCAT(N'{', ColID, N'}'), ColName)
from TableB
-- Execute query with replaced aliases
exec sp_executesql @sql
-- Cleanup
drop table if exists TableA
drop table if exists TableB
OP is using SQL Server 2008 soDROP TABLE IF EXISTS
will not work.
– Chris Albert
Nov 15 '18 at 20:08
add a comment |
You can execute dynamic query, in which you are replacing the aliases with the configured column names. You can do it with code like this:
use [tempdb]
go
drop table if exists TableA
drop table if exists TableB
create table TableA([Type] int, [Col1] int, [Col2] int, [Col3] int, [Col4] int, [Col5] int)
insert into TableA values
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0)
create table TableB(ColID sysname, ColName sysname)
insert into TableB values
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology')
declare @sql nvarchar(max) = N'select [Type], [Col1] as [{Col1}], [Col2] as [{Col2}], [Col3] as [{Col3}], [Col4] as [{Col4}], [Col5] as [{Col5}] from TableA'
-- Execute unmodified query
exec sp_executesql @sql
-- Replace aliases with column names
select @sql = REPLACE(@sql, CONCAT(N'{', ColID, N'}'), ColName)
from TableB
-- Execute query with replaced aliases
exec sp_executesql @sql
-- Cleanup
drop table if exists TableA
drop table if exists TableB
OP is using SQL Server 2008 soDROP TABLE IF EXISTS
will not work.
– Chris Albert
Nov 15 '18 at 20:08
add a comment |
You can execute dynamic query, in which you are replacing the aliases with the configured column names. You can do it with code like this:
use [tempdb]
go
drop table if exists TableA
drop table if exists TableB
create table TableA([Type] int, [Col1] int, [Col2] int, [Col3] int, [Col4] int, [Col5] int)
insert into TableA values
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0)
create table TableB(ColID sysname, ColName sysname)
insert into TableB values
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology')
declare @sql nvarchar(max) = N'select [Type], [Col1] as [{Col1}], [Col2] as [{Col2}], [Col3] as [{Col3}], [Col4] as [{Col4}], [Col5] as [{Col5}] from TableA'
-- Execute unmodified query
exec sp_executesql @sql
-- Replace aliases with column names
select @sql = REPLACE(@sql, CONCAT(N'{', ColID, N'}'), ColName)
from TableB
-- Execute query with replaced aliases
exec sp_executesql @sql
-- Cleanup
drop table if exists TableA
drop table if exists TableB
You can execute dynamic query, in which you are replacing the aliases with the configured column names. You can do it with code like this:
use [tempdb]
go
drop table if exists TableA
drop table if exists TableB
create table TableA([Type] int, [Col1] int, [Col2] int, [Col3] int, [Col4] int, [Col5] int)
insert into TableA values
(101, 1, 2, 3, 2, 5),
(102, 4, 2, 3, 2, 0),
(103, 2, 1, 0, 0, 5),
(103, 7, 2, 0, 0, 5),
(105, 8, 3, 0, 0, 0)
create table TableB(ColID sysname, ColName sysname)
insert into TableB values
('Col1', 'Math'),
('Col2', 'English'),
('Col3', 'French'),
('Col4', 'Fine Arts'),
('Col5', 'Biology')
declare @sql nvarchar(max) = N'select [Type], [Col1] as [{Col1}], [Col2] as [{Col2}], [Col3] as [{Col3}], [Col4] as [{Col4}], [Col5] as [{Col5}] from TableA'
-- Execute unmodified query
exec sp_executesql @sql
-- Replace aliases with column names
select @sql = REPLACE(@sql, CONCAT(N'{', ColID, N'}'), ColName)
from TableB
-- Execute query with replaced aliases
exec sp_executesql @sql
-- Cleanup
drop table if exists TableA
drop table if exists TableB
answered Nov 15 '18 at 20:02
Andrey NikolovAndrey Nikolov
4,2883922
4,2883922
OP is using SQL Server 2008 soDROP TABLE IF EXISTS
will not work.
– Chris Albert
Nov 15 '18 at 20:08
add a comment |
OP is using SQL Server 2008 soDROP TABLE IF EXISTS
will not work.
– Chris Albert
Nov 15 '18 at 20:08
OP is using SQL Server 2008 so
DROP TABLE IF EXISTS
will not work.– Chris Albert
Nov 15 '18 at 20:08
OP is using SQL Server 2008 so
DROP TABLE IF EXISTS
will not work.– Chris Albert
Nov 15 '18 at 20:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324385%2fselect-column-as-column-names-of-values-in-another-table-in-sql-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You will have to use dynamic here.
– Sean Lange
Nov 15 '18 at 17:06
Or if possible redesign this so the Col values are down (rows) not across (columns)
– Alex K.
Nov 15 '18 at 17:07