我有一个链接到Oracle服务器的SQL Server 2014服务器。我想在函数中使用临时表从Oracle数据库返回数据集,然后使用my函数使用常规T-SQL返回结果。由于我对此很陌生,所以我很接近,但收到错误信息
Msg 156,Level 15,State 1,Procedure GetBond,Line 37
关键字' BEGIN'附近的语法不正确。
我在这里发布了功能代码:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetBond]
(@WarControlID bigint)
RETURNS VARCHAR(MAX)
AS
BEGIN
--Create Temp Table
declare @TSQL varchar(max)
DECLARE @WarrantBail table
(
WR_INVL varchar(5),
WR_WARR_CTL VarChar(10),
WR_Bail VarChar(50),
WC_BAIL VarChar(50)
)
SELECT @TSQL = 'SELECT * FROM OPENQUERY(RMSPROD2,''SELECT TIBURON.WRMAST.WR_INVL, TIBURON.WRMAST.WR_WARR_CTL,TIBURON.WRMAST.WR_BAIL,TIBURON.WRWCHG.WC_BAIL
FROM TIBURON.WRMAST
LEFT JOIN TIBURON.WRWCHG ON WRWCHG.WC_WR_CHAIN = WRMAST.WRMAST_ROW
WHERE TIBURON.WRMAST.WR_WARR_CTL = ''''' + @WarControlID + ''''''')'
INSERT INTO @WarrantBail
EXEC (@TSQL)
END
BEGIN
-- Create a Variable
DECLARE @NoBailCount int
DECLARE @ChgCount int
DECLARE @WarTotalBond float
DECLARE @CHGTotalBond float
DECLARE @War_Final_Bail varchar(max)
Select COUNT(DISTINCT w.WR_Bail) AS NoBond_Count
From @WarrantBail w
Where w.WC_BAIL In ('No Bond', 'No Bail','None') Or w.WR_Bail In ('No Bond', 'No Bail','None')
--***********Get Charge Count
Select COUNT(w.WC_BAIL) As ChgCount FROM @WarrantBail w
--******************IF the above fails then we have a bond check the Warrant bond amount
Select SUM (DISTINCT cast(w.WR_Bail As int)) AS WAR_Bond_Total
From @WarrantBail w
Where w.WR_Bail Not In ('No Bond', 'No Bail','None')
--****************We may have additional charges get the total for those charges
Select SUM (cast(w.WC_BAIL As int)) AS CHG_BondTotal
From @WarrantBail w
Where w.WC_BAIL Not In ('No Bond', 'No Bail','None')
IF (@NoBailCount > 0)
Begin
SET @War_Final_Bail = 'NO BAIL'
End
ELSE IF @ChgCount > 0
Begin
SET @War_Final_Bail = @WarTotalBond + @CHGTotalBond
End
Else
Begin
SET @War_Final_Bail = @WarTotalBond
End
RETURN CONVERT(varchar(max), @War_Final_Bail)
END
除了执行代码时的错误,我还看到Line" ALTER FUNCTION [dbo]下的一条波浪线。[GetBond]
该错误表明:
语法错误:' ALTER FUNCTION'必须是这一批中唯一的陈述。
这个错误是否意味着我无法在函数中创建临时表?
答案 0 :(得分:0)
为什么你有END和BEGIN?认为这可能是(你的)问题之一。
INSERT INTO @WarrantBail
SELECT *
FROM OPENQUERY(RMSPROD2, '
SELECT TIBURON.WRMAST.WR_INVL,
TIBURON.WRMAST.WR_WARR_CTL,
TIBURON.WRMAST.WR_BAIL,
TIBURON.WRWCHG.WC_BAIL
FROM TIBURON.WRMAST
LEFT JOIN TIBURON.WRWCHG ON WRWCHG.WC_WR_CHAIN = WRMAST.WRMAST_ROW
WHERE TIBURON.WRMAST.WR_WARR_CTL = ' + CAST(@WarControlID AS VARCHAR(30)) + ')')
你绝对必须使用动态SQL吗?为什么不做这样的事......
{{1}}
答案 1 :(得分:0)
感谢您的评论我是一名初级开发人员,在与一位高级开发人员讨论之后,我刚刚开始尝试使用存储过程来完成任务。
感谢您的评论。