Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the value of a column in a SELECT?

In MySQL I could execute the following:

SELECT "spam" AS "foo", "eggs" AS "bar" LIMIT 1;

...which would return 1 row with 2 columns ("foo" and "bar) and 2 values ("spam" and "eggs").

Is it possible to do this with TSQL in SQL Server?

like image 277
Phillip B Oldham Avatar asked Dec 12 '22 08:12

Phillip B Oldham


1 Answers

You can do any of these:

SELECT 'spam' foo, 'eggs' bar

SELECT 'spam' AS foo, 'eggs' AS bar

SELECT foo = 'spam', bar = 'eggs'

SELECT 'spam' "foo", 'eggs' "bar"

SELECT 'spam' [foo], 'eggs' [bar]

Since you're building a hard-coded set of values you don't need a limiting clause. Just FYI, the equivalent in T-SQL is TOP.

like image 126
Yuck Avatar answered Dec 28 '22 07:12

Yuck