Create table using data rows from select as columns on itSplit Column with delimiter into multiple columnsInsert into … values ( SELECT … FROM … )Add a column with a default value to an existing table in SQL ServerHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?SQL exclude a column using SELECT * [except columnA] FROM tableA?How can I get column names from a table in SQL Server?How do I UPDATE from a SELECT in SQL Server?Select first row in each GROUP BY group?Find all tables containing column with specified name - MS SQL ServerSQL select only rows with max value on a column
soda water first stored in refrigerator and then outside
How predictable is $RANDOM really?
How did the IEC decide to create kibibytes?
Is it acceptable that I plot a time-series figure with years increasing from right to left?
Why is Canon's 17cm focus distance macro, while 16cm focus distance is not macro?
Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?
Passwordless authentication - how invalidate login code
Is it bad to suddenly introduce another element to your fantasy world a good ways into the story?
How serious is plagiarism in a master’s thesis?
PhD: When to quit and move on?
Why do Martians have to wear space helmets?
In the Seventh Seal why does Death let the chess game happen?
Machine Learning Golf: Multiplication
What is the highest level of accuracy in motion control a Victorian society could achieve?
Why does mean tend be more stable in different samples than median?
Why was no first prize awarded at a competition?
How to say "just a precision" properly in English?
How can I use my cell phone's light as a reading light?
Is this car delivery via Ebay Motors on Craigslist a scam?
Taking advantage when HR forgets to communicate the rules
How important is it for multiple POVs to run chronologically?
My professor has told me he will be the corresponding author. Will it hurt my future career?
Changing text size or line height of QGIS attribute table?
Park the computer
Create table using data rows from select as columns on it
Split Column with delimiter into multiple columnsInsert into … values ( SELECT … FROM … )Add a column with a default value to an existing table in SQL ServerHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?SQL exclude a column using SELECT * [except columnA] FROM tableA?How can I get column names from a table in SQL Server?How do I UPDATE from a SELECT in SQL Server?Select first row in each GROUP BY group?Find all tables containing column with specified name - MS SQL ServerSQL select only rows with max value on a column
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I need to create a table using the rows of another table as the column names. The reason is that my database is not a relational one, so, I have in each case a table with the data, and another with the corresponding metadata.
Example:
Table 1:
Person
ID | Info
===================================================
1 | <John Smith><1st Avenue><Miami,Florida><33101>
2 | <Mary Walton><83th Street><New York, NY><1001>
Table 2:
Person_Desc
Field | Info
===================================================
ID | Sequential identifier
Name | Persons full name
Address | Physical location detail
City | City
ZIP_C | Postal office code
I would like to create a stored procedure that receives those two table names as parameter and the creates a third table, like this (bear with me and pseudocode please):
CREATE STORED PROCEDURE sp_relationalTable
@dataTable nvarchar(50),
@metadataTable nvarchar(50) ,
@TmpTable nvarchar(50)
AS
SELECT * FROM @metadataTable
CREATE TABLE @TmpData
( @metadataTable_Field1 nvarchar(100),
,@metadataTable_Field2 nvarchar(100),
,@metadataTable_Field3 nvarchar(100)....
)
END
Thats the first part. Then I would run a SELECT statement against Table1: Person, breaking the data by a known delimiter, and INSERT all data into the newly created table.
INSERT INTO @TmpData (SELECT * FROM @dataTable)
Ideally, it could be run all into one SP as I said at the beginning, so, when you would run such SP it would be like:
EXEC sp_relationalTable Person, Person_Desc, RPerson
And I would end up with:
Table 3:
RPerson
ID | Name | Address | City | ZIP_C |
============================================================
1 | John Smith | 1st Avenue | Miami,Florida |33101 |
2 | Mary Walton | 83th Street | New York, NY |1001 |
sql
add a comment |
I need to create a table using the rows of another table as the column names. The reason is that my database is not a relational one, so, I have in each case a table with the data, and another with the corresponding metadata.
Example:
Table 1:
Person
ID | Info
===================================================
1 | <John Smith><1st Avenue><Miami,Florida><33101>
2 | <Mary Walton><83th Street><New York, NY><1001>
Table 2:
Person_Desc
Field | Info
===================================================
ID | Sequential identifier
Name | Persons full name
Address | Physical location detail
City | City
ZIP_C | Postal office code
I would like to create a stored procedure that receives those two table names as parameter and the creates a third table, like this (bear with me and pseudocode please):
CREATE STORED PROCEDURE sp_relationalTable
@dataTable nvarchar(50),
@metadataTable nvarchar(50) ,
@TmpTable nvarchar(50)
AS
SELECT * FROM @metadataTable
CREATE TABLE @TmpData
( @metadataTable_Field1 nvarchar(100),
,@metadataTable_Field2 nvarchar(100),
,@metadataTable_Field3 nvarchar(100)....
)
END
Thats the first part. Then I would run a SELECT statement against Table1: Person, breaking the data by a known delimiter, and INSERT all data into the newly created table.
INSERT INTO @TmpData (SELECT * FROM @dataTable)
Ideally, it could be run all into one SP as I said at the beginning, so, when you would run such SP it would be like:
EXEC sp_relationalTable Person, Person_Desc, RPerson
And I would end up with:
Table 3:
RPerson
ID | Name | Address | City | ZIP_C |
============================================================
1 | John Smith | 1st Avenue | Miami,Florida |33101 |
2 | Mary Walton | 83th Street | New York, NY |1001 |
sql
What does "my database is not a relational one" mean, if youre using SQLServer?
– Caius Jard
Mar 25 at 20:35
Greetings. Beside the fact that my RDBMS is MSSQL, as you can see in the table examples given, my data does not follow a relational model, as in, there are no foreign keys, and the data is only stored in 2 columns, using another table just to hold my metadata.
– Me_
Mar 25 at 20:39
What version of SQLS?
– Caius Jard
Mar 26 at 5:18
Hi in your table2 you doesn’t have fkey to link thoses with table1 ? Other you have to take look at pivot, dynamic sql and exec
– pascal sanchez
Mar 26 at 6:27
I hesitate to call it a duplicate, because you're really asking two questions, but here's the answer to one of them: stackoverflow.com/questions/46217397/… This isn't a PIVOT by the way, so don't bother researching that, but you DO need to useDynamic SQLto get the column names of your results from the second table.
– Tab Alleman
Mar 26 at 14:16
add a comment |
I need to create a table using the rows of another table as the column names. The reason is that my database is not a relational one, so, I have in each case a table with the data, and another with the corresponding metadata.
Example:
Table 1:
Person
ID | Info
===================================================
1 | <John Smith><1st Avenue><Miami,Florida><33101>
2 | <Mary Walton><83th Street><New York, NY><1001>
Table 2:
Person_Desc
Field | Info
===================================================
ID | Sequential identifier
Name | Persons full name
Address | Physical location detail
City | City
ZIP_C | Postal office code
I would like to create a stored procedure that receives those two table names as parameter and the creates a third table, like this (bear with me and pseudocode please):
CREATE STORED PROCEDURE sp_relationalTable
@dataTable nvarchar(50),
@metadataTable nvarchar(50) ,
@TmpTable nvarchar(50)
AS
SELECT * FROM @metadataTable
CREATE TABLE @TmpData
( @metadataTable_Field1 nvarchar(100),
,@metadataTable_Field2 nvarchar(100),
,@metadataTable_Field3 nvarchar(100)....
)
END
Thats the first part. Then I would run a SELECT statement against Table1: Person, breaking the data by a known delimiter, and INSERT all data into the newly created table.
INSERT INTO @TmpData (SELECT * FROM @dataTable)
Ideally, it could be run all into one SP as I said at the beginning, so, when you would run such SP it would be like:
EXEC sp_relationalTable Person, Person_Desc, RPerson
And I would end up with:
Table 3:
RPerson
ID | Name | Address | City | ZIP_C |
============================================================
1 | John Smith | 1st Avenue | Miami,Florida |33101 |
2 | Mary Walton | 83th Street | New York, NY |1001 |
sql
I need to create a table using the rows of another table as the column names. The reason is that my database is not a relational one, so, I have in each case a table with the data, and another with the corresponding metadata.
Example:
Table 1:
Person
ID | Info
===================================================
1 | <John Smith><1st Avenue><Miami,Florida><33101>
2 | <Mary Walton><83th Street><New York, NY><1001>
Table 2:
Person_Desc
Field | Info
===================================================
ID | Sequential identifier
Name | Persons full name
Address | Physical location detail
City | City
ZIP_C | Postal office code
I would like to create a stored procedure that receives those two table names as parameter and the creates a third table, like this (bear with me and pseudocode please):
CREATE STORED PROCEDURE sp_relationalTable
@dataTable nvarchar(50),
@metadataTable nvarchar(50) ,
@TmpTable nvarchar(50)
AS
SELECT * FROM @metadataTable
CREATE TABLE @TmpData
( @metadataTable_Field1 nvarchar(100),
,@metadataTable_Field2 nvarchar(100),
,@metadataTable_Field3 nvarchar(100)....
)
END
Thats the first part. Then I would run a SELECT statement against Table1: Person, breaking the data by a known delimiter, and INSERT all data into the newly created table.
INSERT INTO @TmpData (SELECT * FROM @dataTable)
Ideally, it could be run all into one SP as I said at the beginning, so, when you would run such SP it would be like:
EXEC sp_relationalTable Person, Person_Desc, RPerson
And I would end up with:
Table 3:
RPerson
ID | Name | Address | City | ZIP_C |
============================================================
1 | John Smith | 1st Avenue | Miami,Florida |33101 |
2 | Mary Walton | 83th Street | New York, NY |1001 |
sql
sql
edited Mar 26 at 14:13
Tab Alleman
28.2k6 gold badges25 silver badges45 bronze badges
28.2k6 gold badges25 silver badges45 bronze badges
asked Mar 25 at 20:26
Me_Me_
263 bronze badges
263 bronze badges
What does "my database is not a relational one" mean, if youre using SQLServer?
– Caius Jard
Mar 25 at 20:35
Greetings. Beside the fact that my RDBMS is MSSQL, as you can see in the table examples given, my data does not follow a relational model, as in, there are no foreign keys, and the data is only stored in 2 columns, using another table just to hold my metadata.
– Me_
Mar 25 at 20:39
What version of SQLS?
– Caius Jard
Mar 26 at 5:18
Hi in your table2 you doesn’t have fkey to link thoses with table1 ? Other you have to take look at pivot, dynamic sql and exec
– pascal sanchez
Mar 26 at 6:27
I hesitate to call it a duplicate, because you're really asking two questions, but here's the answer to one of them: stackoverflow.com/questions/46217397/… This isn't a PIVOT by the way, so don't bother researching that, but you DO need to useDynamic SQLto get the column names of your results from the second table.
– Tab Alleman
Mar 26 at 14:16
add a comment |
What does "my database is not a relational one" mean, if youre using SQLServer?
– Caius Jard
Mar 25 at 20:35
Greetings. Beside the fact that my RDBMS is MSSQL, as you can see in the table examples given, my data does not follow a relational model, as in, there are no foreign keys, and the data is only stored in 2 columns, using another table just to hold my metadata.
– Me_
Mar 25 at 20:39
What version of SQLS?
– Caius Jard
Mar 26 at 5:18
Hi in your table2 you doesn’t have fkey to link thoses with table1 ? Other you have to take look at pivot, dynamic sql and exec
– pascal sanchez
Mar 26 at 6:27
I hesitate to call it a duplicate, because you're really asking two questions, but here's the answer to one of them: stackoverflow.com/questions/46217397/… This isn't a PIVOT by the way, so don't bother researching that, but you DO need to useDynamic SQLto get the column names of your results from the second table.
– Tab Alleman
Mar 26 at 14:16
What does "my database is not a relational one" mean, if youre using SQLServer?
– Caius Jard
Mar 25 at 20:35
What does "my database is not a relational one" mean, if youre using SQLServer?
– Caius Jard
Mar 25 at 20:35
Greetings. Beside the fact that my RDBMS is MSSQL, as you can see in the table examples given, my data does not follow a relational model, as in, there are no foreign keys, and the data is only stored in 2 columns, using another table just to hold my metadata.
– Me_
Mar 25 at 20:39
Greetings. Beside the fact that my RDBMS is MSSQL, as you can see in the table examples given, my data does not follow a relational model, as in, there are no foreign keys, and the data is only stored in 2 columns, using another table just to hold my metadata.
– Me_
Mar 25 at 20:39
What version of SQLS?
– Caius Jard
Mar 26 at 5:18
What version of SQLS?
– Caius Jard
Mar 26 at 5:18
Hi in your table2 you doesn’t have fkey to link thoses with table1 ? Other you have to take look at pivot, dynamic sql and exec
– pascal sanchez
Mar 26 at 6:27
Hi in your table2 you doesn’t have fkey to link thoses with table1 ? Other you have to take look at pivot, dynamic sql and exec
– pascal sanchez
Mar 26 at 6:27
I hesitate to call it a duplicate, because you're really asking two questions, but here's the answer to one of them: stackoverflow.com/questions/46217397/… This isn't a PIVOT by the way, so don't bother researching that, but you DO need to use
Dynamic SQL to get the column names of your results from the second table.– Tab Alleman
Mar 26 at 14:16
I hesitate to call it a duplicate, because you're really asking two questions, but here's the answer to one of them: stackoverflow.com/questions/46217397/… This isn't a PIVOT by the way, so don't bother researching that, but you DO need to use
Dynamic SQL to get the column names of your results from the second table.– Tab Alleman
Mar 26 at 14:16
add a comment |
1 Answer
1
active
oldest
votes
May be this might help you. change the code as per your table names.
DECLARE @TABLE TABLE (
ID INT IDENTITY(1, 1)
,Info VARCHAR(MAX)
)
INSERT INTO @TABLE
VALUES ('<John Smith><1st Avenue><Miami,Florida><33101>')
INSERT INTO @TABLE
VALUES ('<Mary Walton><83th Street><New York, NY><1001>')
SELECT ID
,MAX(CASE
WHEN RNO = 1
THEN INFO
ELSE ''
END) AS [PERSON]
,MAX(CASE
WHEN RNO = 2
THEN INFO
ELSE ''
END) AS [Address]
,MAX(CASE
WHEN RNO = 3
THEN INFO
ELSE ''
END) AS [City]
,MAX(CASE
WHEN RNO = 4
THEN INFO
ELSE ''
END) AS [ZIP_C]
FROM (
SELECT ID
,REPLACE(VALUE, '<', '') INFO
,ROW_NUMBER() OVER (
PARTITION BY ID ORDER BY ID
) RNO
FROM @TABLE
CROSS APPLY string_split(Info, '>')
WHERE VALUE <> ''
) A
GROUP BY ID
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%2f55345887%2fcreate-table-using-data-rows-from-select-as-columns-on-it%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
May be this might help you. change the code as per your table names.
DECLARE @TABLE TABLE (
ID INT IDENTITY(1, 1)
,Info VARCHAR(MAX)
)
INSERT INTO @TABLE
VALUES ('<John Smith><1st Avenue><Miami,Florida><33101>')
INSERT INTO @TABLE
VALUES ('<Mary Walton><83th Street><New York, NY><1001>')
SELECT ID
,MAX(CASE
WHEN RNO = 1
THEN INFO
ELSE ''
END) AS [PERSON]
,MAX(CASE
WHEN RNO = 2
THEN INFO
ELSE ''
END) AS [Address]
,MAX(CASE
WHEN RNO = 3
THEN INFO
ELSE ''
END) AS [City]
,MAX(CASE
WHEN RNO = 4
THEN INFO
ELSE ''
END) AS [ZIP_C]
FROM (
SELECT ID
,REPLACE(VALUE, '<', '') INFO
,ROW_NUMBER() OVER (
PARTITION BY ID ORDER BY ID
) RNO
FROM @TABLE
CROSS APPLY string_split(Info, '>')
WHERE VALUE <> ''
) A
GROUP BY ID
add a comment |
May be this might help you. change the code as per your table names.
DECLARE @TABLE TABLE (
ID INT IDENTITY(1, 1)
,Info VARCHAR(MAX)
)
INSERT INTO @TABLE
VALUES ('<John Smith><1st Avenue><Miami,Florida><33101>')
INSERT INTO @TABLE
VALUES ('<Mary Walton><83th Street><New York, NY><1001>')
SELECT ID
,MAX(CASE
WHEN RNO = 1
THEN INFO
ELSE ''
END) AS [PERSON]
,MAX(CASE
WHEN RNO = 2
THEN INFO
ELSE ''
END) AS [Address]
,MAX(CASE
WHEN RNO = 3
THEN INFO
ELSE ''
END) AS [City]
,MAX(CASE
WHEN RNO = 4
THEN INFO
ELSE ''
END) AS [ZIP_C]
FROM (
SELECT ID
,REPLACE(VALUE, '<', '') INFO
,ROW_NUMBER() OVER (
PARTITION BY ID ORDER BY ID
) RNO
FROM @TABLE
CROSS APPLY string_split(Info, '>')
WHERE VALUE <> ''
) A
GROUP BY ID
add a comment |
May be this might help you. change the code as per your table names.
DECLARE @TABLE TABLE (
ID INT IDENTITY(1, 1)
,Info VARCHAR(MAX)
)
INSERT INTO @TABLE
VALUES ('<John Smith><1st Avenue><Miami,Florida><33101>')
INSERT INTO @TABLE
VALUES ('<Mary Walton><83th Street><New York, NY><1001>')
SELECT ID
,MAX(CASE
WHEN RNO = 1
THEN INFO
ELSE ''
END) AS [PERSON]
,MAX(CASE
WHEN RNO = 2
THEN INFO
ELSE ''
END) AS [Address]
,MAX(CASE
WHEN RNO = 3
THEN INFO
ELSE ''
END) AS [City]
,MAX(CASE
WHEN RNO = 4
THEN INFO
ELSE ''
END) AS [ZIP_C]
FROM (
SELECT ID
,REPLACE(VALUE, '<', '') INFO
,ROW_NUMBER() OVER (
PARTITION BY ID ORDER BY ID
) RNO
FROM @TABLE
CROSS APPLY string_split(Info, '>')
WHERE VALUE <> ''
) A
GROUP BY ID
May be this might help you. change the code as per your table names.
DECLARE @TABLE TABLE (
ID INT IDENTITY(1, 1)
,Info VARCHAR(MAX)
)
INSERT INTO @TABLE
VALUES ('<John Smith><1st Avenue><Miami,Florida><33101>')
INSERT INTO @TABLE
VALUES ('<Mary Walton><83th Street><New York, NY><1001>')
SELECT ID
,MAX(CASE
WHEN RNO = 1
THEN INFO
ELSE ''
END) AS [PERSON]
,MAX(CASE
WHEN RNO = 2
THEN INFO
ELSE ''
END) AS [Address]
,MAX(CASE
WHEN RNO = 3
THEN INFO
ELSE ''
END) AS [City]
,MAX(CASE
WHEN RNO = 4
THEN INFO
ELSE ''
END) AS [ZIP_C]
FROM (
SELECT ID
,REPLACE(VALUE, '<', '') INFO
,ROW_NUMBER() OVER (
PARTITION BY ID ORDER BY ID
) RNO
FROM @TABLE
CROSS APPLY string_split(Info, '>')
WHERE VALUE <> ''
) A
GROUP BY ID
answered Mar 26 at 7:13
MaheshMahesh
17410 bronze badges
17410 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55345887%2fcreate-table-using-data-rows-from-select-as-columns-on-it%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
What does "my database is not a relational one" mean, if youre using SQLServer?
– Caius Jard
Mar 25 at 20:35
Greetings. Beside the fact that my RDBMS is MSSQL, as you can see in the table examples given, my data does not follow a relational model, as in, there are no foreign keys, and the data is only stored in 2 columns, using another table just to hold my metadata.
– Me_
Mar 25 at 20:39
What version of SQLS?
– Caius Jard
Mar 26 at 5:18
Hi in your table2 you doesn’t have fkey to link thoses with table1 ? Other you have to take look at pivot, dynamic sql and exec
– pascal sanchez
Mar 26 at 6:27
I hesitate to call it a duplicate, because you're really asking two questions, but here's the answer to one of them: stackoverflow.com/questions/46217397/… This isn't a PIVOT by the way, so don't bother researching that, but you DO need to use
Dynamic SQLto get the column names of your results from the second table.– Tab Alleman
Mar 26 at 14:16