-1

"as [item] を使用してから、クエリで item 変数を使用することはできません。

例えば:

select c.category as [category],c.orderby as [CatOrder], m.masterno, m.master
,-- select OUT (select count(*) from    rentalitem       ri  with (nolock), 
            rentalitemstatus ris with (nolock), 
            rentalstatus     rs  with (nolock)
    where   ri.rentalitemid    = ris.rentalitemid
            and   ris.rentalstatusid = rs.rentalstatusid
            and   ri.masterid        = m.masterid
            and   rs.statustype     in ('OUT', 'INTRANSIT', 'ONTRUCK'))  as [qtyout] 
,-- select OWNED owned=
(select top 1 mwq.qty                                           
    from    masterwhqty mwq                                              
    where   mwq.masterid    = m.masterid) 

, -([owned]-[qtyout]) as [Variance]

from master m 
    inner join category c on c.categoryid=m.categoryid and c.categoryid=@category
    inner join inventorydepartment d on c.inventorydepartment=@department

分散を計算するときに qtyout または owned を使用できないようです。どうやってやるの?

4

3 に答える 3

2

テーブル変数を使用して、上記のようにそのテーブル変数を参照することもできます.... MSDNの例を次に示します

USE AdventureWorks2012;
GO
DECLARE @MyTableVar table(
    EmpID int NOT NULL,
    OldVacationHours int,
    NewVacationHours int,
    ModifiedDate datetime);
UPDATE TOP (10) HumanResources.Employee
SET VacationHours = VacationHours * 1.25,
    ModifiedDate = GETDATE() 
OUTPUT inserted.BusinessEntityID,
       deleted.VacationHours,
       inserted.VacationHours,
       inserted.ModifiedDate
INTO @MyTableVar;
--Display the result set of the table variable.
SELECT EmpID, OldVacationHours, NewVacationHours, ModifiedDate
FROM @MyTableVar;
GO
--Display the result set of the table.
SELECT TOP (10) BusinessEntityID, VacationHours, ModifiedDate
FROM HumanResources.Employee;
GO
于 2013-04-02T21:06:28.827 に答える