存储过程调用其他过程返回值的问题

gingerkang 2007-07-25 05:57:22
下面是两个简单做测试的存储过程,过程getd怎么得不到过程d的output值?
CREATE procedure d
@d int output
as
select @d=@d*1000
--print @d
GO

create procedure getd
@d int output
AS
exec sp_executesql N'exec d @d',N'@d int output',@d output
print @d
go

exec getd 1 print出来是1,不是1000
...全文
268 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaolei1982 2007-07-26
  • 打赏
  • 举报
回复
declare @d int
exec getd @d output
print @d
xiaolei1982 2007-07-26
  • 打赏
  • 举报
回复
CREATE procedure d
@i int,
@d int output
as
select @d=@i*1000
--print @d
GO

drop proc d

declare @v int
exec d 2,@v output
print @v

create procedure getd
@d int output
AS

exec sp_executesql N'exec d 2, @v output',N'@v int output',@d output
go
OracleRoob 2007-07-25
  • 打赏
  • 举报
回复

动态sql语句基本语法
1 :普通SQL语句可以用Exec执行

eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N

2:字段名,表名,数据库名之类作为变量时,必须用动态SQL

eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格

当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名

declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错



declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确

3. 输出参数
declare @num int, @sql nvarchar(4000)
set @sql='select count(*) from tableName'
exec(@sql)


--如何将exec执行结果放入变量中?

declare @num int, @sql nvarchar(4000)
set @sql='select @a=count(*) from tableName '
exec sp_executesql @sql,N'@a int output',@num output
select @num
OracleRoob 2007-07-25
  • 打赏
  • 举报
回复
--如何将exec执行结果放入变量中?

declare @num int, @sql nvarchar(4000)
set @sql='select @a=count(*) from tableName '
exec sp_executesql @sql,N'@a int output',@num output
select @num

gingerkang 2007-07-25
  • 打赏
  • 举报
回复
create procedure getd
@d int output
AS
exec sp_executesql N'exec d @d output',N'@d int output',@d output
print @d
go
这样也可以
还是自己搞定了
gingerkang 2007-07-25
  • 打赏
  • 举报
回复
找到一个方法,不知道为什么用sp_executesql 就是不行
create procedure getd
@d int output
AS
exec d @d output
print @d
go
gingerkang 2007-07-25
  • 打赏
  • 举报
回复
希望明白的能回答一下,或者帮忙改写出一个能达到目的的存储过程getd
echiynn 2007-07-25
  • 打赏
  • 举报
回复
你第一個存儲過程都沒有輸入參數,祇有一個輸出參數@d,怎麼給@d賦值呢?
ojuju10 2007-07-25
  • 打赏
  • 举报
回复
你的存储过程d 有问题,返回的都是null
ojuju10 2007-07-25
  • 打赏
  • 举报
回复
create procedure getd
@d int,
@Getd int output
AS
exec d @d,@Get output

go

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧