private SqlSmoObject FunctionObj(SmoScriptingContext ctx) { var function = new UserDefinedFunction(ctx.Database, ctx.Metadata.Name, ctx.Metadata.Schema); function.Refresh(); return(function); }
/// <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(smoObject.Script()); } catch (Exception ex) { Logger.Write(LogLevel.Error, "Exception at PeekDefinition GetTableValuedFunctionScripts : " + ex.Message); return(null); } }
public static void ExecuteFunction(UserDefinedFunction func, SqlConnectionInfo connInfo) { try { string execScript = ""; var builder = new StringBuilder(1000); lock (func) { func.Refresh(); func.Parameters.Refresh(true); string execTemplate = func.FunctionType == UserDefinedFunctionType.Scalar ? "SELECT " : "SELECT * FROM "; execScript = string.Format("{0}\r\n {1} [{2}].[{3}]", UseDataBaseGo(func.Parent), execTemplate, func.Schema, func.Name); string parameterList = ""; if (func.Parameters.Count > 0) { for (int i = 0; i < func.Parameters.Count; i++) { if (i > 0) { parameterList += " , "; } parameterList += MakeParameterForFunction(func.Parameters[0]); } } execScript += " ( " + parameterList + " ) "; } CreateSQLDocumentWithHeader(execScript, connInfo); } catch (Exception ex) { log.Error("ExecuteFunctionfailed.", ex); } }
public static void OpenFunctionForModification(UserDefinedFunction userDefinedFunction, SqlConnectionInfo connInfo, HuntingDog.Config.EAlterOrCreate alterOrCreate) { try { var builder = new StringBuilder(1000); lock (userDefinedFunction) { userDefinedFunction.Refresh(); builder.AppendLine(UseDataBaseGo(userDefinedFunction.Parent)); builder.Append(userDefinedFunction.ScriptHeader(alterOrCreate == EAlterOrCreate.Alter)); builder.Append(userDefinedFunction.TextBody); } CreateSQLDocumentWithHeader(builder.ToString(), connInfo); } catch (Exception ex) { log.Error("OpenFunctionForModification failed.", ex); } }