2011-12-22 16 views
0

このクエリの結果として、私はテーブルを持っている:次のクエリで前のクエリの結果を使用するにはどうすればよいですか?

select i.id, o.[name] from Item i 
LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20)) 
where o.name is not null 

enter image description here

今、私は次のクエリでは、この表の結果を使用する必要があります。

select PriceListItem.ProductExternalId, @id.Id, @id.FriendlyName, @id.BriefWiki, 
    [PriceListItem].[ProductExternalDesc] 
from [@id] 
inner join [Product] on Product.ItemId = @name and Product.InstanceId = @id.ID 
inner join [PriceListItem] on Product.ID = PriceListItem.ProductId 

の代わりに " @id '名前= idのテーブルのデータを使用し、' @name 'の代わりに名前=名前のテーブルのデータを使用する必要があります

答えて

1

あなたは'それはあなたがそれらをJOINをどのように計画するか伝えることは難しいだけでクエリから

-- You may need a ; before WITH as in ;WITH 
WITH FirstQuery AS (
    select i.id, o.[name] from Item i 
    LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20)) 
    where o.name is not null 
) 
select PriceListItem.ProductExternalId, 
     FQ.Id, 
     -- Neither of these are in your FirstQuery so you can not use them 
     -- @id.FriendlyName, @id.BriefWiki, 
     [PriceListItem].[ProductExternalDesc] 
from FirstQuery FQ 
    inner join [Product] on Product.ItemId = FQ.name 
         and Product.InstanceId = FQ.ID 
    inner join [PriceListItem] on Product.ID = PriceListItem.ProductId; 

が、これは、あなたが次の1に最初のクエリを利用することができます:SQL 2K8の再あなたは、CTEを使用することができます。

2番目のクエリに構文エラーがあるようです - @id.id

+0

で働く私は、このスクリプトで最初のクエリを使用する方法を理解していませんどこから私のパラメータを得ることができますか?説明できますか? – revolutionkpi

+0

エラーはありません。最初のクエリから取得する方法がわからないパラメータで名前を変更しました。 – revolutionkpi

+0

'@ id'は実際にはテーブル変数ですか? – Yuck

0
select i.id, o.[name] from Item i 
into @temp_table 
LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20)) 
where o.name is not null 

今、あなたは:)

1

標準SQLの方法をしたいとあなたは@temp_table使用することができますが、ほとんどのRDBMS

select PriceListItem.ProductExternalId, @id.Id, @id.FriendlyName, @id.BriefWiki, 
    [PriceListItem].[ProductExternalDesc] 
from 
    (
    select i.id, o.[name] from Item i 
    LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20)) 
    where o.name is not null 
    ) 
    X 
    inner JOIN 
    [@id] ON X.id = @id.id --change here as needed 
    inner join [Product] on Product.ItemId = @name and Product.InstanceId = @id.ID 
    inner join [PriceListItem] on Product.ID = PriceListItem.ProductId* 
+0

質問を読んだところで、副選択は単にクエリに追加されるとは考えられませんでした。* '@ id'ではなく*にする必要があります。 –

関連する問題