SQLRPGLE Insert Statement not working - db2

I have a simple insert statement I want to execute using sqlrpgle.
I build the statement first depending on what variables are empty and then want to use exec sql to do the insert.
This does not work.
But entering the same insert statement with subquery in STRSQL works fine.
I have broken down the sql statement to its simplest version for this question.
Here is the SQLRPGLE code snippet
And here is where I enter the same statement in STRSQL
Any Assistance would be greatly appreciated.
**EDIT
This only happens when I add a where clause, which is essentially what I need and why I am trying to do it using sqlrpgle.
Statements like
sqlstmt = 'insert into jallib/sbrmrpt (shscdt, shcmdt) ' +
'select shscdt, shcmdt ' +
'from r50files/sbschd ';
and
sqlstmt = 'insert into jallib/sbrmrpt (shscdt, shcmdt) ' +
'values (10, 10) ';
All work just fine, once a where clause is added to the subquery is where it does not work in the RPG code but in STRSQL

You have sqlstmt defined as 100 characters maximum and with the where clause your string is over 100 characters long. Your SQL statement is getting truncated and that's why its not working. I suspect you know how to fix this but just for completeness, the solution is the following:
D sqlstmt s 500a varying

Related

How to export multiple csv files from postgresql for loop query?

I have got 100 state codes. I need to use them in a query like this:
select *
from public.clients
where state = '01'
and get 100 different .csv files. I haven't got superuser rights, so if I got it right I can only do it with \copy in psql command line. But I can't understand how to use it properly.
I tried to make it like this:
DO
$do$
declare
i text;
arr text[] := array(
select distinct state
from public.clients c
where state is not null
order by state);
BEGIN
FOREACH i IN ARRAY arr
LOOP
EXECUTE format($x$\COPY (
SELECT *
FROM public.clients c
WHERE c."state" = %1$s)
TO 'G:\Desktop\states\.%1$s.out.csv' WITH DELIMITER ',' CSV HEADER$x$, i);
END LOOP;
END
$do$
But I got ERROR: syntax error at or near "\" and I think it's not the last error because I'm not sure in this WHERE c."state" = %1$s and this TO 'G:\Desktop\states\.%1$s.out.csv' WITH DELIMITER ',' CSV HEADER$x$, i);
DO sends it program to the server to run. Since \copy is a psql metacommand, the server won't know how to run it. So you can't do it this way.
You also can't use the \gexec metacommand, as that can only be used to run SQL commands, not other metacommands.
So you can write a script (say, perl or python, or bash) to print out a series of \copy... and then pipe that into psql, or send it to a file and execute psql -f file.sql

DB2 trigger illegal token

Fairly new to DB2 sql, so forgive my ignorance :)
I have a trigger with a condition inside. I want to then insert some params depending on the condition.. Here it is:
I've looked at DB2 documentation for triggers and also for if statements, and at least to my eyes it appears to comply with it, however i get a -104 error (Illegal symbol token) on the insert line.
The insert works fine provided i use values not from 'N'.
OK, it works if i have nothing in the if then statement.. but only if i have nothing!
Thanks
My guess would be that DB2 is confused by your statement terminators. You use a semicolon to terminate the CREATE TRIGGER statement, but at the same time you use the same terminator inside the CREATE TRIGGER statement, after the INSERT.
In whatever client you use to execute your code, redefine the statement terminator to something else and place that terminator at the end of CREATE TRIGGER, after END ID. For example, if you use the command line processor, save this to a file trig.sql:
CREATE OR REPLACE TRIGGER AUTO_INSERT_DEPO_NOMINEE
AFTER INSERT ON CA_ENTITLEMENT
REFERENCING NEW AS N
FOR EACH ROW
IF NOT EXISTS (SELECT D.DEPO_CD FROM DEPO D WHERE D.DEPO_CD = N.DEPO_CD)
THEN
INSERT INTO DEPO (DEPO_CD, DEPO_NME, BRANCH_CD, AUTO_GENERATED)
VALUES(N.DEPO_CD, NULL, N.BRANCH_CD, 'Y');
END IF#
then run it, specifying "#" as the statement terminator:
db2 -td# -f mytrig.sql

execute command in while loop

I have a postgres function which runs the following loop
while x<=colnum LOOP
EXECUTE
'Update attendrpt set
slot'||x||' = pres
from (SELECT branch, semester, attend_date , div, array_to_string(ARRAY_AGG(first_name||':'||alias_name||':'||lect_type||':'||
to_char(present,'99')),';')
As pres
from attend1 where lecture_slot_no ='||x||'
group by branch, semester, attend_date , div ) j
where attendrpt.branch=j.branch and attendrpt.semester=j.semester
and attendrpt.attenddate=j.attend_date and attendrpt.div=j.div;';
`x:=x+1;
END LOOP;`
The problem here is it is conflicting the single quotes closing in query and the execute command.Is there anyway to solve this.
Thanks in advance.
Quote your function definition with dollar-quoting (like $BODY$ or just $$) as per the manual.
Use execute ... using instead of string substitution. For substituting identifiers use the %I format specifier from the format function.
If you absolutely must use || string concatenation, say if you're on some ancient version of PostgreSQL, you need to use the quote_literal and quote_ident functions to avoid issues with quoting and potential security problems.
Beyond that, it looks like the whole approach is completely unnecessary; you're doing something that looks like it can be done in simple SQL.

T-SQL, Remove space in string

I have two strings in SQL and the REPLACE function only works on one of them, why is that?
Example 1:
SELECT REPLACE('18 286.74', ' ', '')
Example 2:
SELECT REPLACE('z z', ' ', '')
Example 1's output is still "18 286.74" whereas Example 2's output is "zz". Why does SQL not react the same way to both strings?
UPDATE:
When running select replace('123 123.12', ' ', '') that works fine, still not with '18 286.74'.
Test it the following way.
select unicode(substring('18 286.74', 3, 1))
If the code returns 32 then it's a space, if not, it's a different Unicode character and your replace ' ' won't work.
maybe cast is needed.
UPD: or not(on sql 2005 works fine too)
Are you sure it is a space? i.e. the same whitespace character that you are passing as the second argument? The code you've posted works fine for me on SQL Server 2008.
Re working on your friends PC - perhaps the whitespace got normalized when you sent it to him?
You are probably using non-breakable space.
I could reproduce it by typing ALT+0160 into the number in SELECT REPLACE('18 286.74', ' ', '')
Could you please issue this following:
SELECT CAST('18 286.74' AS BINARY), REPLACE('18 286.74', ' ', '')
by copying the '18 286.74' from REPLACE into CAST?
I was having the same issue and found that it was a char(10) (line feed). when copied out of Managment Studio it became a char(32) but in the record it was a char(10) try
Select Replace(#string, char(13), '')

Inserting Values into Dynamic Sql within a stored procedure - Where do the ticks go?

I'm trying to create sql statement in TSQL that looks like this:
INSERT INTO UsersTable (UserName) VALUES (#UserName)
This is easy until you're trying to do it dynamically in T-Sql and #UserName is a varchar
The it looks like this:
SELECT #SQLInsert = 'INSERT INTO UsersTable (UserName) ' +
'VALUES (' + #UserName + ')'
Except of course this doesn't work. How many ticks do I need to create ticks in a string???? Driving me crazy!
I think the problem is your not quoting the #UserName string properly. The best practices way (and safe way) to do this is to use a parameterized query using sp_executesql. Below is how it would be done using sp_executesql (untested). I hope this helps.
DECLARE #SQLInsert NVARCHAR(500)
DECLARE #paramDef NVARCHAR(500)
SET #SQLInsert = 'INSERT INTO UsersTable (UserName) VALUES ( #InsUserName )'
SET #paramDef = '#InsUserName NVARCHAR(50)'
EXECUTE sp_executesql #SQLInsert, #paramDef,
#InsUserName = #UserName;
Literal single-quotes within strings are represented by two consecutive single-quotes.
See Constants (Transact-SQL).
I'm curious as to why you are placing the ' + ' between the first part and the VALUES. It is unnecessary, but other than that go with Justin's answer.
What you are trying to do is vulnerable to SQL Injections:
http://en.wikipedia.org/wiki/SQL_injection
It is very unsafe to wrap your value in quotes.
It is safer to use sp_executesql with parameters or not use it at all:
http://msdn.microsoft.com/en-us/library/ms188001.aspx