SQL 2008 R2 UDF with OpenQuery that uses parameters

Environment:
      MS SQL 2008 R2 Std.
      Linked Server to AS400 Db2

I have a Stored Procedure that does what I need and would like the equivalent User Defined Function.
My objective is to return a single value from a table within the Linked Server using a “WHERE” clause containing two criteria based on variables passed into the call.
I’ve been all over the WEB looking for a solution and believe that there is none based on the constraints inherent with User Defined Functions BUT……..
I thought I’d ask here none the less !
Thanks in advance..!
Here’s the Stored Procedure syntax that I would like an equivalent User Defined Function for :

USE [MyDb]
GO

/****** Object:  StoredProcedure [dbo].[sp_GetPartRev]    Script Date: 08/28/2014 12:34:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:            <Author,,Name>
-- Create date: <Create Date, ,>
-- Description:      <Description, ,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_GetPartRev] @CoNo varchar(3)
                                      ,@ItemNumber varchar(15)
                                      ,@PartRev char(1) OUTPUT

AS
BEGIN

    declare @Command Nvarchar(MAX);
    declare @tick char(1);

    set @tick = '''';

    set @Command = N'SELECT @revOUT = IMPTRV FROM OpenQuery(IBMDA_SQL, '
                 + @tick
                 + 'SELECT PARTREV FROM LIB01.ITEMMSTR WHERE CoNo = '
                 + @CoNo
                 + ' AND ItemNumber = '
                 + @tick
                 + @tick
                 + @ItemNumber
                 + @tick
                 + @tick
                 + @tick
                 + ')';

    execute sp_executesql @Command, N'@revOUT char(1) OUTPUT', @revOUT = @PartRev OUTPUT;
END

GO
LVL 2
Swamp_ThingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy Knight, MCMPrincipal ConsultantCommented:
You shouldn't use a UDF for this anyway.  If you do, you will turn anything you use it in into a loop because it has to execute for each value.
0
Aneesh RetnakaranDatabase AdministratorCommented:
You cant use dynamic sql within UDF
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.