我正在编写一个函数,其中我传递了一个表值参数。在某些情况下,表值参数可能为空。
因此,我的函数如下所示-
CREATE FUNCTION [dbo].[testTableValueParam]
(
@created_date datetime = null
,@Ids dbo.IdList readonly
)
RETURNS TABLE
AS
RETURN
(
SELECT top 10
name
,scores
,mgr_name
from dbo.employee
where
created_date = @created_date
and
employeeId in (select empid from @Ids) --ignore this condition when @Ids is empty.
)
我的表类型如下所示-
CREATE TYPE [dbo].[IdList] AS TABLE(
[empid] [nvarchar](11) NULL
)
我正在从C#代码中调用我的函数。
如果表值参数为空,我想忽略where子句中的条件。
在搜索答案时,我查看了一些链接,但之前的帖子中提供的答案没有解决我的问题。
所以,现在当@Ids参数为空时,它会给我返回零条记录。
在一些帖子中,他们建议根本不要传递表值参数,它会自动将其视为一个空表。
但是我有时需要传递具有数据的参数。
一些答案建议使用
if exist(select 1 from @Ids)
但我无法在where子句中使用if exist。
请提供任何建议。
非常感谢您的回复。
谢谢。