SSIS Can't Store Decimal Output Into Variable (Execute SQL Task) - sql

For the life of me I can't figure out how to store a decimal from a Stored Procedure into an SSIS Variable.
I have an Execute SQL Task that returns a result set. I mapped the column to the variable. I keep getting "Input string was not in a correct format". If I'm returning a decimal from my stored procedure and have the variable type as decimal. I would think this would work, but it doesn't.
Other things I've tried...
- Cast the column as a float and use variable double.
- Cast the column as a varchar and use variable string (succeeded but didn't store value)
- I've done variations on using Output instead of resultset but basically same issues.
Any ideas on how I can get this to work?
Thanks in advance!
As requested here are some screenshots:
This shows the assignment to the variable.
This is showing the variable as a decimal type.
This is showing the error received.
I didn't include the stored procedure but it defines vPreviousValue as a decimal(18,4) and returns it in the result set.

In case it may be helpful to someone else in the future...
After trying many variations I was able to figure out how to make this work.
I had the stored procedure output into a 'double' parameter.
I also found this link useful: http://milambda.blogspot.com/2014/02/sql-server-integration-services-data.html

Related

varchar to decimal - error with convert()

I have over 17,000 Excel sheets I need to open up and extract info from and store in a sql database. This all works fine as long as the sql data types are varchar.
I want to convert this varchar to decimal, so the data is easier to work with once it's moved to the next stage of the process. This is the error I get:
Exception calling "ExecuteNonQuery" with "0" argument(s): "Error converting data type varchar to numeric."
This is the value in my sql statement. The code script is in PowerShell. Perhaps it is a simple syntax error. It does work without the convert bit.
convert(decimal(9,2),'"+$WorkSheet.Range('H49').Text.replace("'","''").replace(" ","").replace("$","")+"' )
My data looked like "$ 10,210.23" which I was filtering down to look like "10,210.23". It failed to convert because of the comma which looking back is obviously not a number. After filtering it out in addition to the spaces and $ sign it worked like I expected it to. Thanks everyone.

Assign an oracle date to an SSIS variable

I am trying select a date from an Oracle DB (1 row result) and assign its value to an SSIS variable. I tried to convert it to_char (which would also work great to me) but I still get an error "SSIS value does not fall within the expected range"
You'll need to ensure your SSIS variable is of type String.
I expect you'll still need to convert the Oracle date to a string data type with the to_char method but I would have to test and don't have an Oracle instance I can test against.
Apparently the problem was about the name of the Result Name from whatever I was using to '1'. The helpful post that solve my problem is: https://dba.stackexchange.com/questions/129106/ssis-odbc-mapping-result-set-columns-to-variables-returns-error-value-does-not

Getting an SQL Exception about being unable to convert one data type to another, but only when a field is too long?

I have a massive stored procedure that I did not write. It's about 10K lines long. Part of it creates a few temp tables, inserts some records into these tables, then goes through about 8,000 lines of validation and removes invalid records from the tables and re-inserts them into a temp Error table for reporting back to the user.
Somewhere along the lines, I get the following SqlException in my C# code:
Conversion failed when converting the varchar value 'AAAAAAAABA683' to data type int.
But if I shorten the value down to just A683 or something, the procedure runs as expected and just marks this particular record as an error. So it sounds to me like it's not actually a problem with the data types, but something else.
Unfortunately the LineNumber property of the SqlException I catch doesn't help me, since I don't have access to seeing the actual SQL being executed. I can only look at the procedure itself.
Has anyone encountered something like this before?
When I get this error, it is because I am using + as a string concatenation operator, but one (or more) of the arguments is an integer. By the rules that SQL Server uses, if any argument is an integer, then the + is treated as addition rather than string concatenation.
Unfortunately, with a 10,000-line stored procedure you have few options (there could be a diatribe here about software engineering, making code more modular, and using constraints to validate data, but that would not be helpful).
What can you do? I don't actually know. You have a bug in your code, in the sense that the stored procedure is not expecting the types of values you are providing (and all the more ironic because it sounds like most of the procedure is validating values). Some logic in the stored procedure is changing the shorter value to something acceptable for numeric conversion, perhaps something like substring(val, 2, 5) and expecting the value to be an integer.
You could prevent the error by using the concat() function for string concatenation in the stored procedure rather than +. However, that might just hide some other error in the code. You could also prevent the error by pre-validating the data and preventing certain rows from being passed in.

Conditionally changing a variable data type based on input

We have a stored procedure that gets call from an application. One of the input to the SP is SSN. We are using SQL Server 2008R2 Enterprise Edition.
The SSN data type is input as VARCHAR(9). We take that SSN and join it to a table who has the SSN stored as DECIMAL(9,0). This causes an implicit conversion at execution time and is affecting performance.
My original plan was to have the input come in as VARCHAR and just copy the variable over to another variable with the DECIMAL datatype however, we have a kink in the process. A user can also input text as 'new' when there is a new client. When this happens, we get a failed to convert datatype varchar to decimal.
Is there a way to conditionally change the data type of a variable based on the input it receives?
Here's an example of what I am trying to use now, to no avail, as the instances where 'new' is entered cause a conversion error:
CREATE PROCEDURE [dbo].[StoredProcedureFromApplication](
#SubscriberSSN VARCHAR(9))
AS
DECLARE #SSN_DEC DECIMAL(9,0)
SET #SSN_DEC = #SubscriberSSN
Any ideas on how to have the datatype able to be changed?
First, you should store SSNs as strings and not numbers, because they can start with 0.
I think the best you can do is to have the resulting value be NULL
SET #SSN_DEC = (case when isnumeric(#SubscriberSSN) = 1
then cast(#SubscriberSSN as decimal(9, 0))
end);

Trapping Exception using TSQLQuery & params

I am getting a "SQL Server Error: arithmetic exception, numeric overflow, or string truncation" error
here is the code below
AQuery:= TSQLQuery.Create(nil);
with AQuery do
begin
SQLConnection:- AConnection;
SQL.Text:= 'Insert into.....';
ParamByName('...').asString:= 'PCT';
.
.
.
try
ExecSQL;
finally
AQuery.Free;
end;
end;
I have alot of ParamByName lines, and I can't figure out which one is throwing the exception. I just know its thrown on the ExecSQL line. How can i tell which paramByName is causing the error?
When you have the metadata of the table, check the maximum length of string fields. When debugging, check the length of the strings you feed the parambynames. Also check the type of numeric fields, and make sure you don't exceed a maximum value. (I had this problem once with a string which length exceeded the varchars length in the table, and had this problem with a smallint databasefield that I tried to set to a too high value)
Get the SQL text after param substitution and run it as a query in SQL Server management studio.
You'll be able to debug it from there.
You are trying to insert a string value into a field that is not big enough to hold the value. Check the length of the values you are inserting against the length of the field in the table.
As others have said, it's almost certainly that you are pushing too-large a string into one of your fields. It could also happen with numeric values but it's most likely to be a string.
I'd suggest you temporarily alter each of your ParamByName('').AsString:=blah lines with a text constant, eg;
ParamByName('surname').AsString:='Smith';
ParamByName('firstname').AsString:='John';
etc, and see if you get an error. If it goes through without an error, then your problem is most likely to be that one of your string parameters is too long. Check your table schema and debug the actual strings you are putting into the parameters.
Depending on how much access (and experience) you have with this, you might find it more helpful to turn on the SQL Server logging such that you can see your queries (and the contents of those parameters) when the get processed by the SQL server. This will show you exactly what string and numeric values are actually being given to the server.
Which version/edition of SQL Server are you using?