Last active
July 31, 2023 07:03
-
-
Save KenjiOhtsuka/23f46f4165199ba9e87928c8a23a21e8 to your computer and use it in GitHub Desktop.
Generate SELECT SQL with listing all columns in the table (SQL Server)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- for one table | |
SELECT 'SELECT ' + STUFF(( | |
SELECT ', ' + name + CHAR(10) | |
FROM sys.columns | |
WHERE object_id = OBJECT_ID('table_name') | |
FOR XML PATH('') | |
), 1, 2, '') + ' FROM table_name;'; | |
-- if you want to generate such SQL for multiple tables: | |
SELECT (SELECT 'SELECT ' + STUFF((SELECT ', ' + name + CHAR(10) | |
FROM sys.columns | |
WHERE object_id = OBJECT_ID(table_name) | |
FOR XML PATH('')), 1, 2, '') + ' FROM ' + table_name + ';') | |
FROM (VALUES ('table_a'), | |
('table_b'), | |
('table_c'), | |
('table_d'), | |
('table_e')) AS t(table_name); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment