如何用sql语句查询出存储过程参数的默认值

ggw128 2011-06-16 09:52:18
如存储过程为
create procedure ptest
@a char(6),
@b int = 33
as
begin
... ...
end
怎样能能够用sql语句查询到@b的默认值是33
...全文
394 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-06-17
  • 打赏
  • 举报
回复
create procedure ptest
@a char(6),
@b int = 33
as
begin
print 3
end
go
;with cte as(
select object_definition((select id from sysobjects where name='ptest')) as s
),c2 as(
select ltrim(substring(s,charindex('=',s,charindex('@b',s))+1,charindex('as',s,charindex('@b',s))-charindex('=',s,charindex('@b',s))+1))s from cte
)select left(s,charindex(' ',s)-1) from c2
/*
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
33

(1 行受影响)
*/
go
drop procedure ptest
ggw128 2011-06-16
  • 打赏
  • 举报
回复
楼上也没有根本上解决问题啊。
叶子 2011-06-16
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/ms176074%28v=SQL.100%29.aspx

官方说明:
has_default_value
SQL Server 只维护该目录视图中的 CLR 对象的默认值;因此,对于 Transact-SQL 对象,此列包含值 0。若要查看 Transact-SQL 对象中的参数的默认值,请查询 sys.sql_modules 目录视图的 definition 列,或使用 OBJECT_DEFINITION 系统函数。

default_value
sql_variant 如果 has_default_value 为 1,则此列的值为该参数的默认值;否则为 NULL。

具体使用:

--第一种查看方式
select object_definition((select id from sysobjects where name='ptest'))

--第二种查看方式
select definition from sys.sql_modules
where object_id=(select id from sysobjects where name='ptest')

--第三种查看方式
select [text] from syscomments where id=
(select id from sysobjects where name='ptest')

叶子 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liangck 的回复:]

sys.parameters
[/Quote]

select name,parameter_id,max_length,default_value from sys.parameters
where object_id=(select id from sysobjects where name='ptest')
/*
name parameter_id max_length default_value
-------------------- ------------ ---------- ---------------
@a 1 6 NULL
@b 2 4 NULL
*/

这里显示的默认值是null
ggw128 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liangck 的回复:]
sys.parameters
[/Quote]

可是..........


create procedure ptest
@a char(6),
@b int = 33
as
begin
select ''
end


select default_value,* from sys.parameters where object_id=object_id('ptest')

default_value 查出为null,不知何故?
挨踢直男 2011-06-16
  • 打赏
  • 举报
回复
select name,default_value from sys.parameters where object_id = object_id('procname')
liangCK 2011-06-16
  • 打赏
  • 举报
回复
sys.parameters

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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