Friday, February 24, 2012

Custom Code and SQL

Can I use a custom code funtion in my SQL select? If so, how?

If by custom code function you mean user defined function then here is a sample.

create function MyFunc( @.inPara1 int,
@.inPara2 varchar(100))
Returns @.outResult Table(col1 int, col2 varchar)
AS
BEGIN
INSERT INTO @.outResult
SELECT age,ename FROM TABLENAME where some_code = @.inPara1 AND dept = @.inPara2

RETURN
END
GO

This is how you execute a user defined function

select * from MyFunc(123,'Marketing')

No comments:

Post a Comment