Select 'S' Size from INFORMATION_SCHEMA.SYSTEM_USERS;
or
Select 'S' Size from INFORMATION_SCHEMA.SYSTEM_USERS WHERE 1 = 0;
BOTH query, resulted in only Size label is printed:
SIZE
====
My desired result for dummy table:
SIZE
====
S
Could anyone advice on this?
p.s I'm running on old version HSQLDB (can't upgrade, office environment)
With any database, including HSQLDB, your query will return an empty result. The condition WHERE 1 = 0 is always false, so any row that exists will fail the test.
Try without the condition, and add a LIMIT to guarantee no more than one row is returned.
Select 'S' Size from INFORMATION_SCHEMA.SYSTEM_USERS LIMIT 1;
If you cannot get the right result from the INFORMATION_SCHEMA.SYSTEM_USERS, then create a table and insert only one row in that table. You can then select from the new table.
create table myTable (id int)
insert into myTable values 1
select 'S' Size from MYTABLE
Related
I am attempting to move some code from c# to a sql statement. The purpose is speed and is warranted. The objective is to get the id of a select if it is found and do an update to it.
If it is not found, then try another select query and if that is found update that record. If THAT is not found then insert a record and use the id of that inserted record in an update to that id record
if
select id from table where field1 = "testcase" has a result then update the record andreturn
else
select id from table where field2 = "othercase" if there is a result then update the record and return
else --no record found
update table field3 = "xx" where id = insert into recorddata values
Agree with comment: You need to do a little light reading before asking for your home work to be done ;)
Read up on CASE in particular - clue in your own question "testcase","othercase".
Incidentally, SQL is a "declarative" language so you need to change your mindset from C# when programming with it - this will help going forward if you have a lot to do in SQL
Virtually exactly what you are looking for here..
https://www.sqlitetutorial.net/sqlite-case/
CASE case_expression
WHEN when_expression_1 THEN result_1
WHEN when_expression_2 THEN result_2
...
[ ELSE result_else ]
END
There is no if then else construct in SQLite, the equivalent is CASE WHEN THEN ELSE END. However, you can only undertake one statement as such at a time, although such statements can be quite complex.
I think that the following is along the lines of what you want but it is unclear what update the record means nor what insert a record means.
DROP TABLE IF EXISTS thetable;
CREATE TABLE IF NOT EXISTS thetable (id INTEGER PRIMARY KEY, field1, field2, field3);
INSERT INTO thetable (field1,field2,field3) VALUES('testcase','othercase','aa'),('nottestcase','notothercase','bb'),('nottestcase','othercase','cc'),('testcase','notothercase','dd');
SELECT * FROM thetable;
INSERT INTO theTable SELECT null,field1,field2,null FROM thetable WHERE NOT (field1 = 'testcase' OR field2 = 'othercase');
UPDATE theTable SET field3 = (SELECT id FROM thetable WHERE field3 IS NULL) WHERE (NOT (field1 = 'testcase' OR field2 = 'othercase')) AND field3 IS NOT NULL;
UPDATE theTable SET field3 = 'mynewfield3value' WHERE field1='testcase' OR field2 = 'othercase';
SELECT * FROM thetable;
DROP TABLE IF EXISTS thetable; /* cleanup test environment*/
The first 3 statements create the environment i.e. a populated table.
The 4th statement shows the table before any changes.
The 5th statement inserts a new row IF there is a row (or rows) for the --no record found condition.
As the question is vague about what is inserted the values of field1 and field 2 or copied from the --no record row, the id (assuming that id is a typical id that is autogenereated) is autogenerated, field3 is NULL (this being used to distinguish the newly added row).
If there are no --no record's then nothing will be inserted.
The 6th statement updates the other --found rows BUT NOT the newly inserted --wasn't even there row.
This row could easily be changed is field3 being NULL is easily selected.
The 7th shows the table after the changes.
The 8th cleans up the environment (i.e. drops the table)
Result 1
The highlighted row is the --no record row.
Result 2
A new row with an id of 5 has been added.
The --no record row has been updated with the id of the new row.
Rows 1,3 and 4 (the --record found rows) have been updated.
It is unclear what should happen to the new row.
CURSOR text IS
SELECT *
FROM DATA
CURSOR MATCHES IS
SELECT NAME
FROM DATA
INTERSECT
SELECT DESCRIPTION
FROM my_table
BEGIN
FOR i IN text
OPEN MATCHES
FETCH MATCHES INTO MATCH
CLOSE MATCH
IF i IN MATCH
THEN
UPDATE my_table
SET col1 = correlating_new_column1, col2 = correlating_new_column2, col3 = correlating_new_column3
WHERE table_im_trying_to_populate.code = my_seq.curval
ELSE
INSERT INTO TABLE_IM_TRYING_TO_POPULATE(CODE, NAME, DESCRIPTION, col1, col2, col3)
VALUES(my_seq.nextval, other_name, other_description, correlating_new_column1, correlating_new_column2, correlating_new_column3)
END IF;
END LOOP;
Basically I am trying to take an explicit cursor I made that is a select statement of a table and then do a line by line loop of that and put it into my other existing table. If it comes across a name in my other exisiting table it will update some of the columns. Else it inserts the whole record into that table. I am attempting to use sequence to update the 'code' column so that it updates where the code from the other existing table = my_seq.curval. Then for the inset it just goes to the next val. I know this is complicated but Im really just trying to see if I have the setup correct. Just started using sql developer for oracle not to long ago.
There are a huge number of problems with your code. However, it looks like what you're trying to do can be achieved with a single MERGE statement, along the lines of:
merge into table_im_trying_to_populate tgt
using data_table src
on (tgt.name = src.other_name
and tgt.description = src.other_description)
when matched then
update set tgt.col1 = src.correlating_new_column1,
tgt.col2 = src.correlating_new_column2,
tgt.col3 = src.correlating_new_column3
when not matched then
insert (tgt.code, tgt.name, tgt.description, tgt.col1, tgt.col2, tgt.col3)
values (my_seq.nextval, src.other_name, src.other_description, src.correlating_new_column1, src.correlating_new_column2, src.correlating_new_column3);
This assumes that the other_name and other_description columns in the data table is unique. I've also had to guess at what the join condition should be, since the join condition you had in your example update statement (table_im_trying_to_populate.code = my_seq.currval) didn't make any sense - you don't use currval to join against as a general rule, since it isn't populated unless you've previously pulled a value from the sequence in the same session.
If this doesn't match what you're trying to do, please update your question with some sample data in both tables and the expected output, and we should be able to help you further.
I am trying to get the current date into a Hive database (version 0.13 running on an HDInsight cluster) with the following script
SET curdt = from_unixtime(unix_timestamp());
DROP TABLE IF EXISTS curtime_test;
CREATE TABLE curtime_test (
dateEntered STRING
);
INSERT INTO TABLE curtime_test
SELECT '${hivevar:curdt}' FROM hivesampletable limit 3;
SELECT * FROM curtime_test;
Note that I want to have the same insert date for all the inserted records, this is a toy example, but the real one I want to use it on has millions of records to insert. This version I tried above just inserts the string '${hivevar:curdt}' into the database, which is not what I want:
${hivevar:curdt}
${hivevar:curdt}
${hivevar:curdt}
Omitting the quotes causes the insert to error out because of the spaces in the string. How can I do this right?
Update:
Using the line
SELECT ${hiveconf:curdt} FROM hivesampletable limit 3;
as per the comment from Charlie Haley (I mixed up ${hivevar} and ${hiveconf}), gives me the results that I want. If he writes it up as an answer I will mark it as right.
The following code sample works for me. Does this solve your problem?
DROP TABLE IF EXISTS curtime_test;
CREATE TABLE curtime_test (
dateEntered STRING
);
INSERT INTO TABLE curtime_test
SELECT unix_timestamp() FROM hivesampletable limit 1;
SELECT * FROM curtime_test;
I want to write a query which updates only one row of a table then returns updated rows.
I can achieve getting returned rows using
select field from final table
(update tablename set anotherfield = 'dd' where someanotherfield = 'bb')
kind of statement. But i cannot update just one row.
Also my program that calls this query is a multithreaded one and i dont want to deadlock any processes so, i found SKIP LOCKED DATA statement which is like readpast in t-sql query.
So what i am trying to do is, my program uses an db2 table like a stack, selects only one row each time, updates it so any other thread cannot access it but does not make them wait
entire table, they just skip updated row and select next record from table. Is this operation possible in a single db2 query?
I have written this query so far, and it cannot be processed
select COLUMN3 FROM FINAL TABLE
(
update MYTABLE
set COLUMN1 = 'R'
where COLUMN1 = ''
order by COLUMN2
FETCH FIRST 1 ROW ONLY
SKIP LOCKED DATA
)
Any help would be appreciated,
Thanks.
Assuming that COLUMN2 is a unique_id , you could do something like this:
select COLUMN3 FROM FINAL TABLE
(
update MYTABLE
set COLUMN1 = 'R'
where COLUMN1 = ''
AND COLUMN2 = ( SELECT COLUMN2
FROM MYTABLE
ORDER by COLUMN2
FETCH FIRST 1 ROW ONLY)
SKIP LOCKED DATA
)
You were doing an order by inside your update and that doesn't work. You have to restrain that to only one result (likely using your primary key).
I have a table which I dynamically fill with some data I want to create some statistics for. I have one value which has some values following a certain pattern, so I created an additional column where I map the values to other values so I can group them.
Now before I run my statistics, I need to check if I have to remap these values which means that I have to check if there are null values in that column.
I can do a select like this:
select distinct 1
from my-table t
where t.status_rd is not null
;
The disadvantage is, that this returns exactly one row, but it has to perform a full select. Is there some way that I can stop the select for the first encounter? I'm not interested in the exact row, because when there is at least one row, I have to run an update on all of them anyway, but I would like to avoid running the update unnecessarily everytime.
In Oracle I would do it with rownum, but this doesn't exist in SQLite
select 1
from my-table t
where t.status_rd is not null
and rownum <= 1
;
Use LIMIT 1 to select the first row returned:
SELECT 1
FROM my_table t
WHERE t.status_rd IS NULL
LIMIT 1
Note: I changed the where clause from IS NOT NULL to IS NULL based on your problem description. This may or may not be correct.