简体   繁体   English

来自子查询的SQL别名返回?

[英]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 SELECT字旁边的第2行
  2. line 2 near ')' ')'附近的第2行
  3. line 9 near the word AS 字词AS附近的第9行

I know that the subquery only returns 1 string (and will only return 1 string no matter what). 我知道子查询仅返回1个字符串(无论如何都将仅返回1个字符串)。

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 . 好的,假设我有两个表: myTable1myTable2

This is myTable1 : 这是myTable1

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

This is myTable2 : 这是myTable2

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

This is what I want my SELECT to return: 这是我希望我的SELECT返回的内容:

| 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: 正如@gbn在他的回答中所说,您必须使用动态SQL来进行操作,这实际上没有任何意义,但是如果您想以任何方式这样做,则可以执行以下操作:

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 Alias在或=之前使用AS

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 动态SQL以使用所需别名构建查询
  • 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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