In SQL Server 2014, I have created a simple stored procedure which does a bulk insert; what I'm struggling with is capturing if the bulk insert succeed or failed and returning 0 or 1 respectively.
The stored procedure code is:
ALTER PROCEDURE [dbo].[BulkInsert]
@file_name nvarchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @bulk_insert NVARCHAR(2000);
SET @bulk_insert =
N'BULK INSERT log_door_access FROM ''' +
@file_name +
N''' WITH (FIELDTERMINATOR = ''\t'', ROWTERMINATOR = ''0x0A'');';
EXEC sp_executesql @bulk_insert;
END
I'm trying to capture the EXEC
result and return a 1 if all went well and 0 if an error occurred.
something like
ALTER PROCEDURE [dbo].[BulkInsert]
@file_name nvarchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @bulk_insert NVARCHAR(2000);
SET @bulk_insert =
N'BULK INSERT log_door_access FROM ''' +
@file_name +
N''' WITH (FIELDTERMINATOR = ''\t'', ROWTERMINATOR = ''0x0A'');';
declare @r int
EXEC @r = sp_executesql @bulk_insert;
select @r
END
should do, or use an output parameter for sp_executesql, or a try catch block.
可以使用TRY...CATCH
'为Transact-SQL实施错误处理,类似于Microsoft Visual C#中的异常处理...'。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.