0

我有一个简单的存储过程。在ie 1001SalesTableItemId以不同的值重复 10 次。所以这个过程应该选择 10 个Amount和的值EmployeeName。然而,它只是返回第一次出现的一个值ItemId。这里有什么问题?

我希望该过程选择并返回 10 个Amount和的值EmployeeName。当我在 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
4

1 回答 1

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
于 2013-09-11T20:17:04.633 回答