当前位置: 代码迷 >> Sql Server >> sql存储过程参数有默认值时执行语句有关问题
  详细解决方案

sql存储过程参数有默认值时执行语句有关问题

热度:45   发布时间:2016-04-24 09:23:50.0
sql存储过程参数有默认值时执行语句问题
create proc proc_RestInsert
@restname varchar(50),
@catid int,
@restintro varchar(500),
@image varchar(200)='upfile//123.png',
@address varchar(200),
@phone varchar(20)
as
declare @name varchar(50)
select @name = RestCatName from tb_RestCategory where RestCatID=@catid
insert tb_Restaurant(ResttName,RestCatID,RestCatName,RestIntros,RestImage,RestAddress,RestPhone)
values(@restname,@catid,@name,@restintro,@image,@address,@phone)
exec proc_RestInsert '123',123,'123' , , '123','123'
select * from tb_Restaurant
我这个意思是image是有默认路径的。当我不指定图片路径时用默认的路径upfile//123.png. 可是
exec proc_RestInsert '123',123,'123' , , '123','123'   执行语句怎么写? 
------解决思路----------------------

exec proc_RestInsert '123',123,'123',@address= '123',@phone='123'   

或者改下参数位置,默认参数移到最后
------解决思路----------------------
两种办法,
一、用命名参数,比较符合规则逻辑
exec proc_RestInsert @restname='123',@catid=123,@restintro='123' ,@address= '123',@phone='123'

二、你可以直接赋值NULL,然后在存储过程里判断NULL就给默认值,不过,如果NULL也是想要的值这样做就不妥了
  相关解决方案