SSIS - how to access system variables in Script Task - variables

Can anyone please advice how to access/read system variables in Script Component (e.g I want to package name from this variable System::PackageName in Script Component)

In the Script Task Editor, provide the variable names you want to access (for example System::PackageName) in the ReadOnlyVariables field.
From the script, in the C# example, use this:
public void Main()
{
bool fireAgain = true;
// Read the variable
String PackageName = (String)Dts.Variables["System::PackageName"].Value;
// Post the value to progress results
Dts.Events.FireInformation(3, "Package name:", PackageName, "", 0, ref fireAgain);
Dts.TaskResult = (int)ScriptResults.Success;
}
The results:

Related

Extent Report: Name of output file does not change even when defined

Im not able to change the name of the outputfile from the extent reports. It does always create the report file called index.html.
In my code, i want to display the name + date/time.html
Here is the codeline for the path definition:
private const string ExtentReportPath = #".\ExtentReports\";
[OneTimeSetUp]
public void ExtentStart()
{
ExtentHtmlReporter htmlreporter = new ExtentHtmlReporter(ExtentReportPath + "UnlockInstruction_Test" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".html");
extent = new ExtentReports();
extent.AttachReporter(htmlreporter);
}
[OneTimeTearDown]
public void ExtentClose()
{
extent.Flush();
}
First solution ive found is:
I changed ExtentHtmlReporter to ExtentV3HtmlReporter.
But after using ExtentV3HtmlReporter, VS is displaying a warning which says, that this method is obsolete and wont be supported in the future.. so still waiting for a up to date solution.
From what I read the static name (index) is expected behavior to increase the efficiency of reporting.
You can add a unique name to the folder name instead of the file, so you would create sub-folders for each index file like this:
String path =System.getProperty("user.dir")+"//reports//"+time+"//index.html";
ExtentSparkReporter reporter = new ExtentSparkReporter(path);

Spring Shell - capturing user input in middle of executing ShellMethod

Is the a way to capture user input in middle of executing #ShellMethod. Basically stoping executing of the method to ask for the user input and carrying on after capturing it.
There is possible solution here: https://stackoverflow.com/a/50954716, authored by ZachOfAllTrades
It works only when your app is SpringBoot-based, so you'll have access to the LineReader object, configured by SpringBoot.
#Autowired
LineReader reader;
public String ask(String question) {
return this.reader.readLine("\n" + question + " > ");
}
#ShellMethod(key = { "setService", "select" }, value = "Choose a Speech to Text Service")
public void setService() {
boolean success = false;
do {
String question = "Please select a service.";
// Get Input
String input = this.ask(question);
// Input handling
/*
* do something with input variable
*/
success = true;
}
} while (!success);
}
I didn't try it myself, though.
Use Spring Shell UI Components, now that we're in the future.
"Starting from 2.1.x there is a new component model which provides easier way to create higher level user interaction for usual use cases like asking input in a various forms. These usually are just plain text input or choosing something from a list."
#ShellComponent
public class ComponentCommands extends AbstractShellComponent {
#ShellMethod(key = "component string", value = "String input", group = "Components")
public String stringInput(boolean mask) {
StringInput component = new StringInput(getTerminal(), "Enter value", "myvalue");
component.setResourceLoader(getResourceLoader());
component.setTemplateExecutor(getTemplateExecutor());
if (mask) {
component.setMaskCharater('*');
}
StringInputContext context = component.run(StringInputContext.empty());
return "Got value " + context.getResultValue();
}
}
https://docs.spring.io/spring-shell/docs/2.1.0-SNAPSHOT/site/reference/htmlsingle/#_build_in_components
You should be able to interact directly with System.in although it is not really what Spring Shell is about: commands should be self contained.

Only show effective SQL string P6Spy

I'm using p6spy to log the sql statements generated by my program. The format for the outputted spy.log file looks like this:
current time|execution time|category|statement SQL String|effective SQL string
I'm just wondering if anyone knows if there's a way to alter the spy.properties file and have only the last column, the effective SQL string, output to the spy.log file? I've looked through the properties file but haven't found anything that seems to support this.
Thanks!
In spy.properties there is a property called logMessageFormat that you can set to a custom implementation of MessageFormattingStrategy. This works for any type of logger (i.e. file, slf4j etc.).
E.g.
logMessageFormat=my.custom.PrettySqlFormat
An example using Hibernate's pretty-printing SQL formatter:
package my.custom;
import org.hibernate.jdbc.util.BasicFormatterImpl;
import org.hibernate.jdbc.util.Formatter;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
public class PrettySqlFormat implements MessageFormattingStrategy {
private final Formatter formatter = new BasicFormatterImpl();
#Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql) {
return formatter.format(sql);
}
}
There is no such option provided to achieve it via configuration only yet. I think you have 2 options here:
fill a new bug/feature request report (which could bring benefit to others using p6spy as well) on: https://github.com/p6spy/p6spy/issues?state=open or
provide custom implementation.
For the later option, I believe you could achieve it via your own class (depending on the logger you use, let's assume you use Log4jLogger).
Well, if you check relevant part of the Log4jLogger github as well as sourceforge version, your implementation should be rather straightforward:
spy.properties:
appender=com.EffectiveSQLLog4jLogger
Implementation itself could look like this:
package com;
import com.p6spy.engine.logging.appender.Log4jLogger;
public class EffectiveSQLLog4jLogger extends Log4jLogger {
public void logText(String text) {
super.logText(getEffectiveSQL(text));
}
private String getEffectiveSQL(String text) {
if (null == text) {
return null;
}
final int idx = text.lastIndexOf("|");
// non-perfect detection of the exception logged case
if (-1 == idx) {
return text;
}
return text.substring(idx + 1); // not sure about + 1, but check and see :)
}
}
Please note the implementation should cover github (new project home, no version released yet) as well as sourceforge (original project home, released 1.3 version).
Please note: I didn't test the proposal myself, but it could be a good starting point and from the code review itself I'd say it could work.
I agree with #boberj, we are used to having logs with Hibernate formatter, but don't forget about batching, that's why I suggest to use:
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;
import org.hibernate.engine.jdbc.internal.Formatter;
/**
* Created by Igor Dmitriev on 1/3/16
*/
public class HibernateSqlFormatter implements MessageFormattingStrategy {
private final Formatter formatter = new BasicFormatterImpl();
#Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql) {
if (sql.isEmpty()) {
return "";
}
String template = "Hibernate: %s %s {elapsed: %sms}";
String batch = "batch".equals(category) ? ((elapsed == 0) ? "add batch" : "execute batch") : "";
return String.format(template, batch, formatter.format(sql), elapsed);
}
}
In p6Spy 3.9 this can be achieved quite simply. In spy.properties set
customLogMessageFormat=%(effectiveSql)
You can patch com.p6spy.engine.spy.appender.SingleLineFormat.java
removing the prepared element and any reference to P6Util like so:
package com.p6spy.engine.spy.appender;
public class SingleLineFormat implements MessageFormattingStrategy {
#Override
public String formatMessage(final int connectionId, final String now, final long elapsed, final String category, final String prepared, final String sql) {
return now + "|" + elapsed + "|" + category + "|connection " + connectionId + "|" + sql;
}
}
Then compile just the file
javac com.p6spy.engine.spy.appender.SingleLineFormat.java
And replace the existing class file in p6spy.jar with the new one.

SSIS ScriptTask change Value property of Variable

I try to simply change value of SSIS variable doing this code in ScriptTask:
string path = Dts.Connections["BazyPobrane"].ConnectionString.ToString();
string[] nameZIParray = Directory.GetFiles(path, "*.ZIP");
string[] nameRARarray = Directory.GetFiles(path, "*.RAR");
foreach (string nameZIP in nameZIParray) //search new ZIP
{
if (File.GetCreationTime(nameZIP) > DateTime.Now.AddDays(-1))
{
Dts.Variables["User::NazwaPliku"].Value = Path.GetFileName(nameZIP);
}
}
foreach (string nameRAR in nameRARarray) //search new RAR
{
if (File.GetCreationTime(nameRAR) > DateTime.Now.AddDays(-1))
{
Dts.Variables["User::NazwaPliku"].Value = Path.GetFileName(nameRAR);
}
}
Dts.TaskResult = (int)ScriptResults.Success;
After executing ScriptTask it simply don't change the variable Value. Debug mode seems fine. Maybe i miss some component settings?
Thx!
Some things to check:
Are you sure the variable isn't changing? If you put a subsequent script task with a MessageBox in place, does it show the correct value?
I don't think you need the variable type, i.e. remove "user::"
Make sure the variable is in the ReadWriteVariables property, as suggested by #OcasoP
What's the scope of the variable? Make sure you don't have two copies at different scopes, or that at least the one you do have is visible from the scope of the script
You could try locking the variable before writing to it (which should be equivalent to (3) above)
Code example for the last point:
IDTSVariables100 variables = null;
this.VariableDispenser.LockOneForWrite("NazwaPliku",ref variables);
variables[0].Value = myValue;
variables.Unlock();
debug your script task adding MsgBox(variable_name) and see its value through the execution.
Best debugging option :)

How do I prompt for input from an SSIS package?

I want to be able to have a sql query in my DTSX package and I want to be able to have some sort of prompt to update the value of a null column. See what I have below:
UPDATE SF1411
SET [QuoteNumber] = '123456'
, [ItemNumber] = '123654-100'
, [DeleteItem] = 'NO'
WHERE [QuoteNumber] = '0'
I want to be able to be prompted for the QuoteNumber and ItemNumber, then have the script update as needed. Is this possible and if so how can I do it?
This can be acheived as below: This will be in your intial script component.
System.Windows.Forms.Form frm = new Form();
TextBox txt = new TextBox();
Button inputset = new Button();
public void Main()
{
inputset.Text = "Set Variable Value";
inputset.Width = 200;
inputset.Height = 100;
inputset.Click += new EventHandler(inputset_Click);
txt.Name = "Input";
frm.Controls.Add(txt);
frm.Controls.Add(inputset);
frm.ShowDialog();
MessageBox.Show(Dts.Variables["Value1"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
void inputset_Click(object sender, EventArgs e)
{
Dts.Variables["Value1"].Value = Convert.ToInt32(txt.Text);
frm.Close();
}
This should be the initial component in your package to set the variable value or construct you SQL Command.
In general, an SSIS package is not used interactively. Your cleanest solution is a custom solution that gets the input from the user, and then launches the SSIS package.
A simpler alternative is using Package Configurations. You can store the user input in an external location (XML file, SQL Server database, and others) and the SSIS package will load the value at run time.