colbybhearn
colbybhearn

Reputation: 482

select value into variable and alias in SQL Server

I'd like to simultaneously do two things in an MSSQL query:

  1. select a field's value into a variable select @myvar = colName from tableName
  2. alias my column select colName as [myCol] from tableName

I've tried these things:

If this is possible, how can it be accomplished?

Upvotes: 0

Views: 2611

Answers (1)

HABO
HABO

Reputation: 15816

A select can either assign values to variables or return column values, but not both.

In some cases, e.g. when using select to provide data to an insert, an output clause may be useful. A tool well worth having in your pocket as it provides access to identity values from insert and both before and after values when used with update.

Upvotes: 3

Related Questions