This question can easily take several paths, so I will first hit a more specific path. While working with SQL Server 2005, I am trying to create a scalar function that acts like "TryCast" from varchar to int. Where I ran into a problem when I add a TRY block to a function;
CREATE FUNCTION u_TryCastInt
(
@Value as VARCHAR(MAX)
)
RETURNS Int
AS
BEGIN
DECLARE @Output AS Int
BEGIN TRY
SET @Output = CONVERT(Int, @Value)
END TRY
BEGIN CATCH
SET @Output = 0
END CATCH
RETURN @Output
END
It turns out that with this statement everything is wrong, including "Invalid use of side effect or time-dependent statement in" BEGIN TRY "inside a function" and "Invalid use of side effect or time-dependent statement in" END TRY "inside a function." I can't seem to find examples of using try statements in a scalar function that made me think if error handling in the function is possible?
, Convert Cast, SELECT depsite. , :
CREATE TABLE tblTest
(
f1 VARCHAR(50)
)
GO
INSERT INTO tblTest(f1) VALUES('1')
INSERT INTO tblTest(f1) VALUES('2')
INSERT INTO tblTest(f1) VALUES('3')
INSERT INTO tblTest(f1) VALUES('f')
INSERT INTO tblTest(f1) VALUES('5')
INSERT INTO tblTest(f1) VALUES('1.1')
SELECT CONVERT(int,f1) AS f1_num FROM tblTest
DROP TABLE tblTest
, 'f' . - :
SELECT u_TryCastInt(f1) AS f1_num FROM tblTest
fi_num
__________
1
2
3
0
5
0
? -, ? , SQL Server 2000, Try.