在insert into语句中执行存储过程问题
存储过程
IF EXISTS (SELECT uname FROM person WHERE uname = 'getAvgAge')
DROP PROCEDURE getAvgAge
GO
CREATE PROCEDURE getAvgAge
@name char(10),
@avgage int OUTPUT
AS
DECLARE @Save INT
SET @Save = 0
SELECT @avgage=AVG(age)
FROM person
WHERE uname = @name
IF(@@ERROR <> 0)
SET @Save = @@ERROR
RETURN @Save
GO
DECLARE @returnvalue INT,@avg INT
EXEC @returnvalue = getAvgAge 'lw',@avg OUTPUT
PRINT '执行结果:'
PRINT '返回值='+CAST(@returnvalue as CHAR(2))
PRINT 'pews'+CAST(@avg as CHAR(10))
GO
INSERT INTO 中执行
CREATE TABLE per
(
uid int,
uname char(10),
age int
)
INSERT INTO per
EXEC getAvgAge
GO
我想问的是为什么表per中没有结果?请高手指教,谢谢
------解决方案--------------------SQL code
INSERT INTO per
EXEC getAvgAge
不报错 ? 没传参数 没返回值
------解决方案--------------------
SQL code
INSERT INTO per
EXEC getAvgAge
你定义的参数,但执行时去没有参数,
------解决方案--------------------
你的getAvgAge 必须有查询结果集.
insert into 存储过程的原理类似于insert into select
如果select 无结果,即无数据插入.
所以要有数据插入,存储过程执行后必须有结果集输出.
------解决方案--------------------
回帖是一种道德