次のストアド プロシージャpr_generate_sales_orderは、データベース DB1 の 2 つのテーブル SO Header と SO Detail にレコードを生成します。次に、SO ヘッダーと詳細レコードを DB1 から DB2 にエクスポートします。
ASP.Net/C# からこの SP を呼び出そうとするたびに、次のエラー メッセージが生成されます 。'c_customers' という名前のカーソルが存在しないため、データベースにレコードが作成されません。
ただし、このストアド プロシージャpr_generate_sales_orderを SSMS で直接実行すると、正常に動作し、DB1 と DB2 にレコードが生成されます。
問題はどこですか?ビギントライキャッチと関係あるのか…???
USE [DB1]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ------------------------------------------------------------------------------------
-- Name: pr_generate_sales_order
-- ------------------------------------------------------------------------------------
-- Parameters:
-- - N/A
--
-- Description:
-- Generate Sales Order in DB1 & DB2
-- ------------------------------------------------------------------------------------
alter procedure [dbo].[pr_generate_sales_order] () as
Begin
-- --------------------------------------------------------------------------------
-- Variable declaration
-- --------------------------------------------------------------------------------
Declare @v_error_number nvarchar(10)
Declare @v_error_message_en nvarchar(1000)
Declare @v_customer_id int
Declare @v_time_log_id int
Declare @v_hourly_rate numeric(18, 5)
Declare @v_total_time numeric(18, 5)
Declare @v_description nvarchar(max)
Declare @tmp_time_log Table
(
time_log_id int,
customer_id int,
sku_id int,
hourly_rate numeric(18, 5),
hours int,
minutes int
)
-- --------------------------------------------------------------------------------
-- Main program
-- --------------------------------------------------------------------------------
Set @v_error_number = '0'
Set @v_error_message_en = ''
Begin Try
Begin Transaction T1
-- ------------------------------------------------------------------------
--
-- ------------------------------------------------------------------------
Insert Into @tmp_time_log
Select tl.time_log_id as time_log_id
, tl.customer_id as customer_id
, tl.sku_id as sku_id -- 24/09/2013
, tl.hourly_rate as hourly_rate
, tl.hours_rounded as hours_rounded
, tl.minutes_rounded as minutes_rounded
From time_log tl
Where tl.transaction_date between '2013-01-06' and getdate()
-- ------------------------------------------------------------------------
-- Open Cursor
-- ------------------------------------------------------------------------
-- ------------------------------------------------------------------------
-- Declare Customer Cursor: Get the list of Customer
-- ------------------------------------------------------------------------
Declare c_customers cursor for
Select distinct IsNull(customer_id, 0)
From @tmp_time_log
Open c_customers
Fetch Next From c_customers Into @v_customer_id
While @@fetch_status = 0
Begin
-- ----------------------------------------------------------------
-- Create Sales Order Header for each Customer
-- ----------------------------------------------------------------
Insert Into sales_order_header ...
-- Get Sales Order Header Id
Set @v_sales_order_header_id = scope_identity()
-- ----------------------------------------------------------------
-- Declare Time Log Cursor: Get the list of Time Log by customer in order to Create
-- ----------------------------------------------------------------
Declare c_time_log cursor for
Select time_log_id as time_log_id
, hourly_rate as hourly_rate
, cast( (cast(tl.hours as numeric(18, 5)) + (cast(tl.minutes as numeric(18, 5)) / 60) ) as numeric(18, 5))
as total_time
, sku_id as sku_id
From @tmp_time_log tl
Where customer_id = @v_customer_id
-- ----------------------------------------------------------------
-- Open Cursor
-- ----------------------------------------------------------------
Open c_time_log
Fetch Next From c_time_log
Into @v_time_log_id, @v_hourly_rate, @v_total_time, @v_sku_id
While @@fetch_status = 0
Begin
-- Create Sales Order Detail for Each SO Header
Insert Into sales_order_detail ....
-- Get the next Cursor
Fetch Next From c_time_log Into @v_time_log_id, @v_hourly_rate, @v_total_time, @v_sku_id
End
-- Close & Deallocate Cursor
Close c_time_log
Deallocate c_time_log
-- Get the next Cursor
Fetch Next From c_customers Into @v_customer_id
End
-- Close & Deallocate Cursor
Close c_customers
Deallocate c_customers
-- ------------------------------------------------------------------------
-- Export Sales Order Header & Detail From DB1 to DB2
-- ------------------------------------------------------------------------
Exec [DB2].[dbo].[pr_import_sales_order_from_db1]
--
Commit Transaction T1
End Try
Begin Catch
-- If Error Found Then Rollback Transaction
If @@TRANCOUNT > 0
Begin
-- Close & Deallocate Cursor
Close c_customers
Deallocate c_customers
-- Rollback
Rollback Transaction T1
End
-- Get Error # & Error Message
Select @v_error_number = ERROR_NUMBER()
, @v_error_message_en = 'An error occurred in ' + ERROR_PROCEDURE() + ': ' + ERROR_MESSAGE()
GOTO Step_END
End Catch
-- ------------------------------------------------------------------------------------------
-- Return: Error # & Error message
-- ------------------------------------------------------------------------------------------
STEP_END:
Select @v_error_number as error_number
, @v_error_message_en as error_message_en
End
GO