0
/* result 1 */
 select Id, Name
 from Items


/* result 2 */
 select Id,
        Alias
 from ItemAliases
 where Id in (
     select Id, Name
     from table abc
 )

SQL Server 2008 を使用しています。

上記の例を使用すると、私がやろうとしていることは非常に簡単です。

クエリ 1 の結果を返し、クエリ 2 の結果を返す必要があります。

ただし、クエリ 2 は、結果 1 のレコードのみを含めるようにフィルター処理する必要があります。

これが、私が最終的に何をしたいのかを示すための私の試みです。

VAR A = (
   select Id, Name
   from Items
)

/* result 1 */
 select A.*

/* result 2 */
 select Id,
        Alias
 from ItemAliases
 where Id in ( A.Id )
4

2 に答える 2

1

Result1 を保存し、それを使用して Result2 を構成したいだけだと思います。

declare @Result1 table (Id int primary key, Name varchar(100));

insert into @Result1
    -- store Result1
    select Id, Name
    from Items

--return Result1
select Id, Name 
from @Result1;


--return Result2 using stored Result1
select Id,
    Alias
from ItemAliases
where Id in (select Id from @Result1);
于 2014-01-08T00:03:28.547 に答える
0
--Result 1
SELECT  ID, Name
FROM    Items
[You WHERE Clause here if any]

--Result 2
SELECT Id, Alias
 FROM ItemAliases ia
        INNER JOIN Items i ON ia.ID = i.ID

また

--Using temporay in memory table
DECLARE @abc AS TABLE (
ID AS Int,
Name AS varchar(25)
)

SELECT  ID, Name
INTO    @abc
FROM    Items
[You WHERE Clause here if any]

--Result 1
SELECT * FROM @abc

--Result 2
SELECT Id, Alias
 FROM ItemAliases ia
        INNER JOIN @abc i ON ia.ID = i.ID
于 2014-01-08T00:04:04.203 に答える