public static void ScriptUserDefinedFunctions(Database sDatabase, ScriptingOptions cOption, ScriptingOptions rOption, string funcName, string schemaName) { UserDefinedFunction userFunction = sDatabase.UserDefinedFunctions[funcName, schemaName]; userFunction.Script(cOption); userFunction.Script(rOption); }
/// <summary> /// Script a TableValuedFunction using SMO /// </summary> /// <param name="objectName">TableValuedFunction name</param> /// <param name="schemaName">Schema name</param> /// <returns>String collection of scripts</returns> internal StringCollection GetTableValuedFunctionScripts(string objectName, string schemaName, ScriptingOptions scriptingOptions = null) { try { UserDefinedFunction smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedFunction(this.Database, objectName) : new UserDefinedFunction(this.Database, objectName, schemaName); smoObject.Refresh(); return((scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions)); } catch (Exception ex) { Logger.Write(LogLevel.Error, "Exception at PeekDefinition GetTableValuedFunctionScripts : " + ex.Message); return(null); } }
public void ScriptObject(UserDefinedFunction function, Options options) { StringCollection script = function.Script(_scriptingOptions); string path = _directoryService.GetFunctionsDirectory(options.Root); string fileName = Path.Combine(path, string.Concat(function.Name, ".sql")); _fileWriter.WriteFile(fileName, script.Cast <string>()); }
public static string GetFunctionDescription(string pDatabaseName, string pSchemaName, string pFunctionName, string connectionString) { SqlConnection connection = new SqlConnection(connectionString); Server server = new Server(new ServerConnection(connection)); Database database = server.Databases[pDatabaseName]; UserDefinedFunction fn = database.UserDefinedFunctions[pFunctionName, pSchemaName]; StringBuilder script = new StringBuilder(); foreach (string line in fn.Script()) { script.Append(line); } return(script.ToString()); }
private void LoadUserDefinedFunctions(Database db, string workingFolder) { List <string> UDFs = GetNonSystemObjectNames(serverName, db.Name, eObjectTypes.function); foreach (string name in UDFs) { int pos = name.IndexOf("."); string nameOnly = name.Substring(pos + 1); string schema = name.Substring(0, pos); UserDefinedFunction udf = db.UserDefinedFunctions[nameOnly]; if (udf == null) { // find it. I couldn't find out what the key should be when the object has a schema other than dbo System.Collections.IEnumerator i = db.UserDefinedFunctions.GetEnumerator(); while (i.MoveNext()) { UserDefinedFunction spo = (UserDefinedFunction)i.Current; if (spo.Name == nameOnly && spo.Schema == schema) { //Console.Out.WriteLine("Found"); udf = spo; break; } } } filename = udf.Schema + "." + udf.Name; oname.Text = "Processing UDFs... " + filename; FileStream file = CreateFileStream(workingFolder + filename + ".sql"); StreamWriter sw = new StreamWriter(file); StringCollection sc = udf.Script(); WriteToStream(sc, sw); sw.Close(); file.Close(); objectCount++; } }
protected void ScriptFunctions(DbServerContext context, IEnumerable <ScriptObject> udfs) { if ((m_options.ObjectsToInclude & ObjectTypeEnum.Function) == ObjectTypeEnum.Function) { string outputDirectory = Path.Combine(m_definitionDirectory, m_options.FolderNameFunctions); foreach (ScriptObject udfObject in udfs) { if (udfObject.ObjectAction == ObjectActionEnum.Drop) { DeleteScript(udfObject, outputDirectory); } else { UserDefinedFunction currentUdf = context.Database.UserDefinedFunctions[udfObject.ObjectName, udfObject.ObjectSchema]; ScriptDefinition(udfObject, outputDirectory, new Func <StringCollection>(() => { return(currentUdf.Script(m_options.ScriptingOptions)); })); } } } }