編輯:根據以下答案,我添加了「dbo」。並提出澄清意見。SQL Server:作爲另一個函數的參數
問題:如何將SQL用戶定義函數(UDF)作爲另一個UDF的參數傳遞?請注意,該函數返回一個表。
select dbo.fn_Scalar() -- Returns 'sa'.
select * from dbo.fn_Tabler('sa') -- Returns a table (not scalar).
select dbo.fn_ScalarArg(dbo.fn_Scalar()) -- Returns 'sa'
select * from dbo.fn_Tabler(dbo.fn_Scalar()) -- ERROR: Incorrect syntax near '.'.
(目前使用SQL Server 2008 R2)
編輯:爲了便於調試,這裏是示例代碼來創建上述功能:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_Scalar]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.fn_Scalar
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_Tabler]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.fn_Tabler
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_ScalarArg]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.fn_ScalarArg
GO
create function fn_Scalar()
returns char(200) as
begin
declare @user char(200)
select @user = 'sa'
return @user
end
GO
create function fn_Tabler(@User char(200))
returns table as
return select @User [User]
GO
create function fn_ScalarArg(@User char(200))
returns char(200) as
begin
declare @u2 char(200)
select @u2 = @User
return @u2
end
go
是否'選擇*從fn_Tabler(選擇fn_Scalar())'工作? – cdhowie
@cdhowie:'select * from dbo.fn_Tabler(select dbo.fn_Scalar())'yield「在關鍵字'select'附近有錯誤的語法''和'near syntax''''' –
嗯,這值得一試。 – cdhowie