简体   繁体   中英

SQL alias from subquery return?

I have a this procedure (simplified):

CREATE PROCEDURE myProcedure
    @var1 int,
    @var2 NVARCHAR(50),
    @var3 NVARCHAR(50),
    @var4 NVARCHAR(50)  
AS
BEGIN
   SELECT 
       stuff AS (SELECT date FROM dateTable WHERE condition), 
       otherStuff
       moreStuff
   FROM
       myTable
   WHERE
       myConditions;
END

It says that I have incorrect syntax in two places:

  1. line 2 near the word SELECT
  2. line 2 near ')'
  3. line 9 near the word AS

I know that the subquery only returns 1 string (and will only return 1 string no matter what).

It worked before I added the subquery, so something must be messing it up.

EDIT: To clarify, I want to print the values from "stuff" but the name of the column should be what my subquery returns, not the other way around =)

2nd EDIT:

Ok, let's say that I have the tables: myTable1 and myTable2 .

This is myTable1 :

| stuff | otherStuff | otherStuff |
|   x   |    x       |    x       |
|   x   |    x       |    x       |
             ...

This is myTable2 :

| date  | id |
| x/x/x | 1  |
| x/x/x | 2  | 
     ...

This is what I want my SELECT to return:

| x/x/x | otherStuff | otherStuff |
|   x   |    x       |    x       |
|   x   |    x       |    x       |

It should be this way:

  SELECT 
       (SELECT date FROM dateTable WHERE condition) AS stuff , 
       otherStuff
       moreStuff
   FROM
       myTable
   WHERE
       myConditions;

The alias should be after the correlated subquery, this assuming that this subquery returns only one value.

Another syntax is to write it this way:

  SELECT 
       stuff = (SELECT date FROM dateTable WHERE condition), 
       otherStuff
       moreStuff
   FROM
       myTable
   WHERE
       myConditions;

Update

As @gbn said in his answer, you have to use dynamic SQL to do so, it makes no sense really, but if you want to this any way you can do something like this:

DECLARE @columnname VARCHAR(20);
DECLARE @query VARCHAR(2000);

SELECT @columnname =  QUOTENAME([date]) FROM dateTable;


SELECT @query = 'SELECT (SELECT date FROM dateTable) AS' +  @columnname + 
                 ' , otherStuff, moreStuff FROM myTable';

execute(@query);

Like thi:

Alias uses AS after or = before

SELECT 
   stuff = (SELECT date FROM dateTable WHERE condition), 
   otherStuff
   moreStuff
FROM
   myTable
WHERE
   myConditions;

SELECT 
   (SELECT date FROM dateTable WHERE condition) AS stuff, 
   otherStuff
   moreStuff
FROM
   myTable
WHERE
   myConditions;

After question edits

You can not have dynamic column names in a normal select. It makes no sense really because now your return dataset is different depending on the subquery.

You can use one of

  • dynamic SQL to build the query with the desired alias
  • add an extra column that acts as the column name

Example:

SELECT 
   (SELECT date FROM dateTable WHERE condition) AS StuffColumnName, 
   stuff,
   otherStuff
   moreStuff
FROM
   myTable
WHERE
   myConditions;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM