函数内部的sql

dsf9zpds  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(373)
ALTER FUNCTION [dbo].[FSmapping] (
    @runID as bigInt,
    @TypMap as char(1)
    )

RETURNS TABLE
AS
RETURN

IF (@TypMap ='C')
BEGIN
SELECT b.[A/C],g.accountNumber, b.[Process], g.[endingBalanceLC], g.beginningBalanceLC
FROM table1 g
LEFT JOIN  table2  b ON b.[Acc2]=g.Account_Synt
where RunID = @runID
END

我试图在sql中实现一个表函数。基本上应该还有3个 IF 唯一要切换的是表2,从基于输入的表2-4开始。错误是 IF 语句,我知道缺少了一些东西,我很难用table作为返回值实现某种开关函数。select在没有if语法的情况下可以完美地工作,它还表示if中的两个变量有错误
必须声明标量变量runid和typmap

rjjhvcjd

rjjhvcjd1#

您需要将查询更改为多语句表值函数,以便函数必须如下所示:

CREATE OR ALTER FUNCTION [dbo].[FSmapping] (
    @runID as bigInt,
    @TypMap as char(1)
    )
RETURNS @Table1JoinTable2 
TABLE (
--table definition here 
Col1 VARCHAR(10)
)
AS
BEGIN
IF (@TypMap ='C')
BEGIN
INSERT INTO @Table1JoinTable2 
SELECT @TypMap
--Put select query SELECT b.[A/C],g.accountNumber, b.[Process], g.[endingBalanceLC], g.beginningBalanceLC
--FROM table1 g
--LEFT JOIN  table2  b ON b.[Acc2]=g.Account_Synt
--where RunID = @runID
END
IF (@TypMap ='I')
BEGIN
INSERT INTO @Table1JoinTable2 
SELECT @TypMap
--Put your select query
END
IF (@TypMap ='I')
BEGIN
INSERT INTO @Table1JoinTable2 
SELECT @TypMap
--Put your select query
END
IF (@TypMap ='R')
BEGIN
INSERT INTO @Table1JoinTable2 
SELECT @TypMap
--Put your select query
END
IF (@TypMap ='S')
BEGIN
INSERT INTO @Table1JoinTable2 
SELECT @TypMap
--Put your select query
END
RETURN
END
zujrkrfu

zujrkrfu2#

如果typmap没有返回c,函数将返回什么?
我想你需要“别的”。

IF Boolean_expression
BEGIN
    -- Statement block executes when the Boolean expression is TRUE
END
ELSE
BEGIN
    -- Statement block executes when the Boolean expression is FALSE
END

相关问题