Wednesday, 10 March 2010

Error and Troubleshoot

Error and Troubleshoot in SQL server 2005

-- Errors and Error methods
BEGIN TRY
-- SELECT 1/0--Evergreen divide by zero example!
select FirstName/100 from customer
END TRY

BEGIN CATCH
SELECT 'There was an error! ' + ERROR_MESSAGE()
SELECT ERROR_NUMBER()
SELECT ERROR_SEVERITY()
SELECT ERROR_STATE()
SELECT ERROR_PROCEDURE()
SELECT ERROR_LINE()
SELECT ERROR_MESSAGE()
RETURN
END CATCH;



-- Error Query

select distinct e.department_id + ' - ' +d.department_name as [Department Name]
from employee e
left outer join Department as d on d.department_id=e.department_id

/*-- Error Message
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to bigint.
*/

-- Correct Query
select distinct
CAST(e.department_id AS NVARCHAR) + ' - ' +d.department_name as [Department Name]
from employee e
left outer join Department as d on d.department_id=e.department_id







_______________________________________________________________

No comments:

Post a Comment