Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value 'Noida' to data type int.

  • CREATE TABLE EMPLOYEE

    (

    EMP_ID INT PRIMARY KEY,

    FIRSTNAME nvarchar(100),

    LASTNAME nvarchar(100),

    Salary INT,

    CITY nvarchar(100)

    )

    insert INTO EMPLOYEE VALUES(2,MonuRathor,4789.00,Agra);

    insert INTO EMPLOYEE VALUES(4,RahulSaxena,5567.00,London);

    insert INTO EMPLOYEE VALUES(5,Prabhat Kumar,4467.00,Bombay);

    BEGIN TRANSACTION

    Declare @b-2 varchar(100)

    SET @b-2 = 'Nodia';

    Declare @D varchar(100)

    SET @D = 'Delhi'

    Update employee

    SET city =(

    Case

    WHEN city = 'Agra' THEN @b-2

    WHEN city = 'London' THEN @D

    ELSE 0

    END)

    END

    The error faced was as present in the subject. Please help

    I also tried the below way,

    BEGIN TRANSACTION

    Update employee

    SET city =(

    Case

    WHEN city = 'Agra' THEN 'Delhi'

    WHEN city = 'London' THEN 'Noida'

    ELSE 0

    END)

    END

    BUT I CAME ACCROSS:

    Msg 102, Level 15, State 1, Line 9

    Incorrect syntax near 'END'.

    Please HELP

  • Two definite issues:

    1. You have to watch your data types. In your CASE expression your default case outputs a 0, which will default to an INT in SQL Server. Try putting the 0 in single quotes so SQL Server views it as a string-literal.

    2. The ending command for an explicit transaction is not END, it is COMMIT TRANSACTION.

    BEGIN TRANSACTION;

    Declare @b-2 varchar(100);

    SET @b-2 = 'Nodia';

    Declare @D varchar(100);

    SET @D = 'Delhi';

    Update employee

    SET city =(Case

    WHEN city = 'Agra' THEN @b-2

    WHEN city = 'London' THEN @D

    ELSE '0'

    END);

    COMMIT TRANSACTION;

    One potential issue:

    1. Do you need a WHERE-clause? Your update statement will update all rows in the table. Are you sure that is what you want?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Thanks A Lot!!!!:-):-)

    That worked perfectly. Yes were are only two records in the table as of now.

    Where i wanted to swap the city details to some desired.

    Well do u meant to use 'Where' clause for tuning/Performance? Faster transactions?

  • kiran.vaichalkar (3/17/2013)


    Thanks A Lot!!!!:-):-)

    You're welcome 🙂 Thank you for the feedback.

    Just one more note about the use of an explicit transaction. By default SQL Server will auto-commit statements unless the database option IMPLICIT_TRANSACTIONS is ON or if the statements appear within an explicit transaction block, i.e. after a BEGIN TRANSACTION; and before a COMMIT TRANSACTION; statement as you have shown. What this means for you is that if all you need to do is issue the UPDATE statement then you do not need to open an explicit transaction, i.e. you could delete the BEGIN TRANSACTION; and COMMIT TRANSACTION statements from your batch and it would function exactly the same way.

    Well do u meant to use 'Where' clause for tuning/Performance? Faster transactions?

    Performance yes, and possibly correctness. The update statement you showed did not appear to be something one would typically issue in a production scenario and leaving off a WHERE-clause is a mistake that is sometimes very hard to recover from. Ideally you want to focus on limiting the number of rows the database engine would need to evaluate in order to get the correct result, and only ask the engine to do as little work as necessary. In a production scenario where you might have hundreds or thousands of queries needing to run per second every little bit of performance advantage helps.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply