I'm attempting to insert multiple rows into a DB2 database using c / c++ code like this:
EXEX SQL BEGIN DECLARE SECTION;
char inputArrayChar1 [3][10];
char inputArrayChar2 [3][10];
char inputArrayChar3 [3][10];
EXEX SQL END DECLARE SECTION;
for(int i = 0 ; i < 3 ; i++)
{
sprintf(inputArrayChar1[i] , "column1Data%d" , i + 1);
sprintf(inputArrayChar2[i] , "column2Data%d" , i + 1);
sprintf(inputArrayChar3[i] , "column3Data%d" , i + 1);
}
EXEC SQL INSERT INTO TABLETEST (COLUMN1 , COLUMN2 , COLUMN3)
VALUES(:inputArrayChar1 , :inputArrayChar2 ,inputArrayChar3);
run process DB result : only 1 row data , other 2 rows data not insert to DB.
Can anyone explain what could account for this?
I have inquiries to 1 case , need add ROWS syntax code like this:
EXEC SQL INSERT INTO TABLETEST (COLUMN1 , COLUMN2 , COLUMN3)
3 ROWS VALUES(:inputArrayChar1 , :inputArrayChar2 ,inputArrayChar3);
but use this syntax , Compiler issue is SQL0104N message "An unexpected token '3 ROWS' ....."
Can anyone explain what could account for this? Or I need add Environment variable to db ? or add Environment variable to Compiler Environment ?
thanks.
Related
I am looking for an example of how to use QuestDB embedded in Java process. I have a table
CREATE TABLE market_tick ( tickTimeStamp TIMESTAMP, token SYMBOL)
timestamp(tickTimeStamp)
PARTITION BY MONTH
and I append rows like
TableWriter tableWriter = questDB.getTableWrite( "market_tick" ) ;
TableWriter.Row row = tableWriter.newRow() ;
row.putTimestamp( 0, timeStamp );
String token = String.valueOf(event.getToken()) ;
row.putSym(1, token );
tableWriter.commit();
and I have exception
io.questdb.cairo.CairoException: [5] Cannot remove directory: C:\trade\questdb\market_tick\1970-01\token.k
at io.questdb.std.ThreadLocal.initialValue(ThreadLocal.java:36)
at java.base/java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:195)
at java.base/java.lang.ThreadLocal.get(ThreadLocal.java:172)
at io.questdb.cairo.CairoException.instance(CairoException.java:42)
How to insert rows using Java embedded API?
Since the table market_tick has designated timestamp, you cannot use tableWriter.newRow() to create new row but have to use tableWriter.newRow(timeStamp). The rest of the code is correct
TableWriter tableWriter = questDB.getTableWrite( "market_tick" ) ;
TableWriter.Row row = tableWriter.newRow(timeStampMicroSeconds) ;
String token = String.valueOf(event.getToken()) ;
row.putSym(1, token );
tableWriter.commit();
There is a scenario where I receive a string to the bigquery function and need to use it as a column name.
here is the function
CREATE OR REPLACE FUNCTION METADATA.GET_VALUE(column STRING, row_number int64) AS (
(SELECT column from WORK.temp WHERE rownumber = row_number)
);
When I call this function as select METADATA.GET_VALUE("TXCAMP10",149); I get the value as TXCAMP10 so we can say that it is processed as SELECT "TXCAMP10" from WORK.temp WHERE rownumber = 149 but I need it as SELECT TXCAMP10 from WORK.temp WHERE rownumber = 149 which will return some value from temp table lets suppose the value as A
so ultimately I need value A instead of column name i.e. TXCAMP10.
I tried using execute immediate like execute immediate("SELECT" || column || "from WORK.temp WHERE rownumber =" ||row_number) from this stack overflow post to resolve this issue but turns out I can't use it in a function.
How do I achieve required result?
I don't think you can achieve this result with the help of UDF in standard SQL in BigQuery.
But it is possible to do this with stored procedures in BigQuery and EXECUTE IMMEDIATE statement. Consider this code, which simulates the situation you have:
create or replace table d1.temp(
c1 int64,
c2 int64
);
insert into d1.temp values (1, 1), (2, 2);
create or replace procedure d1.GET_VALUE(column STRING, row_number int64, out result int64)
BEGIN
EXECUTE IMMEDIATE 'SELECT ' || column || ' from d1.temp where c2 = ?' into result using row_number;
END;
BEGIN
DECLARE result_c1 INT64;
call d1.GET_VALUE("c1", 1, result_c1);
select result_c1;
END;
After some research and trial-error methods, I used this workaround to solve this issue. It may not be the best solution when you have too many columns but it surely works.
CREATE OR REPLACE FUNCTION METADATA.GET_VALUE(column STRING, row_number int64) AS (
(SELECT case
when column_name = 'a' then a
when column_name = 'b' then b
when column_name = 'c' then c
when column_name = 'd' then d
when column_name = 'e' then e
end from WORK.temp WHERE rownumber = row_number)
);
And this gives the required results.
Point to note: the number of columns you use in the case statement should be of the same datatype else it won't work
I would like to get a different result to my select statement when a parameter is 0, 1 or 2. I am not very skilled in PLSQL so I am not sure if my code would give the expected result. If i run this code i get a "SQL statement ignored" on line 3.
BEGIN
IF (:PARTYPE = 1) THEN
SELECT * FROM x
WHERE to_date(date) >= (Select to_date(sysdate)from DNV.dual)
ELSE
SELECT * FROM x
WHERE to_date(date) <= (Select to_date(sysdate)from DNV.dual)
END IF;
END;
This is just a example of my SELECT statement. Later this statement will become longer and more complex but I think this shows which results I am trying to get.
Below is a copy of my entire code but because I am not allowed to show this it has become very unreadable:
BEGIN
IF (:PARTYPE = 1) THEN
Select table1.Column1
, table1.Column2
, table1.Column3
, table1.Column4
, table1.Column5
, table1.Column6
, table1.Column7
, table1.Column8
, table1.Column9
, table1.Column10
, table1.Column11
, table1.Column12
, (Select table2.ColumnX From x2 table2 Where somthing) as "something" From x1 table1
WHERE to_date(date) >= (Select to_date(sysdate)from DNV.dual)
Order by columnX
ELSE
Select table1.Column1
, table1.Column2
, table1.Column3
, table1.Column4
, table1.Column5
, table1.Column6
, table1.Column7
, table1.Column8
, table1.Column9
, table1.Column10
, table1.Column11
, table1.Column12
, (Select table2.ColumnX From x2 table2 Where somthing) as "something" From x1 table1
WHERE to_date(date) <= (Select to_date(sysdate)from DNV.dual)
Order by columnX
END IF;
END;
I have created some new code with which i am trying to learn how a case statement works. This might help me with the code above. Unfortunately this code also doesn't work but I think it explanes my situation better. In this excample i use a separate table with data i made up. In some cases user2 is null but user1 is always filled. I want to get all items where user2 equals the parameter but if user2 is null and user1 does equal the paramter i still need that item to apear.
Select t1.user1,
t1.user2
From table t1
Where (Case
When t1.user2 IS NULL Then t1.user1 in (:PARUSER)
ELSE t1.user2 in (:PARUSER)
End Case)
Since the relational operator of the where clause depends on the partype, you cannot do the traditional CASE statement charm here. I'll have to resort with this one:
SELECT * FROM x
WHERE (to_date(date) >= (Select to_date(sysdate)from DNV.dual) AND :PARTYPE = 1)
OR (to_date(date) <= (Select to_date(sysdate)from DNV.dual) AND :PARTYPE != 1)
I have a table with 10,000 rows and I want to select the first 1000 rows and then select again and this time, the next set of rows, which is 1001-2001.
I am using the BETWEEN clause in order to select the range of values. I can also increment the values. Here is my code:
count = cursor.execute("select count(*) from casa4").fetchone()[0]
ctr = 1
ctr1 = 1000
str1 = ''
while ctr1 <= count:
sql = "SELECT AccountNo FROM ( \
SELECT AccountNo, ROW_NUMBER() OVER (ORDER BY Accountno) rownum \
FROM casa4 ) seq \
WHERE seq.rownum BETWEEN " + str(ctr) + " AND " + str(ctr1) + ""
ctr = ctr1 + 1
ctr1 = ctr1 + 1000
cursor.execute(sql)
sleep(2) #interval in printing of the rows.
for row in cursor:
str1 = str1 + '|'.join(map(str,row)) + '\n'
print "Records:" + str1 #var in storing the fetched rows from database.
print sql #prints the sql statement(str) and I can see that the var, ctr and ctr1 have incremented correctly. The way I want it.
What I want to achieve is using a messaging queue, RabbitMQ, I will send this rows to another database and I want to speed up the process. Selecting all and sending it to the queue returns an error.
The output of the code is that it returns 1-1000 rows correctly on the 1st but, on the 2nd loop, instead of 1001-2001 rows, it returns 1-2001 rows, 1-3001 and so on.. It always starts on 1.
I was able to recreate your issue with both pyodbc and pypyodbc. I also tried using
WITH seq (AccountNo, rownum) AS
(
SELECT AccountNo, ROW_NUMBER() OVER (ORDER BY Accountno) rownum
FROM casa4
)
SELECT AccountNo FROM seq
WHERE rownum BETWEEN 11 AND 20
When I run that in SSMS I just get rows 11 through 20, but when I run it from Python I get all the rows (starting from 1).
The following code does work using pyodbc. It uses a temporary table named #numbered, and might be helpful in your situation since your process looks like it would do all of its work using the same database connection:
import pyodbc
cnxn = pyodbc.connect("DSN=myDb_SQLEXPRESS")
crsr = cnxn.cursor()
sql = """\
CREATE TABLE #numbered (rownum INT PRIMARY KEY, AccountNo VARCHAR(10))
"""
crsr.execute(sql)
cnxn.commit()
sql = """\
INSERT INTO #numbered (rownum, AccountNo)
SELECT
ROW_NUMBER() OVER (ORDER BY Accountno) AS rownum,
AccountNo
FROM casa4
"""
crsr.execute(sql)
cnxn.commit()
sql = "SELECT AccountNo FROM #numbered WHERE rownum BETWEEN ? AND ? ORDER BY rownum"
batchsize = 1000
ctr = 1
while True:
crsr.execute(sql, [ctr, ctr + batchsize - 1])
rows = crsr.fetchall()
if len(rows) == 0:
break
print("-----")
for row in rows:
print(row)
ctr += batchsize
cnxn.close()
I'm trying to execute a raw sql string using my custom Connection class..
(The Execute method simply executes the given sql statement..)
MyConnection.Execute(
"DECLARE #i int "
"SET #i = 0 "
"WHILE (#i < 10000) BEGIN "
"INSERT INTO my_table VALUES (1, 2, 3) "
"SET #i = #i + 1");
Running this statement gives me a syntax error:
Code = 80040e14
Code meaning = I
Source = ASEOLEDB
Description = [42000]
[ASEOLEDB] Incorrect syntax near '1'.
As you can see, I'm connecting to Sybase Adaptive Server Enterprise, which accepts T-SQL..
I'm assuming there is something wrong with my sql string,, or with how it's formatted?
Thanks
PS I'm sure there's nothing wrong with my "Execute" method, as a single line delete statement works fine..
i found this example in the web
there is no END at your code and no ; but the ';', i think, you don't need.
DECLARE #MyCounter int;
SET #MyCounter = 0;
WHILE (#MyCounter < 26)
BEGIN;
INSERT INTO TestTable VALUES (#MyCounter, CHAR( ( #MyCounter + ASCII('a') ) ) );
SET #MyCounter = #MyCounter + 1;
END;
BR
Alex