在存储过程中返回多个值

时间:2013-09-11 20:01:14

标签: sql sql-server sql-server-2008

我有一个简单的存储过程。在SalesTableItemId,即1001重复10次,具有不同的值。因此,此过程应选择AmountEmployeeName的10个值。然而,它只是在第一次出现ItemId时返回一个值。这有什么不对?

我希望该过程选择并返回AmountEmployeeName的10个值。当我在SQL Server Management Studio中单独运行查询时,它返回所有值,但在存储过程中只返回第一个值。请帮忙。

ALTER procedure entitiesRead
   @ItemId               integer
   ,@SessionId           integer      
as
begin
   select
       a.amount as Amount
       b.EmployeeName               
   from 
       salesTable a
   left outer join 
       nameEmployees b on (b.id = a.employeeId)
   where 
       ItemId = @ItemId
end

1 个答案:

答案 0 :(得分:2)

您的查询对我不起作用;它包含一个错误。在您选择的“金额”后添加逗号:

select
a.amount as Amount,
b.EmployeeName               
from salesTable a
left outer join nameEmployees b on(b.id=a.employeeId)
where ItemId=@ItemId

顺便说一句,这是我的测试,它运行良好。您可能还需要仔细检查表格中的数据:

CREATE TABLE #salesTable ( ItemId int, amount money, employeeId int )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 100.00, 1 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 101.00, 1 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 102.00, 1 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 103.00, 1 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 104.00, 1 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 105.00, 2 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 106.00, 2 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 107.00, 2 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 108.00, 2 )
INSERT INTO #salesTable ( ItemId, amount, employeeId ) VALUES ( 1001, 109.00, 2 )

CREATE TABLE #nameEmployees ( id int, employeeName varchar(50) )
INSERT INTO #nameEmployees ( id, employeeName ) VALUES ( 1, 'John Smith' )
INSERT INTO #nameEmployees ( id, employeeName ) VALUES ( 2, 'Jane Doe' )

select
a.amount as Amount,
b.EmployeeName               
from #salesTable a
left outer join #nameEmployees b on(b.id=a.employeeId)
where ItemId=1001