我知道这个问题可能会重演。我检查了我的查询超过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
获取错误:
Msg 245,Level 16,State 1,Procedure SPApprove_CRM_Doctor_Request, 第19行 转换nvarchar值'INSERT时转换失败 到dbo。[Financial_Assistance](CRM_Doctor_Request_Id,Is_Active, Is_Deleted,Is_Cancelled,Created_Date)VALUES('to data type int。
答案 0 :(得分:1)
您应该将@C_Id
转换为varchar,以便将其连接到字符串。
SET @sql = 'INSERT into dbo.' + quotename(@Service_Table) +
'(Request_Id, Is_Active, Is_Deleted, Is_Cancelled, Created_Date)
VALUES (' + convert(varchar,@C_Id) + ',1, 0, 0,'''+ convert(VARCHAR,@Today_Date) +''')'
答案 1 :(得分:0)
此外,您可以将sp_executesql系统存储过程与参数
一起使用SET @sql = 'INSERT into dbo.' + quotename(@Service_Table) + '(Request_Id, Is_Active, Is_Deleted, Is_Cancelled, Created_Date) VALUES (@C_Id,1, 0, 0,@Today_Date)'
EXEC sp_executesql @sql, N'@C_Id int, @Today_Date datetime', @C_Id, @Today_Date