我在尝试从 SQL 函数返回表时遇到一些问题,其中创建表的 SQL 是动态编写的。
到目前为止,我有:
CREATE FUNCTION dbo.SEL_PCD
(
@COBDate AS DATETIME,
@FileName AS VARCHAR(50),
@PC AS VARCHAR(50),
@MyList AS VARCHAR(max),
DECLARE @SQL VARCHAR(max)
SET @SQL = 'SELECT * FROM
(SELECT tab1.TID FROM
(SELECT TID FROM dbo.SEL_RT('+@COBDate+','+@FileName+') WHERE BID IN ('+ @MyList +')) tab1
JOIN
(SELECT TID FROM CT WHERE (Col_Name LIKE %' + @PC + '% OR Bk LIKE %' + @PC + '%) AND FileName = ' + @FileName + ' AND COBDate = @COBDate) tab2
ON tab1.TID = tab2.TID) tab3
JOIN
(SELECT TID, Value FROM CR WHERE BID IN (' + @MyList + ') AND COBDate = ' + @COBDate + ' AND FileName = ' + @FileName + 'AND ScenID = 266) tab7
ON tab3.TID = tab7.TID'
)
RETURNS TABLE AS
RETURN
(
EXEC sp_executesql @SQL
)
GO
我在声明 SQL 变量时遇到错误。我可以通过执行命令返回表吗?