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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:51:55+00:00 2026-05-14T19:51:55+00:00

I’m trying to generate scripts from a database, but with slight modifications to those

  • 0

I’m trying to generate scripts from a database, but with slight modifications to those objects. For example, I want to generate the script for a stored procedure, but I want the script to create it under a different schema.

I have an example of what I’m currently trying below. If I’m going in a completely wrong direction then please point me in the right direction. I want to avoid doing any kind of string search and replace since that has a lot of potential hazards.

What I have tried to do is create a function object from an existing function in a database, then I make a new one and try to make it a copy of the one that I just created. This seemed necessary to allow me to change the schema since it has to be in memory and not tied to a database. I then try to change the schema and script it out. Unfortunately, I haven’t been able to find a syntax for .Script that doesn’t fail with an error in PowerShell.

$instance = $args[0]    # Gets the instance name from the command line parameters
$database = $args[1]    # Gets the database name from the command line parameters

# Import the SMO assembly
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null

# Create an instance for the SQL Server
$serverInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance

# Create an instance for the Database
$databaseInstance = $serverInstance.Databases[$database]

# Loop through all of the user defined functions (that are not system objects) in the database
foreach ($function in $databaseInstance.UserDefinedFunctions | Where-Object {$_.IsSystemObject -eq $False})
{
    $newFunction = New-Object ('Microsoft.SqlServer.Management.Smo.UserDefinedFunction') ($databaseInstance, $function.Name, "tnf_dbo_func")

    $newFunction.TextHeader = $function.TextHeader
    $newFunction.TextBody = $function.TextBody

    $newFunction.TextMode = $False
    $newFunction.ChangeSchema("tnf_dbo_func")
    $newFunction.TextMode = $True

    Write-Host $newFunction.Schema
    Write-Host $newFunction.TextHeader
    Write-Host $newFunction.TextBody
    $newFunction.Script()
}
  • 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-14T19:51:55+00:00Added an answer on May 14, 2026 at 7:51 pm

    As I typed in my question I realized that the .TextMode settings were in place from another solution that I was trying. Removing those fixed the problem. Since the question was already typed in I figured I’d post it anyway in case this helps someone else.

    The corrected code:

    $instance = $args[0]    # Gets the instance name from the command line parameters
    $database = $args[1]    # Gets the database name from the command line parameters
    
    # Import the SMO assembly
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
    
    # Create an instance for the SQL Server
    $serverInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance
    
    # Create an instance for the Database
    $databaseInstance = $serverInstance.Databases[$database]
    
    # Loop through all of the user defined functions (that are not system objects) in the database
    foreach ($function in $databaseInstance.UserDefinedFunctions | Where-Object {$_.IsSystemObject -eq $False})
    {
        $newFunction = New-Object ('Microsoft.SqlServer.Management.Smo.UserDefinedFunction') ($databaseInstance, $function.Name, "tnf_dbo_func")
    
        $newFunction.TextHeader = $function.TextHeader
        $newFunction.TextBody = $function.TextBody
    
        $newFunction.ChangeSchema("tnf_dbo_func")
    
        Write-Host $newFunction.Schema
        Write-Host $newFunction.TextHeader
        Write-Host $newFunction.TextBody
        $newFunction.Script()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 409k
  • Answers 409k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have been using the following code in lattice. The… May 15, 2026 at 7:05 am
  • Editorial Team
    Editorial Team added an answer Look in your environment log (e.g. log/development.log) if the query… May 15, 2026 at 7:05 am
  • Editorial Team
    Editorial Team added an answer Sarfraz has already mentioned overflow-x, which is one answer although… May 15, 2026 at 7:05 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.