尝试创建一个表函数,该函数将返回一个两列表,该表必须以if语句为条件。
我还有另外两个功能可以分开执行此操作,但是合并后无法正常工作。
create function dbo.PDM_GetCategorytable (@Mystring varchar(200) ,@appid int)
returns table
as -- begin
--return
begin
-- declare @MyString varchar(200);
--set @MyString= 'Fred mtf is dead'--'RE: [Encrypt]]FW: MTF Military ---
--UPDATE URGENT'
--declare @AppId int =5
declare @Dubb Table (Col varchar(200));
insert into @dubb
(Col )
values ( @Mystring);
if ((select top 1 categoryid from @dubb a
left join
(SELECT (pass.Recordid) as categoryid , pass.appid,
pass.Priority_Identifier, pass.PrioritySortorder, pass.Category,
ps.Search_String AS SrchVar
FROM dbo.PDM_PriorityAssignments AS pass INNER JOIN
dbo.PDM_Priority_Search AS ps ON pass.Recordid =
ps.PriorityAssignmentid where pass.appid=@AppId ) b on a.col like '%' +
b.SrchVar + '%'
order by PrioritySortorder) is not null)--'where
appid=@AppId
begin
select top 1 categoryid,Category from @dubb a
left join
(SELECT (pass.Recordid) as categoryid , pass.appid,
pass.Priority_Identifier, pass.PrioritySortorder, pass.Category,
ps.Search_String AS SrchVar
FROM dbo.PDM_PriorityAssignments AS pass INNER JOIN
dbo.PDM_Priority_Search AS ps ON pass.Recordid =
ps.PriorityAssignmentid where pass.appid=@AppId ) b on a.col like '%' +
b.SrchVar + '%'
order by PrioritySortorder;
end
else
begin
select recordid as categoryid,Category FROM dbo.PDM_PriorityAssignments
AS pass where appid=@AppId and Priority_Identifier like 'Routine'
end
return
end;
预期结果将返回两列,类别ID和类别。
答案 0 :(得分:0)
看起来像是多语句表值函数的重复项 Multi-statement Table Valued Function vs Inline Table Valued Function
这是一个实现此目标的示例。
CREATE FUNCTION foo
(@param int)
RETURNS
@T TABLE (id int, val varchar)
As
BEGIN
IF (@param = 1)
insert into @t Select 1, 'a';
else
insert into @t Select 0, 'a';
return ;
END
GO
答案 1 :(得分:0)
问题恰恰是因为您需要使用算法。
该要求可防止您仅返回表。在这种情况下,您需要做一些不同的事情。
首先定义结果表,然后根据需要插入值。
没有您的数据,我将无法运行它,但是它应该可以工作。
CREATE FUNCTION dbo.PDM_GetCategorytable (
@Mystring VARCHAR(200)
,@appid INT
)
RETURNS @GetCategorytable TABLE
(
[CategoryId] INT,
[Category] VARCHAR(200))
AS -- begin
--return
BEGIN
-- declare @MyString varchar(200);
--set @MyString= 'Fred mtf is dead'--'RE: [Encrypt]]FW: MTF Military ---
--UPDATE URGENT'
--declare @AppId int =5
DECLARE @Dubb TABLE (Col VARCHAR(200));
INSERT INTO @dubb (Col)
VALUES (@Mystring);
IF (
(
SELECT TOP 1 categoryid
FROM @dubb a
LEFT JOIN (
SELECT (pass.Recordid) AS categoryid
,pass.appid
,pass.Priority_Identifier
,pass.PrioritySortorder
,pass.Category
,ps.Search_String AS SrchVar
FROM dbo.PDM_PriorityAssignments AS pass
INNER JOIN dbo.PDM_Priority_Search AS ps ON pass.Recordid = ps.PriorityAssignmentid
WHERE pass.appid = @AppId
) b ON a.col LIKE '%' + b.SrchVar + '%'
ORDER BY PrioritySortorder
) IS NOT NULL
) --'where
-- NOT SURE WHAT YOU ARE TRYING TO DO WITH THIS -- appid = @AppId
BEGIN
insert into @GetCategorytable(Categoryid, Category)
SELECT TOP 1 categoryid
,Category
FROM @dubb a
LEFT JOIN (
SELECT (pass.Recordid) AS categoryid
,pass.appid
,pass.Priority_Identifier
,pass.PrioritySortorder
,pass.Category
,ps.Search_String AS SrchVar
FROM dbo.PDM_PriorityAssignments AS pass
INNER JOIN dbo.PDM_Priority_Search AS ps ON pass.Recordid = ps.PriorityAssignmentid
WHERE pass.appid = @AppId
) b ON a.col LIKE '%' + b.SrchVar + '%'
ORDER BY PrioritySortorder;
END
ELSE
BEGIN
insert into @GetCategorytable(Categoryid, Category)
SELECT recordid AS categoryid ,Category FROM dbo.PDM_PriorityAssignments AS pass
WHERE appid = @AppId
AND Priority_Identifier LIKE 'Routine'
END
RETURN
END;