gpt4 book ai didi

json - 如何处理SQL中的错误并控制存储过程

转载 作者:行者123 更新时间:2023-12-03 08:39:03 25 4
gpt4 key购买 nike

我从未学习过T-SQL中的错误陷阱。真是有时curl.xget

SELECT @JSON = curl.xget(null, ''+@GETSTRING+'') 

INSERT INTO [pcrd].[stg1_fredSeries]
([SeriesCode]
,[realStartTime]
,[realEndTime]
,[observationDate]
,[observationValue]
,[etl_dateUpdate]
,[ETL_UserUpdate]
,fk_series_id
,api_url)
SELECT
@SERIES_CODE AS SeriesID,
*,
GETDATE() AS etl_dateUpdate,
'sp_get_fredSeriesPCTpe4' AS etl_userUpdate,
@api_type,
@GETSTRING
FROM
OPENJSON(@JSON,'$.observations')
引发错误:

Msg 6522, Level 16, State 1, Procedure pcrd.sp_get_fredSeriesPCType4, Line 72 [Batch Start Line 20]
A .NET Framework error occurred during execution of user-defined routine or aggregate "XGET":
System.Net.WebException: The remote server returned an error: (400) Bad Request.
System.Net.WebException:
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at Curl.Get(SqlChars H, SqlChars url)


这只是问题还是引发错误并寻找Msg 6522?此插入和 @GETSTRING的构建位于游标提取中。游标提取中可以有BEGIN TRY吗?假设这是要走的路。

最佳答案

由于您要对curl.xget进行单个调用,因此可以在try/catch中捕获错误。

fetch next from thecursor into @GETSTRING;

--better to initialize @json on each iteration
select @JSON = null;

begin try
SELECT @JSON = curl.xget(null, ''+@GETSTRING+'')
end try
begin catch
--do something with the error, log etc...or just silence it
end catch

--when error for the @json assignment, @json will be null and no insertion happens
--because openjson returns no result
INSERT INTO [pcrd].[stg1_fredSeries]
SELECT
FROM
OPENJSON(@JSON,'$.observations')

关于json - 如何处理SQL中的错误并控制存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63823564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com