SQL Server ストアド プロシージャの句からデータベース エラーで構成される結果セットを返す必要がありますがCATCH
、それで行き詰まっています。結果セットを返すためにカーソルを使用する必要がありますか? その場合OUTPUT
、.NET アプリケーションのパラメーターの型宣言は何ですか? 試しObject
てみVariant
ましたが、うまくいきませんでした。
また、ステートメントを使用して返す簡単な方法も試しましたSELECT
が、あるストアドプロシージャでは機能しますが、別のストアドプロシージャでは機能しませんCATCH
。
while (@I <= @count)
begin
BEGIN TRY
-- delete all previous rows inserted in @customerRow for previous counts @I
delete from @customerRow
-- this is inserting the current row that we want to save in database
insert into @customerRow
SELECT
[id],[firstname], [lastname], [street], [city],
[phone],[mobile],[fax], [email], [companyName],
[licence],[brn], [vat], [companyStreet], [companyCity], [status]
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY id ASC) AS rownumber,
[id], [firstname], [lastname], [street], [city],
[phone], [mobile], [fax], [email], [companyName],
[licence], [brn], [vat], [companyStreet], [companyCity], [status]
FROM
@registerDetails) AS foo
WHERE
rownumber = @I
-- this stored procedure handles the saving of the current customer row just defined above
-- if there is any error from that sproc, it will jump to CATCH block
--save the error message in the temp table and continue
--with the next customer row in the while loop.
exec dbo.sp_SaveCustomer @customerRow
END TRY
BEGIN CATCH
IF @@TranCount = 0
-- Transaction started in procedure.
-- Roll back complete transaction.
ROLLBACK TRANSACTION;
if XACT_STATE()= -1 rollback transaction
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE() + ' ' + (select firstname from @registerDetails where id=@I)
SELECT @ErrorSeverity = ERROR_SEVERITY();
SELECT @ErrorState = ERROR_STATE()
INSERT INTO #registrationResults (error,id)
SELECT @ErrorMessage, @I
END CATCH
set @I = @I +1
end
COMMIT TRANSACTION registerTran
select * from #registrationResults
上記は、vb.net コードで次のように呼び出すと、1 つのストアド プロシージャで機能します。
ta.Fill(registrationErrors, clientDetailsDT)
whereregistrationErrors
とclientDetailsDT
は厳密に型指定されたデータ テーブルです。
これはしません:
begin catch
IF @@TranCount > 0 or XACT_STATE()= -1 ROLLBACK TRANSACTION;
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
DECLARE @ErrorLine INT;
SELECT @ErrorMessage = ERROR_MESSAGE();
SELECT @ErrorSeverity = ERROR_SEVERITY();
SELECT @ErrorState = ERROR_STATE();
SELECT @ErrorLine = ERROR_Line();
****ERROR -- THIS SELECT WAS MESSING ALL UP as it was this select that was being returned to the .NET and not the select of the desired #temp table after, hence returning 0 resultset as this select was EMPTY. !!
select status_indicator from InsertInvoiceTriggerData where session_GUID = guid**
delete from InsertInvoiceTriggerData where session_GUID = @guid**
INSERT INTO #registrationResults (error,id)
SELECT @ErrorMessage, NULL
select * from #registrationResults
end catch
結果セットを返す方法について何か提案はありますか?