Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:45:19+00:00 2026-05-31T12:45:19+00:00

I am writing a function that would take table-name as argument. I know the

  • 0

I am writing a function that would take table-name as argument.
I know the column name I need out of the table, but the table name + table owner is dynamically passed in (well I need that to be the case, but it is currently not working).

It kind of looks like this:

CREATE FUNCTION [myowner].  
[alex_diff](@fromdate datetime, @todate datetime, @table_name varchar(256), @country_code varchar(3))  
RETURNS int  
AS  
BEGIN
...
set @date_string = (select calendar_month2 from ??? where calendar_year=@current_year and country_code = @country_code)
...
END

I couldn’t put @table_name in place of ‘???’
That gave me error:

Msg 1087, Level 16, State 1, Procedure alex_business_date_diff, Line
53 Must declare the table variable “@table_name”.

What can I do to execute SQL on the dynamically passed table name?

I have tried doing the following:

    set @statement = 'select @output=' + @column_name + ' from ' + @table_name + ' where calendar_year=' + cast(@current_year as varchar(4)) + ' and country_code = ' + @country_code;
    EXEC sp_executesql @statement, N'@output varchar(31) OUTPUT', @date_string OUTPUT

But get this error (when I try to run the function):

Msg 557, Level 16, State 2, Line 1 Only functions and some extended
stored procedures can be executed from within a function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:45:21+00:00Added an answer on May 31, 2026 at 12:45 pm

    You could use CLR in SQL Server and take advantage of the .NET language which would run dynamic sql.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a PHP function that would use a table of sorts to look
How would i go about writing a function that would handle the data in
I'm writing a Javascript function that would manipulate an array written on-the-fly and sent
I'm writing a function that fishes out the src from the first image tag
I am writing a function that will take a parameter and, among other things,
I'm writing a function that replaces long hex coded color ( #334455 ) with
Suppose I'm writing a function that takes a list of integers and returns only
I am writing a function that takes in a output target file and a
I am writing a function that takes a string, string pointer and an int.
I find that I frequently end up writing a function that I always call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.