Can I use dynamic SQL in a function for a COLUMN NAME

I am trying to pass a column name and value to a SQL function so that I can call the function and get the results I need.  So far I have been unsuccessful.

The call would be something like
SELECT * FROM myfunction('THISCOLUMN','0082')

I've tried a couple of ways without success like

CREATE FUNCTION [dbo].[myfunction] (@psdcolumn varchar(10), @vall varchar(5))
RETURNS @RESULT TABLE
(
VALUE1 VARCHAR(50),
ACTUAL_HOURS numeric(11, 0)
)
-- there is code that does all this prep work, but the result is something like these values

SET @hmsdate = '20130731'
SET @newPrj = '1'

-- The last part with the "AND @psdcolumn=thisvalue" is where I'm stuck
SET @actualHours = (SELECT SUM(PSD_MIN)/60 FROM PSLINES LEFT JOIN PSDETAIL ON PSD_PSL=PSL_KEY WHERE PSL_PRJ=@newPrj AND PSD_DATE <= @hmsdate AND @psdcolumn=thisvalue)

INSERT INTO @RESULT VALUES (@val1,  @actualHours)

-- My goal is to return a TABLE so I thought the function would be the easiest but I am open to other suggestions if this is the wrong way to go at it.
hmstechsupportAsked:
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.

Surendra NathTechnology LeadCommented:
no you cannot use the dynamic sql inside a function it is not allowed in sql server
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
Surendra NathTechnology LeadCommented:
you can actually create a stored procedure with the same input and output and get the desired results without much change of code, except your execution in the outer query changes from

select * from <my functionction>()
to
exec <my stored proc> param1, param2....
0
hmstechsupportAuthor Commented:
K. I did read that I could not use dynamic SQL but had hoped that was incorrect.  Can you point me to an example of a stored procedure returning a table?
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

PortletPaulfreelancerCommented:
how many columns are there in PSLINES and PSDETAIL?
of these how many would you actually use to filer this information?
are there any column names that are common (that would need [table].[field] syntax)?

& aren't you calculating a single value, or is there more to this?
-- The last part with the "AND @psdcolumn=thisvalue" is where I'm stuck
SET @actualHours = (
        SELECT
                SUM(PSD_MIN) / 60
        FROM PSLINES
        LEFT JOIN PSDETAIL
                ON PSD_PSL = PSL_KEY
        WHERE PSL_PRJ = @newPrj
                AND PSD_DATE <= @hmsdate
                AND @psdcolumn = thisvalue
)

Open in new window

0
hmstechsupportAuthor Commented:
Much more and it would have to be a table of rows to return.  The example I gave was just to simplify the issue.
0
hmstechsupportAuthor Commented:
Thanks for the confirmation.
0
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

From novice to tech pro — start learning today.