我知道这个问题可能会重复。我检查了我的查询超过 2 次,但我不知道是什么问题?
这是我的带有游标的存储过程:
ALTER PROCEDURE [dbo].[SPApprove_Request](@Id int, @NumRowsChanged int OUTPUT)
AS
BEGIN TRAN ApprovalTransaction
DECLARE @Mapping_Id int, @C_Id int, @Service_Table varchar(50), @Ser_Id int, @sql nvarchar(4000), @Today_Date datetime
SET @Today_Date=GETDATE()
DECLARE cursorObj CURSOR LOCAL static FOR SELECT serType.Service_Table_Name, serMap.Id, serMap.Request_Id FROM [Service_Type] as serType, [Service_Mapping] as serMap WHERE serType.Id = serMap.Service_Type_Id AND serType.Id IN (SELECT Service_Type_Id FROM Service_Mapping WHERE Request_Id = @Id) and serMap.Request_Id=@Id
OPEN cursorObj
--SELECT @@CURSOR_ROWS AS TotalRows
FETCH NEXT FROM cursorObj into @Service_Table, @Mapping_Id, @C_Id
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'INSERT into dbo.' + quotename(@Service_Table) + '(Request_Id, Is_Active, Is_Deleted, Is_Cancelled, Created_Date) VALUES (' + @C_Id + ',1, 0, 0,'''+ convert(VARCHAR,@Today_Date) +''')'
EXEC sp_executesql @sql
SELECT @Ser_Id = @@IDENTITY
IF (@@ERROR <> 0) GOTO ERR_HANDLER
UPDATE [Service_Mapping] SET Service_Id = @Ser_Id WHERE Id = @Mapping_Id
FETCH NEXT FROM cursorObj into @Service_Table, @Mapping_Id, @C_Id
END
CLOSE cursorObj
DEALLOCATE cursorObj
UPDATE [Request] SET State_Id = 2, Service_State_Id = 1, Approve_Disapprove_Date = @Today_Date, Date_Modified = @Today_Date WHERE Id = @Id
SELECT @NumRowsChanged = @@ROWCOUNT
COMMIT TRAN ApprovalTransaction
RETURN @NumRowsChanged
ERR_HANDLER:
PRINT 'Unexpected error occurred while Approving CRM Request!'
ROLLBACK TRAN ApprovalTransaction
得到错误:
消息 245,级别 16,状态 1,过程 SPApprove_CRM_Doctor_Request,第 19 行将
nvarchar 值 'INSERT 转换为 dbo 时转换失败。[Financial_Assistance](CRM_Doctor_Request_Id, Is_Active, Is_Deleted, Is_Cancelled, Created_Date)