摘要:ms sql 純量值函數(範例)
也就是傳回一個值,有可能是字串或數值,當然stored procedure也可以傳回值,但建議還是會寫成純量值函數
但我還是特別寫一下stored procedure如何傳回值
create procedure gf_autohid_gid
(
@compid nvarchar(10),
@ls_hid nvarchar(15) output --這邊就是要傳回值的參數
)
呼叫的方式
exec gf_autohid_gid 'compid',@hid output
---------------以下則是純量值函數的範例
其實很簡單,不多說明,如果看不懂的話,可以參考我之前寫的預存程序或trigger或自行google一下。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[fn_getordhid]
(
@incompid nvarchar(10),
@incustid nvarchar(10)
)
returns nvarchar(15) ''定值傳回的值
as
Begin
declare @ls_hid nvarchar(15) /*單號*/
declare @ls_tmp nvarchar(15)
declare @ld_tmp decimal(15,0)
set @ls_hid = ''
--單號
select @ls_hid = isnull(max(hid),'') from ordh
where compid = @incompid
and custid = @incustid
if @ls_hid = ''
begin
set @ls_tmp = ltrim(@incustid) + '0001'
end
else begin
set @ld_tmp = cast(right(@ls_hid,4) as int) + 1
set @ls_tmp = ltrim(@incustid) + right('0000'+cast(@ld_tmp as nvarchar(4)),4)
end
return @ls_tmp --傳回的變數值
END
呼叫方式
set @tmp=exec fn_getordhid 'incompid,'incustid'