Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
kiran.vaichalkar
kiran.vaichalkar
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 127
CREATE TABLE EMPLOYEE
(
EMP_ID INT PRIMARY KEY,
FIRSTNAME nvarchar(100),
LASTNAME nvarchar(100),
Salary INT,
CITY nvarchar(100)
)

insert INTO EMPLOYEE VALUES(2,   Monu   Rathor,   4789.00,   Agra);
insert INTO EMPLOYEE VALUES(4,   Rahul   Saxena,   5567.00,   London);
insert INTO EMPLOYEE VALUES(5,   Prabhat Kumar,   4467.00,   Bombay);

BEGIN TRANSACTION
Declare @B varchar(100)
SET @B = 'Nodia';
Declare @D varchar(100)
SET @D = 'Delhi'
Update employee
SET city =(
Case
WHEN city = 'Agra' THEN @B
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
Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8219 Visits: 14368
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 varchar(100);
SET @B = 'Nodia';
Declare @D varchar(100);
SET @D = 'Delhi';
Update employee
SET city =(Case
WHEN city = 'Agra' THEN @B
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
kiran.vaichalkar
kiran.vaichalkar
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 127
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?
Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8219 Visits: 14368
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search