Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefix all columns in T-SQL statement

Given a table "ABC" with columns Col1, Col2 and Col3 it is possible to automatically generate something like the following:

SELECT
Col1 AS 'ABC_Col1', 
Col2 AS 'ABC_Col2',
Col3 AS 'ABC_Col3' 
FROM ABC

I have a table without a fixed set of columns (users are able to append their own columns) where I still need the column prefix (because it is needed in a JOIN/CTE with other tables that also have columns with the names Col1, Col2 etc...)

Therefore I would like to be able to write something like this:

SELECT
T0.* AS 'ABC_T.*', 
FROM ABC T0

Which is of course not valid SQL, but can it be done somehow so the "*" columns all get the same prefix?

like image 432
RWJ Avatar asked Dec 02 '09 16:12

RWJ


People also ask

How do I find the datatype of all columns in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.

What is prefix in SQL query?

Prefix enables developers to easily see what their code is doing as they write and test their code. Including SQL queries, HTTP calls, errors, logs, and much more. This makes Prefix really handy for viewing SQL queries your code is using. Prefix is free!

What is SELECT T * in SQL?

The SELECT statement is the most used statement in the T-SQL language. It is executed to retrieve columns of data from one or more tables. The SELECT statement can constrain the data returned by using the WHERE or HAVING clause, and sort or group results using the ORDER BY and GROUP BY clauses, respectively.


2 Answers

This will give you a map of old column names and new column names:

SELECT syscolumns.name as old_column_name, 'ABC_' + syscolumns.name as new_column_name
   FROM sysobjects 
        JOIN syscolumns ON sysobjects.id = syscolumns.id
   WHERE sysobjects.name = 'ABC'
ORDER BY sysobjects.name,syscolumns.colid

From there it's just some dynamic sql. I'm still playing with it.

EDIT

OK, I ditched that.

DECLARE @sql varchar(max)
SET @sql = 'SELECT '

DECLARE @old_column_name varchar(50)
DECLARE @getNext CURSOR
SET @getNext = CURSOR FOR 
    SELECT syscolumns.name
       FROM sysobjects 
            JOIN syscolumns ON sysobjects.id = syscolumns.id
       WHERE sysobjects.name = 'ABC'
OPEN @getNext
FETCH NEXT FROM @getNext INTO @old_column_name
WHILE @@fetch_status = 0
BEGIN

    --BUILD DYNAMIC SQL
    SET @sql = @sql + @old_column_name + ' AS ''ABC_' + @old_column_name + ''', '

FETCH NEXT FROM @getNext INTO @old_column_name
END
CLOSE @getNext
DEALLOCATE @getNext

--REMOVE FINAL COMMA AND ADD TABLE
SET @sql = SUBSTRING(@sql, 0, LEN(@sql)) + ' FROM ABC'

exec(@sql)

A) this is terrible performance (because it's a cursor)

B) I know you're not meant to do work for people on here, but I got carried away.

C) I considered not even posting this because of how poor of an answer I feel it is, but it's a least an idea.

like image 83
Nick Spiers Avatar answered Sep 27 '22 18:09

Nick Spiers


You seem confused as to what column aliases do. As you can see in your select clause, you're already only selecting fields from T0 by referencing T0.*. You can still reference those fields as T0.<whatever> later in your query without aliasing the fields, you will just have to refer to them by their full field name, ie, T0.[My Users Suck And Make Really Long Field Names].

EDIT: To be more clear, you can not change the prefix of a field by aliasing it. You can only change the name of it. The prefix of the field is the alias of the table that it comes from.

like image 35
Donnie Avatar answered Sep 27 '22 19:09

Donnie