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 7610317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:23:22+00:00 2026-05-31T01:23:22+00:00

I have a function which executes a select statement. When I attempt to execute

  • 0

I have a function which executes a select statement. When I attempt to execute this select statement which returns a single value and sets it to a variable my function crashes with the following error:

Msg 203, Level 16, State 2, Procedure Info_GetWholeNumber, Line 20
The name ‘SELECT MAX(LEN(CAST(FLOOR([pcom_audit_cant_con]) AS
VARCHAR(38)))) AS WHOLE_NO FROM Auditorias.prod_com_audit’ is not a
valid identifier.

If I copy and paste that select statement it works fine… therefore it’s not the select it has something to do with my EXEC… anyways here’s my functions code…

ALTER FUNCTION [dbo].[Info_GetWholeNumber]
(
    @TABLE VARCHAR(MAX)
    , @COLUMN VARCHAR(MAX)
)
RETURNS INT
AS
BEGIN
    -- Declare the return variable here
    DECLARE @WHOLE_NO INT

    -- Add the T-SQL statements to compute the return value here
    DECLARE @SQL VARCHAR(MAX)
    SET @SQL = 'SELECT MAX(LEN(CAST(FLOOR([' + @COLUMN + ']) AS VARCHAR(38))))
      AS WHOLE_NO FROM ' + @TABLE
    EXEC @WHOLE_NO = @SQL

    -- Return the result of the function
    RETURN @WHOLE_NO

END

If anyone has any ideas how I can fix this I’d really appreciate the help! Thanks in advance!

UPDATES:

OK so im attempting to use the sp_executesql function as stated and i am going to paste the new function.

ALTER FUNCTION [dbo].[Info_GetWholeNumber]
(
@TABLE VARCHAR(MAX)
, @COLUMN VARCHAR(MAX)
)
RETURNS INT
AS
BEGIN
DECLARE @WHOLE_NO INT
DECLARE @SQL NVARCHAR(MAX)
DECLARE @PARAMS NVARCHAR(MAX)

SET @SQL = N'SELECT @WHOLE_NOOUT = MAX(LEN(CAST(FLOOR(@COL) AS VARCHAR(38)))) FROM @TBL'
SET @PARAMS = N'@COL VARCHAR(MAX), @TBL VARCHAR(MAX), @WHOLE_NOOUT INT OUTPUT'

EXECUTE sp_executesql @SQL, @PARAMS, @COL = @COLUMN, @TBL = @TABLE, @WHOLE_NOOUT = @WHOLE_NO OUTPUT;

-- Return the result of the function
RETURN @WHOLE_NO

END

I am now getting this error:

Msg 1087, Level 16, State 1, Line 1
Must declare the table variable “@TBL”

Which in my opinion seems correct since the variables arent declared other than in the @PARAM variable… Am I missing something here?

  • 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-31T01:23:24+00:00Added an answer on May 31, 2026 at 1:23 am

    You can neither assign directly from an EXEC statement, nor can you directly assign a variable from inside an EXECd dynamic SQL (it’s out of scope). This also shouldn’t really be called from a function as SQL expects functions to be deterministic and this really isn’t (In fact even if you fix your error, it still probably won’t work.)

    Assuming you change how you’re calling this code you’ll need to know how to get your value from the dynamic sql. If you want to pass a variable from a dynamic SQL string, you must use EXEC SP_ExecuteSQL with an output parameter.

    Here is a simple example:

    DECLARE @whole_no int;
    DECLARE @SQL nvarchar(500);
    DECLARE @Parms nvarchar(500);;
    
    SET @SQL = N'SELECT @whole_noOUT = 1';
    SET @Parms = N'@whole_noOUT int OUTPUT';
    
    EXECUTE sp_executesql @SQL, @Parms, @whole_noOUT=@whole_no OUTPUT;
    SELECT @WHOLE_NO;
    

    In this example, we’re setting a param definition, and an output parameter. In general, sp_executesql is the preferred method for calling dynamic sql since it can be parameterized which both increases security by avoiding the need to concatenating sql directly, but also allows plan reuse when your sql is fully parameterized.

    I should also add the obligatory warning that your really shouldn’t be building dynamic sql in such a cavalier manner as you open yourself up to SQL injection, either now or in the future. I understand that you cannot parameterize your particular query but, at the very least, you should be using QUOTENAME() on your column name instead of hardcoding the [].

    Read more on sp_executesql here: http://msdn.microsoft.com/en-us/library/ms188001.aspx
    And more on QUOTENAME() here: http://msdn.microsoft.com/en-us/library/ms176114.aspx

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

Sidebar

Related Questions

I have a VB.Net function which executes a Oracle Stored Proc and returns a
I have this formula in a cell: =GetData(Channel_01,Chicago) Which executes this code: Public Function
I have created a JS function which executes fine when it's included an the
I have an application in VC++ which needs to execute a function provided to
I have a function which searches an STL container then returns the iterator when
I have a very simple stored procedure which returns multiple record sets. All of
I have the following loop inside an function init(); which is executed onload, I
I have a function which parses one string into two strings. In C# I
I have an function which decodes the encoded base64 data in binary data but
I have a function which gets a key from the user and generates a

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.