简体   繁体   中英

stored procedure sql server 2008

I am working on a project and am using stored procedure. I'm getting this error:

Line: 939
Error: Sys.WebForms.PageRequestManagerServerErrorException: Invalid object name 'IT_Assets'.
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.

Please find below my stored procedure code:

alter PROCEDURE [ITAssets_sp_IT_Assets]
-- Add the parameters for the stored procedure here

    (@Mode varchar(12)='ADD',
    @ID integer , @AssetCode nvarchar(20)=null, @Description nvarchar(70)=null,
    @Site nvarchar(10)=null)
AS
Begin

IF @Mode='ADD'
    Begin
Begin Tran
  INSERT INTO [IT_Assets]
      ([ID]
            ,[AssetCode]
            ,[Description]
            ,[Site])
                values
(@ID, @AssetCode, @Description, @Site
)
 If @@ERROR <> 0  
        ROLLBACK TRAN 
    Else
        COMMIT TRAN

Select  @ID
End
ELSE 
Begin
    Begin Tran
            UPDATE [IT_Assets]
                SET 
AssetCode = @AssetCode, Description = @Description, Site = @Site 
WHERE ID = @ID      
If @@ERROR <> 0  
        ROLLBACK TRAN 
    Else
        COMMIT TRAN
    Select  @ID 
End
End

I didn't understand the error and I don't know exactly where is the problem? Would someone please help me in sloving this problem?

From the error Invalid object name 'IT_Assets' I believe that the table/view 'IT_Assets' is present in a diferent database than the stored procedure (assuming that the object exist and you are using the correct name).

Then you need to fully quaify it wih Db name like

UPDATE [DB_NAME].[dbo].[IT_Assets]  (assuming `dbo` is the owner)

Try using your database name at top of procedure using use statement like

use [DB_NAME] 
GO
alter PROCEDURE [ITAssets_sp_IT_Assets]
-- Add the parameters for the stored procedure here

    (@Mode varchar(12)='ADD',
 ...

Also change you transaction handling using TRY .. CATCH consruct like below

Begin Tran
BEGIN TRY
  INSERT INTO [IT_Assets]
      ([ID]
            ,[AssetCode]
            ,[Description]
            ,[Site])
                values
(@ID, @AssetCode, @Description, @Site);
        COMMIT TRAN;
END TRY
BEGIN CATCH
        ROLLBACK TRAN ;
END CATCH

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM