Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

passing parameters to table valued functions and using parameters as column name on select

$
0
0

I am creating a function where I want to pass it parameters and then use those parameters in a select statement. When I do that it selects the variable name as a literal not a column. How do I switch that context.

Query:

ALTER FUNCTION [dbo].[ufn_Banner_Orion_Employee_Comparison_parser_v2]

    ( 
@BANNER_COLUMN AS VARCHAR(MAX),
@ORION_COLUMN AS VARCHAR(MAX)


RETURNS @Banner_Orion_Employee_Comparison TABLE 

    (
LAST_NAME nvarchar(max),
EMPNO int,
BannerColumnName nvarchar(max),
BANNER nvarchar(max),
ORION nvarchar(max)
)


AS

BEGIN



INSERT INTO @Banner_Orion_Employee_Comparison

(LAST_NAME, BANNER, ORION)

SELECT

a.LAST_NAME, @BANNER_COLUMN, @ORION_COLUMN

FROM OPENQUERY(ORCLPROD_APDORACLE, 'select LAST_NAME, BANNER_RANK, BADGE, EMP_STATUS from XTRACT_VIEW') AS a
inner join IWM_Stage.dbo.ViewPersonnel AS b
on a.BADGE = b.badge
WHERE a.EMP_STATUS = 'A'
and a.BANNER_RANK <> b.[rank]

RETURN;

END;


GO

-----------------------

Output

I execute this:

select * from ufn_Banner_Orion_Employee_Comparison_parser_v2 ('a.BANNER_RANK' , 'b.[rank]')

and get:

CerecerezNULLNULLa.BANNER_RANKb.[rank]

      

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>