I cannot use values claus with subquery? - insert

I need this information for my task. I can not use alternative. I know that I can use subquery like this:
insert into (select id from test) (select employee_id FROM employees);
But this is not what I want. I tasked to finde the answer to this question. Please help me. Thanks everybody.

You should use like the below without using values clause in INSERT
Insert into test
select employee_id FROM employees;

Related

Hive Generating ID

I'm trying to generate unique id's for a table that was originally done in DB2 using the following:
insert into database.table
select next value for database.sequence,
current_timestamp,
from source
Where the sequence has a defined start value (e.g 25430).
The code I'm currently using is:
insert into database.table
select
row_number() over() + select max(id) from table,
from_unixtime(unix_timestamp())
from source;
Which is fine apart from the nested select statement not working, at the moment I have to run
select max(id) from table
and put it into the query manually.
Can anyone suggest a way to do this in the one query?
You have to force a crossjoin, something like this:
select
...
from source,
(select max(id)as maxid from table) as m_id
;
This way you get one value for your max id back, and you can use that to generate your new one.
Generating surrogate keys with hive is kind of painful, sadly enough.

Fill in the NOT NULL columns of a table while using the INSERT INTO statement

I'm working through a problem where I need to insert 135 rows into a recently created table with a select statement. I have a handful of NOT NULL constraints on that table and I don't understand how to alter my SELECT to insert the correct information.
Here's what I'm trying to do:
CREATE SEQUENCE target_table_s1 START WITH 1001;
INSERT INTO target_table(colA,ColB,ColC,ColD,ColE)
target_table_s1.NEXTVAL,
(SELECT (colB,colC,ColD)
FROM source_table),
colE;
Where colA is a sequence number (to provide a primary key for the target_table) and colE basically just needs to be something simple like SYSDATE.
Any suggestions on how I can make this work? I know that what I've written above isn't going to work but it's the best way I can illustrate what I'm trying to accomplish. Do I need to find a way to put my sequence inside the select statement so it follows the proper "INSERT INTO SELECT" format?
I think you should just be using the INSERT INTO ... SELECT construct here:
INSERT INTO target_table (colA,ColB,ColC,ColD,ColE)
SELECT target_table_s1.NEXTVAL, ColB, ColC, ColD, SYSDATE
FROM source_table
I assume above that you want to insert the SYSDATE into column E.

How make multiple subqueries with Eloquent?

A have this code:
DB::select('
SELECT SUM(count) as count
FROM (
SELECT COUNT(*) AS count FROM ad_banners
UNION ALL
SELECT COUNT(*) AS count FROM ad_context
UNION ALL
SELECT COUNT(*) AS count FROM ad_content
UNION ALL
SELECT COUNT(*) AS count FROM ad_decoration
UNION ALL
SELECT COUNT(*) AS count FROM ad_front
UNION ALL
SELECT COUNT(*) AS count FROM ad_universal
) as ad'
);
How make this query using Eloquent ORM methods and will this make sense? Thanks in advance!
EDIT:
I wrote this code, but my attempts to use Eloquent ended to fail. I don't understand how make sub queries with union and count. I didn't wont to write my code for me, it will be enough a small example. And sorry for my bad english.
Have you tried using the DB::raw() method? Sometimes, if you know the SQL you want to use but don't know how to use Eloquent to accomplish it, you may use DB::raw() to get the results you need. Here's an example, you can tailor it to your needs:
$query = DB::connection('connection_name')->select(DB::raw("SELECT * FROM table WHERE ..."));
Also, connection_name can be found as you defined it in app/config/database.php in the connections array.
Hope that provides some insight. Without an idea of your models and relationships and what you're trying to accomplish, it would be hard to advise you on an Eloquent solution to this issue.

How do I check if a sequence exists or not in Oracle 11g?

I am using Oracle 11g. I want to be able to determine whether a particular sequence exists or not. I have tried the code below but it is not working. (It is returning 0 as count value when there should be more):
SELECT COUNT(*)
FROM user_sequences
WHERE sequence_name = 'SCHEMA.SEQUENCE_NAME';
If anyone knows why this is, please help me.
If you are running the query as user MP then try it like this:
SELECT COUNT(*)
FROM user_sequences
WHERE sequence_name = 'SEQ_SSO_KEY_AUTHENTICATION';
else, try it like this:
SELECT COUNT(*)
FROM all_sequences
WHERE sequence_name = 'SEQ_SSO_KEY_AUTHENTICATION'
AND sequence_owner = 'MP' ;
Also, keep in mind that you may not be granted to see all sequences in DB.
In this case scripts provided above may not work, and you should run something like
SELECT COUNT(*) FROM DBA_SEQUENCES;
But this also may not work if you have no access to DBA_SEQUENCES view.
Check Oracle docs.
For newer/11g versions, the following query works. One suggestion, I couldn't initially make it work because I was using lowercase letters for sequence names. Making it all caps returned expected value
SELECT * FROM all_objects ao WHERE ao.owner = 'SCHEMA_NAME' AND ao.OBJECT_TYPE ='SEQUENCE'
AND ao.OBJECT_NAME ='CAPITAL_SEQ_NAME';

How do I find the definition of a named constraint in Oracle?

All I know about the constraint is it's name (SYS_C003415), but I want to see it's definition.
Looks like I should be querying ALL_CONSTRAINTS.
select OWNER, CONSTRAINT_NAME, CONSTRAINT_TYPE, TABLE_NAME, SEARCH_CONDITION from ALL_CONSTRAINTS where CONSTRAINT_NAME = 'SYS_C003415';
Another option would be to reverse engineer the DDL...
DBMS_METADATA.GET_DDL('CONSTRAINT', 'SYS_C003415')
Some examples here....
http://www.psoug.org/reference/dbms_metadata.html
Use following query to get a definition of constraint in oracle:
Select DBMS_METADATA.GET_DDL('CONSTRAINT', 'CONSTRAINT_NAME') from dual
Or to see all constaints use SYS.DBA_CONSTRAINTS (If you have the privileges)

Resources