parameterized query with long string - sql

I have a parametrized SQL query that I want to execute from (local) R on Exasol database as described here:
https://db.rstudio.com/best-practices/run-queries-safely/#parameterized-queries.
with tab as
(select
t.*,
position(value in ?) as pos
from MY_TABLE t
)
select * from tab where pos > 0;
The value that is passed to ? is a (long) string. When this string is 2000 characters long or less, everything works fine. When I increase it to 2001 characters, I get an error:
Error in result_bind(res#ptr, as.list(params)) :
nanodbc/nanodbc.cpp:1587: 40001: [EXASOL][EXASolution driver]GlobalTransactionRollback
msg: data exception - string data, right truncation. (Session: 1640027176042911503)
I guess the source of the problem is that my parameter is recognized as CHAR and not as VARCHAR.
The Exasol User Manual states:
"The length of both types is limited to 2,000 characters (CHAR) and 2,000,000 characters (VARCHAR), respectively".
Is there any way to cast ? to VARCHAR?

If you establish your db connection via ODBC you could try having a look at these parameters:
MAXPARAMSIZE and DEFAULTPARAMSIZE.
Probably, if you set DEFAULTPARAMSIZE to a higher value in the odbc config:
https://docs.exasol.com/connect_exasol/drivers/odbc/using_odbc.htm?Highlight=varchar

The problem above has been present when I tried using the first suggested method for running parametrized queries described in tutorial here: https://db.rstudio.com/best-practices/run-queries-safely/. This first approach uses a combination of functions dbSendQuery() and dbBind().
My problem with long strings has been solved when I switched to the second (less safe) method which uses the sqlInterpolate() function.

Related

SSRS Report builder LENGTH expression & size specific field

I'm trying to build a bank file using SSRS report builder (3.0). I'm running into two issues:
I'm trying to get a length expression to work on a check number field but LEN(Field) doesn't work (returns 4 as the value regardless of the actual length of the field).
And LENGTH(Field) gives me an error:
The Value expression for the textrun 'Textbox15.Paragraphs[0]. TextRuns[0\' contains an error: [BC30451] Name 'LENGTH' is not declared*
The only reason why I'm even trying to get #1 to work is because I need to have one of the fields on the bank file have a constant length. Regardless of the check number, I need to make sure this field is always at 14 characters with leading zeros. I thought the best way to do this is to do a switch statement and add the number of appropriate zeros in depending on the size of the check number field.
Thanks for the help.
Edit: using a SQL server DB
For the length issue:
There are two ways to get string length
Using the LEN function
= LEN(Fields!myfield.Value)
Using the length property
= Fields!myfield.Value.Length
If your field is not a string, try converting first by using the Cstr function
= LEN( Cstr(Fields!myfield.Value) )
= Cstr(Fields!myfield.Value).Length
For the formatting issue:
For numeric fields set the cell format expression to have as many zeros as required eg. for 14 digit numbers
= "00000000000000"
I don't know on which database you are working if you are using sql server then try LEN
function and LENGHT in oracle.
I think you first convert it into integer if it's character and then try len function.

Specify scale in a ABS function?

Is it possible to specify scale in a ABS function from a decimal ?
= ABS(([Accounting].salary+ABS([Accounting].expenses))/2)
As of right now everything works correctly but there is a problem with results when the number has only 1 number after the decimal point:
100.12 - All ok
230.1 - Not ok, it should be 230.10
I want the outcome to always have two numbers after the decimal point.
The query has to work on both MS SQL Server and Oracle. I can specify the query for each db manually.
To me, this is more a user interface issue than a SQL issue.
230.1 - Not ok, it should be 230.10
Seriously, that's both the same number.
In Oracle you can convert a number into a string in order to present it to an end user.
select to_char(234.1,'$9,999.99') from dual
$234.10
Have a look at format models in the Oracle documentation.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
Just change code:
= ABS(([Accounting].salary+ABS([Accounting].expenses))/2)
By the following:
= CAST(ABS(([Accounting].salary+ABS([Accounting].expenses))/2) AS DECIMAL(18 /*set your format here instead of 18*/,2))
I've just checked it in code:
SELECT CAST(230.1 AS DECIMAL(18 /*set your format here instead of 18*/, 2))
And all works fine
In fact, it's just a question of representation of your numbers in the UI for the user. You can convert your number to the string with using to_char () with the format specified. But it is better to define the output format on the front-end

SSIS to handle SQL binary data type

I am strugling wiith handling a sql binary(8) data type.
No matter what I try to do with it inside the SSIS package, it always fails with an error of: "Invalid cast specification"
Let me describe what I am tying to do in details:
I have a single row that I am assigning to a variable in a SQL Task in the control flow.
select max(LastRowVersion as bigint) as MinRV from MyTable
LastRowVersion is of datatype binary(8).
2.Then I am assigning the result to variable - User::MaxRowVersion
If I configure MaxRowVersion to be of String or Object data type, then this part works fine.
3.Next I am opening a data flow task with the following select statement:
select fields
from AnotherTable
where LastRowVersion > ?
and assigning User::MaxRowVersion to the query.
Again LastRowVersion is of datatype binary(8) in the table - AnotherTable.
Here is where I am getting the error that I mentioned above.
I have tried various types of playing with the DT_BYTES cast type in the expression of the User::MaxRowVersion variable, but it is failing.
I have also read that there is a possibility to open a C# script task to handle it or that Dynamic SQL can help, but I would rather keep the solution as simple as possible with no scripting if possible.
Thanks for the help,
Dani
Have you tried this?
Keep MaxRowVersion as a Sting data type
Cast the variable reference in the SQL statement
Example:
"
SELECT my_field
FROM my_table
WHERE LastRowVersion > CAST('" + #[User::MaxRowVersion] + "' as MyDataType);
"

Convert String Variable to Datetime Returns Incorrect Value in SSIS

Following my question in https://stackoverflow.com/questions/7387418/where-clause-using-date-taken-from-string-variable-sent-out-incorrect-return-in-s, this is the simplified version of the question:
The query used for the OLE DB Source is this, with a string type variable inside:
select
*
from
A
where
A.A_IN_DATETIME < CONVERT(DATETIME,?,105) and
(A.A_OUT_DATETIME is NULL or A.A_OUT_DATETIME >= CONVERT(DATETIME,?,105))
The query above works inside a Foreach Variable Enumerator, with the variable used is an array of string variable consisting of this: 13-09-2011,12-09-2011,11-09-2011,10-09-2011,09-09-2011,08-09-2011,07-09-2011,06-09-2011,05-09-2011,04-09-2011,03-09-2011,02-09-2011,01-09-2011
The condition for the problem is this: For example, there is a record in A with A_IN_DATETIME = 2011-09-12 (YYYY-MM-DD format) and A_OUT_DATETIME = NULL.
The correct result for the query above that it should have been only returning values for 13-09-2011 and the rest return 0 records, but in the execution, there was a result for 12-09-2011 to 10-09-2011 also. I don't know if the SSIS somehow mistaken the 12-09-2011, 11-09-2011, 10-09-2011 to 09-12-2011,09-11-2011,09-10-2011 (FYI, I've checked the parsing result and also the loop enumerator by printing it out in a message box generated from the script task, and it is still in its correct form).
Is there any solution for this problem?
Run Sql Profiler and see what query comes through to SQL Server. This will allow you to re-run the exact query in SSMS and see the results right there.
I don't believe conversion to DATETIME accepts a format, i.e. 105.Try this instead:
CONVERT(DATETIME, '9/13/2011 15:37:00')
Result is DATETIME - 2011-09-13 15:37:00.000

SQL Paramaters in FoxPro 2.6 DOS

In FoxPro 2.6 for MS-DOS is there a way to use a variable in a SELECT command? For example, how can I write the following query:
SELECT * FROM DBFILE WHERE Ord_no = temp_no
Given that temp_no is a previously defined variable. I tried using "&temp_no" but this does not appear to be the correct syntax.
Your code looks correct, and you shouldn't need to macro it via the "&". What may be failing is due to data types. If your table "dbfile", column "ord_no" is numeric and your variable "temp_no" is a character string, that would fail due to a data type mismatch... make sure they are the same data type... again, REGARDLESS of using the "&" macro.
MyVarOrd_No = 23
select * from DBFile where Ord_No = MyVarOrd_No
or if a string/charcter based column, just change
MyVarOrd_No = "23"
However you may need to pad with spaces/justify if its being picky.
The microsoft line on using variables in foxpro.