我在SQL Server中有两个表:
表A
ID Num
11 tj55
4 tj40
表B
ID NUM A_ID
3 se400 4
5 se500 4
我想创建一个存储过程,它将返回B中所有相应行中的每一行,我希望得到这个结果
NumA NumB
tj40 se400
tj40 se500
代码:
ALTER PROCEDURE [dbo].[Proc]
@param nvarchar(MAX),
AS
BEGIN
SELECT
aa.Num,
bb.Num
FROM
[dbo].[A] as aa
INNER JOIN
[dbo].[B] AS bb ON bb.A_ID = aa.ID
WHERE
aa.Num = @param
ORDER BY
aa.Num
END
这个存储过程工作正常并且正确地返回我需要但是当我添加OUTPUT
参数时(我需要)
ALTER PROCEDURE [dbo].[Proc]
@param nvarchar(MAX),
@RET1 varchar(MAX) OUTPUT,
@RET2 nvarchar(MAX) OUTPUT
AS
BEGIN
SELECT
@RET1= aa.Num,
@RET2 = bb.Num
FROM
[dbo].[A] as aa
INNER JOIN
[dbo].[B] AS bb ON bb.A_ID = aa.ID
WHERE
aa.Num = @param
ORDER BY
aa.Num
END
结果只有一行
NumA NumB
tj40 se400
非常感谢
答案 0 :(得分:0)
存储过程仅返回标量值,而不是行。它将打印查询结果,但用户定义的内联表函数更好:
CREATE FUNCTION [dbo].[ProcFunc] (
@param nvarchar(MAX)
)
RETURNS table
AS
RETURN (SELECT aa.Num, bb.Num
FROM [dbo].[A] aa INNER JOIN
[dbo].[B] bb
ON bb.A_ID = aa.ID
WHERE aa.Num = @param
ORDER BY aa.Num
);
然后您可以将其称为:
select *
from dbo.ProcFunc('abc');