I have the following query that gathers the column names for a table:
SELECT COLUMN_NAME
FROM theDBNameHere.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'markers'
That works just fine giving me the needed column names of that table. However, I want to rename the column names to something else. I tried the following:
SELECT venueID AS 'Venue ID',
venueEventDate AS 'Venue Date',
venueCounty AS 'Venue County',
venueAdvocate AS 'Venue Advocate',
venueSpanish AS 'in Spanish',
lastUpdated AS 'Last Updated',
updatedBy AS 'Updated By',
SysSrcLoadDt AS SysSrcLoadDt
FROM theDBNameHere.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'markers'
The error it tosses to me is pretty much saying
Invalid column name 'venueXXXXXXXX'
How would I go about changing the default column name with using the query above? Is this even possible to do in the first place?
If you want only column names:
SELECT venueID AS [Venue ID],
venueEventDate AS [Venue Date],
venueCounty AS [Venue County],
venueAdvocate AS [Venue Advocate],
venueSpanish AS [in Spanish],
lastUpdated AS [Last Updated],
updatedBy AS [Updated By],
SysSrcLoadDt AS [SysSrcLoadDt]
FROM markers
WHERE 1 = 2 -- column headers only, comment if you want rows also
or as Sean Lange
proposed in comment:
SELECT '' AS [Venue ID],
'' AS [Venue Date],
'' AS [Venue County],
'' AS [Venue Advocate],
'' AS [in Spanish],
'' AS [Last Updated],
'' AS [Updated By],
'' AS [SysSrcLoadDt]
WHERE 1 = 2;
EDIT:
If you wanted names in one column:
SELECT *
FROM (VALUES ('Venue ID'), ('Venue Date'), ('Venue County'),
('Venue Advocate'), ('in Spanish'), ('Last Updated'),
('Updated By'), ('SysSrcLoadDt') ) AS t(column_name);
Output:
╔════════════════╗
║ column_name ║
╠════════════════╣
║ Venue ID ║
║ Venue Date ║
║ Venue County ║
║ Venue Advocate ║
║ in Spanish ║
║ Last Updated ║
║ Updated By ║
║ SysSrcLoadDt ║
╚════════════════╝
May be you need the CASE
statement
SELECT CASE WHEN COLUMN_NAME = 'venueID'
THEN 'Venue ID'
WHEN COLUMN_NAME = 'venueEventDate'
THEN 'Venue Date'
WHEN COLUMN_NAME = 'venueCounty'
THEN 'Venue County'
-- do the rest of columns here
END AS COLUMN_NAME
FROM theDBNameHere.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'markers'
If you need to rename column names, use the Alter table and Rename Column keywords...
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.