Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value 'Noida' to data type int. Expand / Collapse
Author
Message
Posted Saturday, March 16, 2013 11:24 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, July 24, 2014 12:29 PM
Points: 54, Visits: 101
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
Post #1431924
Posted Saturday, March 16, 2013 2:19 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:38 PM
Points: 7,097, Visits: 12,598
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
Post #1431943
Posted Sunday, March 17, 2013 4:09 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, July 24, 2014 12:29 PM
Points: 54, Visits: 101
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?
Post #1431968
Posted Sunday, March 17, 2013 5:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:38 PM
Points: 7,097, Visits: 12,598
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
Post #1431973
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse