简体   繁体   中英

Update stored procedure - SQL Server 2012

I have an error and I cannot find the reason for failing of this procedure. All I know it does something I have no clue about. Does anyone have an idea what is this about? :|

ALTER PROCEDURE [dbo].[updateAccountPersonJob] 
    @department nvarchar(50),
    @description nvarchar(50),
    @personID int,
    @name nvarchar(50),
    @surname nvarchar(50),
    @email nvarchar(50),
    @username nvarchar(50),
    @password nvarchar(50),
    @status nvarchar(50)
AS
BEGIN TRANSACTION 
    DECLARE @missionID int, @jobID int;
    BEGIN TRY

       SET @jobID = (SELECT id FROM Jobs 
                     WHERE department = @department AND description = @description)

       UPDATE Persons 
       SET name = @name, surname = @surname, email = @email, jobID = @jobID 
       WHERE Persons.id = @personID

       UPDATE Accounts 
       SET Username = @username, Password = @password, Status = @status  
       WHERE Accounts.Password = @personID
    END TRY
    BEGIN CATCH
        SELECT 
            ERROR_NUMBER() AS ErrorNumber
            ,ERROR_SEVERITY() AS ErrorSeverity
            ,ERROR_STATE() AS ErrorState
            ,ERROR_PROCEDURE() AS ErrorProcedure
            ,ERROR_LINE() AS ErrorLine
            ,ERROR_MESSAGE() AS ErrorMessage;

        BEGIN IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION;
            PRINT('ROLLBACK')
        END
    END CATCH;

    IF @@TRANCOUNT > 0
        COMMIT TRANSACTION;

EXEC:

EXEC [dbo].updateAccountPersonJob
        @department = N'Facultatea de Matematica si Informatica',
        @description = N'Teacher',
        @personID = N'16',
        @name = N'Dummy',
        @surname = N'BarFOO',
        @email = N'bar@ba.com',
        @username = N'dummy',
        @password = N'd',
        @status = N'Teacher'

Error:

Conversion failed when converting the nvarchar value 'director' to data type int.

you are checking personid (integer) column against password column which is nvarchar. Update you query to use correct parameter or column and your code should work

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