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

  • Home
  • SEARCH
  • 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 988953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:42:50+00:00 2026-05-16T05:42:50+00:00

I am trying to create some dynamic DDL to build a function and when

  • 0

I am trying to create some dynamic DDL to build a function and when I run it, it keeps giving me an error. I am not sure what I am doing wrong with the format….I have tried a lot of different things and now it is just out of curiousity that I want to understand how to get it to work. Any input is greatly appreciated.

CODE:

DECLARE @SQL nvarchar(max) = 
        'ALTER FUNCTION dbo.GetFiscalDate() 
            RETURNS DATETIME 
         AS 
         BEGIN 
            DECLARE @RESULT DATETIME
            SELECT @RESULT = @FY
            RETURN @RESULT;
         END'
   ,@FY datetime = '01/01/2016'
   ,@ParamDef nvarchar(50) = N'@FY datetime'
exec sp_executesql @SQL,@ParamDef,@FY

Gives me this error:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'FUNCTION'.
Msg 178, Level 15, State 1, Line 7
A RETURN statement with a return value cannot be used in this context.

This Code however…works:

DECLARE
 @FY nvarchar(10) = '01/01/2015'
,@SQL nvarchar(max) 

Select @SQL = 
        'ALTER FUNCTION dbo.GetFiscalDate() 
            RETURNS DATETIME 
         AS 
         BEGIN 
            DECLARE @RESULT DATETIME
            SELECT @RESULT = ' + @FY + '
            RETURN @RESULT;
         END'
exec sp_executesql @SQL

What am I missing with this when I want to pass in params instead of concatenating them with the statement?

As usual I greatly appreciate all input.

Thanks,

S

  • 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-16T05:42:51+00:00Added an answer on May 16, 2026 at 5:42 am

    parameters are used in execution plans so that you can reuse the execution plan, not in DDL statements, use your 2nd approach

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

Sidebar

Related Questions

I'm trying to create some dynamic code within clojure. In the function below, the
I am trying to create some dynamic html out of some data from db.
I have been trying to create some dynamic Xaml. I have the following c#
I'm having some trouble trying to understand how to create a dynamic choice field
I'm trying to create dynamic balls by using a for loop. For some reason
I'm trying to create an associative array with dynamic data, and having some trouble.
I am trying to create a dynamic website, I have a database with some
I'm trying to create some dynamic scrollbars for a project I'm working on in
I am trying to create some dynamic sql using the following code block firstSqlStatement
I am trying to create a screen with some dynamic controls, well radio buttons

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.