SSIS Variable- Evaluating Expression Taking Time in Script Task - sql-server

I have a Variable which uses another variable to get its value. I had set the EvaluateAsExpression property to True. Now when I use this variable in a Script task it is taking 2-3 Minutes to evaluate that expression. But if I Hard Code the variable value instead of reading it from another variable then it is finishing in less than a Second. Anyone let me know if I am missing anything ? I am using BIDS/SSIS 2008
Variable1: D:\app\srikar.mogaliraju
Variable2: #[User::Variable1]+"\\Product"
Script Task Code:
public void Main()
{
String SourceFolderPath, DestinationFolderpath;
SourceFolderPath = Dts.Variables["User::Variable2"].Value.ToString();
//Random Code
Dts.TaskResult = (int)ScriptResults.Success;
}
Variable 2 Expression Builder:

I haven't got any solution for this. So posting the answer with the solution I implemented to bypass this problem.
Use ExecuteSqlTask to read Variable1 and Append "\Product" to Variable1 value.Set the result set property to Single Row.
Assign the Result Set to Variable2 and use this Variable in Script task.

Related

How to use a SQL output parameter with FromSqlInterpolated? Or alternative

Using ASP.Net core 3.0. my Database query works - I can get the result set (records), but how to pass/get the output parameter? Also would like to know if there's a way to get the return value.
I tried using different ways to call the SQL query and the only one I was able to get working was FromSqlInterpolated, but open to different methods.
This code works for me, but I want to pass an additional parameter that can get populated as an output parameter (which is working when I test it by calling the stored proc from within SQL Server).
var result = _context.Users
.FromSqlInterpolated($"EXEC mystoredproc {user.Username} ").AsEnumerable().FirstOrDefault();
I tried creating a variable before the call
string out1 = null;
And then including that in the call but I can't figure out the syntax and I'm not sure if it's supported with this method.
var result = _context.Users
.FromSqlInterpolated($"EXEC mystoredproc {user.Username}, OUTPUT {out1}").AsEnumerable().FirstOrDefault();
Console.WriteLine(out1);
Hoping someone can point me in the right direction - would like to know both how to use the output parameter and how to get the return value, not just the record set. Thank you.
The answer accepted does not seem to work for me.
However I finally manage to have the OUTPUT parameter work by using the following code
var p0 = new SqlParameter("#userName", {user.Username});
var p1 = new SqlParameter("#out1", System.Data.SqlDbType.NVarChar, 20) { Direction = System.Data.ParameterDirection.Output };
string out1 = null;
var result = _context.Users
.FromSqlRaw($"EXEC mystoredproc #userName, #out1 OUTPUT ", p0, p1).AsEnumerable().FirstOrDefault();
Console.WriteLine(p1.Value); // Contains the new value of the p1 parameter
Just a litte warning, the p1 value property will be changed only when the request will be executed (by an AsEnumerable, Load() or anything that forces the Sql execution)

Interception not working as expected with Entity Framework 6

I'm trying to follow the interception example shown here to get it working with EF 6 but running into a problem with the function RewriteFullTextQuery as shown in Figure 1. The interception seems to work but it does not actually execute the logic in the for loop of the RewriteFullTextQuery method because the cmd.Parameters.Count is always zero. Furthermore the cmd.CommandText property seems to be displaying the correct SQL query which I take as another piece of evidence that the interception is working correctly.
Figure 1: RewriteFullTextQuery Code Excerpt
public static void RewriteFullTextQuery(DbCommand cmd)
{
string text = cmd.CommandText;
for (int i = 0; i < cmd.Parameters.Count; i++)
{
DbParameter parameter = cmd.Parameters[i];
if (parameter.DbType.In(DbType.String, DbType.AnsiString, DbType.StringFixedLength, DbType.AnsiStringFixedLength))
{
The RewriteFullTextQuery function is being called by the ReaderExecuting function shown in Figure 2 which gives it the command argument that is causing all the trouble.
Figure 2: ReaderExecuting Function
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
RewriteFullTextQuery(command);
}
Even though my code isn't exactly the same as the example, the interception seems to be working so it is making me wonder what conditions is it that will populate the command to have a Parameters.Count of more than zero?
It works only if you pass the parameter to the query as a variable. If you use a literal EF won't use parameters.
I mean, this won't generate any parameter
context.Notes.Where(_ => _.NoteText == "CompareValue").Count();
This will
string compareValue = "CompareValue";
context.Notes.Where(_ => _.NoteText == compareValue).Count();
It turns out that it is because of the way Entity Framework generates the SQL. If you pass in a string literal as your search value to your LINQ statement it does not generate a SQL that makes use of a parameter. But if you pass in your search value as a variable, it will generate the SQL that utilizes a parameter. A solution (for dynamic queries) and more details can be found on this blog.

eval(): can't assign to function call

Yet another question for my cookie clicker...
Here is the code I made that produces an error:
cps=cps+bcps[buych]
c=c-bprice[buych]
eval(buych)=eval(buych)+1
cps is a variable,
c is a variable,
b1,b2,b3,b4,b5 are variables
buych is a string (Which I get from an input() command)
bcps and bprice are maps (shown below)
bcps = {"b1":'1',
"b2":'5',
"b3":'10',
"b4":'20',
"b5":'25'}
bprice = {"b1":'10',
"b2":'20',
"b3":'30',
"b4":'40',
"b5":'50'}
So, what I'm trying to achieve is:
-Take the input string value, which will be 'b1','b2', 'b3', 'b4', 'b5'
-Increase cps buy it's value within bcps
-Decrease c by it's value within bprice
-Use eval to convert the 'b#' to b#
-Increase b# by 1
When running the script, I do not get error text in the python shell. Instead, I get a pop-up that says "Can't assign to function call". The error is highlighted as the first white space before the eval(). I really don't get this, as I have no functions within the error.
Thanks for reading,
Cookie Monster
eval(buych) is a function call -- you're calling the function eval(). You can only assign to variables, you can't assign to the result of a function.
You need to concatenate the variable into a valid assignment expression, and exec that:
exec(buych + '=' + buych + '+1')
eval is for evaluating expressions, you have to use exec to execute a whole statement.
But if you find yourself needing to do this, you're usually doing something wrong. Instead of computing variable names and expressions, you should use lists or dicts so you can refer to them using an index or key.

setting object properties value for object array in matlab

I have created an array of objects and I would like assign a property value in a vector operation without using a for loop. Unfortunately I get an error.
A simplified example of the problem.
classdef clsMyClass < handle
properties
dblMyProperty1
end
methods
function obj = clsMyClass()
end
end
end
And when running
vecMyArray = clsMyClass.empty(100,0);
vecMyArray(100) = clsMyClass;
vecMyArray.dblMyProperty1 = 1:100;
We get the following error:
??? Incorrect number of right hand side elements in dot name
assignment. Missing [] around left hand side is a likely cause.
Any help would be appreciated.
I see what you're trying to do now. Use disperse from the MATLAB File Exchange:
>> [vecMyArray.dblMyProperty1] = disperse(1:100);
>> vecMyArray(1).dblMyProperty1
ans =
1
>> vecMyArray(10).dblMyProperty1
ans =
10
You can use the deal function for exactly this purpose:
[vecMyArray.dblMyProperty1] = deal(1:100);
See: http://www.mathworks.com/company/newsletters/articles/whats-the-big-deal.html
Edit: No you can't, actually; that'll set them to all be the vector 1:100.
I think you'll find your answer here in "Struct array errors." Even though this is a class, similar rules apply.
Unfortunately missing [] is not the cause, since adding them causes more errors. The cause is that you cannot assign the same value to all fields of the same name at once, you must do it one at a time, as in the following code:
So you'll need:
for ii=1:100
vecMyArray(ii).dblMyProperty1 = ii;
end
I know it's not satisfying, but I think it at least helps us to definitively understand this error.

How do I use a package variable inside a Script task?

Currently, I have a file path value hard coded inside the Script task of an SSIS package.
I have a string variable sPath. How should I use this variable sPath inside Script Task?
string strPath = "C:\\File.xls";
if( File.Exists(strPath))
{
File.Delete(strPath);
}
Here is one possible way of using variables inside Script Task. Assuming that you have a variable named FilePath declared on your package as shown in screenshot #1 then you can use the following code to use the variable inside the Script Task. this is one of the possible ways to use the variable. Here the variable is used only to read the value using the method LockForRead. You can also write values to the variable if the variable is declared with LockForWrite method.
By the way, the functionality described in the Scrip Task code can also be carried out using the File System Task available in SSIS Control Flow task list.
Hope that helps.
Using package variables inside Script Task:
C# code that can be used only in SSIS 2008 and above.
.
public void Main()
{
Variables varCollection = null;
string FilePath = string.Empty;
Dts.VariableDispenser.LockForRead("User::FilePath");
Dts.VariableDispenser.GetVariables(ref varCollection);
FilePath = varCollection["User::FilePath"].Value.ToString();
if (File.Exists(FilePath))
{
File.Delete(FilePath);
}
varCollection.Unlock();
Dts.TaskResult = (int)ScriptResults.Success;
}
Screenshot #1:

Resources