Sybase insert into temp table with identity column - sybase-ase15

I'm trying to insert records for couple of columns from a physical table into a temp table with customized IDENTITY. It creates the identity column (field name = idnum), but the values are 0 for all rows. I'm using below code. If anyone can help me what I'm doing wrong would be greatly appreciated.
Note: I'm trying this is Sybase ASE 15.7
SELECT
* INTO #achu_test
FROM (SELECT TOP 10
idnum = IDENTITY(8),
First_Name,
Last_Name
FROM Employees) myTable

My bad! I misplaced the IDENTITY. instead of using it before "* INTO", I used inside the Subquery.
SELECT idnum = IDENTITY(8),* INTO #achu_test
FROM (SELECT TOP 10 First_Name, Last_Name FROM Employees) myTable
A good sleep might have given the result for me :)

Related

select from the table name values after _ using oracle sql

Suppose if the table name is ABC_XYZ_123. I want to extract the integer values after _.
The output should be integer values after _.
In the above case, the output should be 123.
I have used the below sql query.
select from table_name like 'XXX_%';
But I am not getting required output. Can anyone help me with this query.
Thanks
Using REGEXP_SUBSTR with a capture group we can try:
SELECT REGEXP_SUBSTR(name, '_(\d+)$', 1, 1, NULL, 1)
FROM yourTable;
The question is somewhat unclear:
it looks as if you're looking for table names that contain number at the end, while
query you posted suggests that you're trying to select those numbers from one of table's columns
I'll stick to
Suppose if the table name is ABC_XYZ_123
If that's so, it is the data dictionary you'll query. USER_TABLES contains that information.
Let's create that table:
SQL> create table abc_xyz_123 (id number);
Table created.
Query selects numbers at the end of table names, for all my tables that end with numbers.
SQL> select table_name,
2 regexp_substr(table_name, '\d+$') result
3 from user_tables
4 where regexp_like(table_name, '\d+$');
TABLE_NAME RESULT
-------------------- ----------
TABLE1 1
TABLE2 2
restore_point-001 001
ABC_XYZ_123 123 --> here's your table
SQL>
Apparently, I have a few of them.

ORACLE SQL:Can someone explain me the difference between these two?

I'm a student and this is my first year of learning Oracle SQL. On the exam, I used this code:
SELECT department_id,MAX(salary)
FROM employees
GROUP BY department_id
HAVING INSTR(TO_CHAR(department_id),'5')!=1 AND MAX(salary)<1000;
and the professor said that I should be using something like
SELECT DEPARTMENT_ID, MAX(SALARY)
FROM EMPLOYEES
WHERE SUBSTR(TO_CHAR(DEPARTMENT_ID),1,1)<>'5'
GROUP BY DEPARTMENT_ID
HAVING MAX(SALARY) < 1000;
We got the same table so my question is when these two can display different results. I'm aware that data processing is different but as he said that was not the problem. The problem is not is using the INSTR function but not using WHERE.
The Where clause workn on the raw contents of the rows
so you filter the dataset that is evaluated for the select clause
WHERE SUBSTR(TO_CHAR(DEPARTMENT_ID),1,1)<>'5'
don't select the rows with
SUBSTR(TO_CHAR(DEPARTMENT_ID),1,1)='5'
so these rows are not used for
SELECT DEPARTMENT_ID, MAX(SALARY) ..GROUP BY DEPARTMENT_ID
HAVING work on the result of the selected result so also the rows with SUBSTR(TO_CHAR(DEPARTMENT_ID),1,1)='5' should be processed
in your case each value for the column DEPARTMENT_ID is always selected because is mentioned in group by and then both the query should return the same result

How to copy all constrains and data form one schema to another in oracle

I am using Toad for oracle 12c. I need to copy a table and data (40M) from one shcema to another (prod to test). However there is an unique key(not the PK for this table) called record_Id col which has something data like this 3.000*******19E15. About 2M rows has same numbers(I believe its because very large number) which are unique in prod. When I try to copy it violets the unique key of that col. I am using toad "export data to another schema" function to copy the data.
when I execute query in prod
select count(*) from table_name
OR
select count(distinct(record_id) from table_name
Both query gives the exact same numbers of data.
I don't have DBA permission. How do I copy all data without violating unique key of the table.
Thanks in advance!
You can use UPSERT for decisional INSERT or UPDATE or you may write small procedure for this.
you may consider to use NOT EXISTS, but your data is big and it might not be resource efficient.
insert into prod_tab
select * from other_tab t1 where NOT exists (
select 1 from prod_tab t2 where t1.id = t2.id
);
In Oracle you can use a MERGE query for that.
The following query proceeds as follows for each data row :
if the source record_id does not yet exist in the target table, a new record is inserted
else, the existing record is updated with source values
For the sake of the example, I assumed that there are two other columns in the table : column1 and column2.
MERGE INTO target_table t1
USING (SELECT * from source_table t2)
ON (t1.record_id = t2.record_id)
WHEN MATCHED THEN UPDATE SET
t1.column1 = t2.column1,
t1.column2 = t2.column2
WHEN NOT MATCHED THEN INSERT
(record_id, column1, column2) VALUES (t2.record_id, t2.column1, t2.column2)

CHAR_USED query returned '0 rows fetched from 1 column'

I created the following table:
Create table temp.test(c1 VARCHAR2(10 BYTE));
I was trying to use CHAR_USED to determine whether the column size is in BYTES or CHARS but all I am getting back is '0 rows fetched from 1 column'. The database version i am using is Oracle 11g. Does anyone have a clue as to why it is not return the semantic length information for this table?
The query used are as follows:
select CHAR_USED from all_tab_columns where table_name='temp.test'
select CHAR_USED from all_tab_columns where table_name='test' and owner = 'temp'
Assuming that you are not using case-sensitive identifiers (which you are not and should not), object names are stored in the data dictionary in upper case. So when you query a table like all_tab_columns, you'd need to use upper-case
SELECT column_name, char_used
FROM all_tab_columns
WHERE table_name = 'TEST'
AND owner = 'TEMP'

Oracle insert if not exists statement

insert into OPT (email, campaign_id) values('mom#cox.net',100)
where not exists( select * from OPT where (email ="mom#cox.net" and campaign_id =100)) ;
Error report: SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
how to insert a new row if it doesn't exists in Oracle?
insert into OPT (email, campaign_id)
select 'mom#cox.net',100
from dual
where not exists(select *
from OPT
where (email ='mom#cox.net' and campaign_id =100));
The correct way to insert something (in Oracle) based on another record already existing is by using the MERGE statement.
Please note that this question has already been answered here on SO:
oracle insert if row not exists
insert if not exists oracle
MERGE INTO OPT
USING
(SELECT 1 "one" FROM dual)
ON
(OPT.email= 'mom#cox.net' and OPT.campaign_id= 100)
WHEN NOT matched THEN
INSERT (email, campaign_id)
VALUES ('mom#cox.net',100)
;
insert into OPT (email, campaign_id)
select 'mom#coxnet' as email, 100 as campaign_id from dual MINUS
select email, campaign_id from OPT;
If there is already a record with mom#cox.net/100 in OPT, the MINUS will subtract this record from the select 'mom#coxnet' as email, 100 as campaign_id from dual record and nothing will be inserted. On the other hand, if there is no such record, the MINUS does not subract anything and the values mom#coxnet/100 will be inserted.
As p.marino has already pointed out, merge is probably the better (and more correct) solution for your problem as it is specifically designed to solve your task.
Another approach would be to leverage the INSERT ALL syntax from oracle,
INSERT ALL
INTO table1(email, campaign_id) VALUES (email, campaign_id)
WITH source_data AS
(SELECT 'mom#cox.net' email,100 campaign_id
FROM dual
UNION ALL
SELECT 'dad#cox.com' email,200 campaign_id
FROM dual)
SELECT email
,campaign_id
FROM source_data src
WHERE NOT EXISTS (SELECT 1
FROM table1 dest
WHERE src.email = dest.email
AND src.campaign_id = dest.campaign_id);
INSERT ALL also allow us to perform a conditional insert into multiple tables based on a sub query as source.
There are some really clean and nice examples are there to refer.
oracletutorial.com
oracle-base.com/

Resources