I'm having a table like this
Create table test1(emp_id decimal(5,0), emp_name varchar(20));
Insert into test1(2015,'XYZ');
Insert into test1(2016,'XYZ2');
Now I want to update emp_id to row_number()
or
add new column into same table like (emp_no integer) to row_number().
can anyone please tell me the query for this?
You need to use UPDATE FROM:
UPDATE test1
FROM
( SELECT ROW_NUMBER() OVER (ORDER BY emp_id) AS rn,
emp_id
FROM test1
) AS src
SET emp_id = src.rn
WHERE test1.emp_id = src.emp_id -- must be unique column(s)
Btw, instead of updating all rows of a table it might be better to INSERT/SELECT or MERGE the SELECT into a new table. You must do it if there's no unique column, you should if emp_id is the PI of your table (otherwise performance will be horrible).
Create table test1(
emp_id decimal(5,0),
emp_name varchar(20),
emp_no INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1
INCREMENT BY 1
)
);
Insert into test1(2015,'XYZ1',2);
Insert into test1(2016,'XYZ2',2);
Insert into test1(2015,'XYZ3',null);
Insert into test1(2016,'XYZ4',null);
Related
When i am inserting fields from table T_EMP_MASTER into temp table , it's appearing in #temp_empdet as per the order of values of emp_id,emp_id2,emp_id3 .
I want emp_id,emp_id2,emp_id3 fields to to be inserted as per the order of insertion, like
1. emp_id
2. emp_id2
3. emp_id3
How can I do the same here , using same insertion with union or using anything else ?
CREATE TABLE #TEMP_EMPDET
(SN INT,EMP_ID INT)
INSERT INTO #TEMP_EMPDET
SELECT EMP_ID FROM T_EMP_MASTER WHERE CODE = #CODE
UNION
SELECT EMP_ID2 FROM T_EMP_MASTER WHERE CODE = #CODE
UNION
SELECT EMP_ID3 FROM T_EMP_MASTER WHERE CODE = #CODE
If you want to guarantee the order of insertion, use three separate statements:
INSERT INTO #TEMP_EMPDET (EMP_ID)
SELECT EMP_ID FROM T_EMP_MASTER WHERE CODE = #CODE;
INSERT INTO #TEMP_EMPDET (EMP_ID)
SELECT EMP_ID2 FROM T_EMP_MASTER WHERE CODE = #CODE;
INSERT INTO #TEMP_EMPDET (EMP_ID)
SELECT EMP_ID3 FROM T_EMP_MASTER WHERE CODE = #CODE;
Because SQL tables represent unordered sets, you will only see the "ordering" in an identity column or if EMP_ID is a clustered key.
CREATE TABLE #TEMP_EMPDET (
EMPDET_ID INT IDENTITY PRIMARY KEY,
SN INT,
EMP_ID INT
);
Your code is not inserting values in order specifically because of the UNION. That eliminates duplicates and re-arranges the data. If you want that functionality, things get a bit more complicated. I think I would recommend:
INSERT INTO #TEMP_EMPDET
SELECT v.EMP_ID
FROM T_EMP_MASTER EM CROSS APPLY
(VALUES (1, EMP_ID), (2, EMP_ID2), (3, EMP_ID3)
) V(WHICH, EMP_ID)
WHERE CODE = #CODE
GROUP BY v.EMP_ID
ORDER BY MIN(WHICH);
Suppose I am inserting the following queryset into a new table in SQLite:
CREATE TABLE queryset_cache AS
SELECT ROW_NUMBER() over () AS rowid, * FROM mytable ORDER BY product;
Is it possible to either:
Set the rowid as auto-incrementing PK in sqlite from the insert, or;
Exclude the rowid and have SQLite auto-add in an autoincrementing primary key for each inserted record.
How would this be done?
Currently, without that when I do the insert, the rowid is not indexed.
rowid is already there. You can just do:
CREATE TABLE queryset_cache AS
SELECT t.*
FROM mytable t
ORDER BY product;
You will see it if you do:
SELECT rowid, t.*
FROM queryset_cache;
Here is a db<>fiddle
Auo increment should solve this. Documentation here:
https://www.sqlite.org/autoinc.html
Create source table:
create table sourceTable (oldID integer, data TEXT);
Add source data:
insert into sourceTable values(7, "x");
insert into sourceTable values(8, "y");
insert into sourceTable values(9, "z");
Create target table with auto-increment:
create table target(newID INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);
Move data from source to target:
insert into target select null, data from sourceTable
If we have a table like:
create table employee (empID integer, name text , address text);
insert data into this table.
create a table in which you want to insert employee table data:
create table newEmployee (newempID integer PRIMARY KEY, name text , address text);
copy data to newEmployee table:
insert into newEmployee select * from employee
(select * from employee) to copy all the columns
This question has already been asked several times but the solution is not working for me. I don't know why.
Actually i am trying to create a temp table in sql query where i am inserting some records in temp table using select into but everytime it returns empty row:
here is what i am trying:
Create Table #TempTable
(
EntityID BIGINT
)
INSERT INTO #TempTable (EntityID)
SELECT pkEntityID FROM Employee WHERE EmpID = 45
Select * from #TempTable
Corresponding to 45 , there are 10 rows in Employee table. IS it like I have to do something else or a loop like structure here as we can only insert one row in a table at once?
This has been stated in the comments, all of which i up-voted, but to answer your question... there isn't anything else you have to do. There clearly isn't an EmpID = 45 in your source table. Here's a reproducible example:
Declare #Employee Table (pkEntityID bigint, EmpID int)
insert into #Employee (pkEntityID, EmpID)
values
(32168123,45),
(89746541,45),
(55566331,45),
(45649224,12)
Create Table #TempTable
(
EntityID BIGINT
)
INSERT INTO #TempTable (EntityID)
SELECT pkEntityID FROM #Employee WHERE EmpID = 45
Select * from #TempTable
drop table #TempTable
Have you accidentally also created the Employee table in the master database and you are currently connected to the master database?
Is there a way to select only one column from other table in a single SQL statement without using Alter again after creating a table with new columns
like below but its just for understanding
create table compliance_rules_filter_groups
( group_id int,
group_name varchar(50),
rule_id int (select rule_id from compliance_rules),
m_group_filter_logical_condition varchar(10)
)
You are creating table column rule_id as int not, inserting data in this statement. You can use rule_id from compliance_rules table while you are going to insert data to compliance_rules_filter_groups table.
While inserting you may do as following or INNER JOIN could be used depends on your table structure. But after all main thing is, you can't insert data with CREATE statement.
INSERT INTO compliance_rules_filter_groups(group_id, group_name, rule_id, m_group_filter_logical_condition)
SELECT 1, '', (select rule_id from compliance_rules), ''
I found an old table without a primary key, and in order to add one, I have to add a new column and fill it with sequence values. I have another column which contains the time of when the record was created, so I want to insert the sequence values to the table sorted by the column with the time.
I'm not sure how to do it. I tried using PL\SQL - I created a cursor for a query that returns the table with an ORDER BY, and then update for each record the cursor returns but it didn't work.
Is there a smart working way to do this?
Thanks in advance.
Another option is just to use a correlated subquery, with the wrinkle of a nested subquery to generate the row number. Setting up some sample data:
create table t42 (datefield date);
insert into t42 (datefield) values (sysdate - 7);
insert into t42 (datefield) values (sysdate + 6);
insert into t42 (datefield) values (sysdate - 5);
insert into t42 (datefield) values (sysdate + 4);
insert into t42 (datefield) values (sysdate - 3);
insert into t42 (datefield) values (sysdate + 2);
select * from t42;
DATEFIELD
---------
12-JUL-12
25-JUL-12
14-JUL-12
23-JUL-12
16-JUL-12
21-JUL-12
Then adding and populating the new column:
alter table t42 add (id number);
update t42 t1 set t1.id = (
select rn from (
select rowid, row_number() over (order by datefield) as rn
from t42
) t2
where t2.rowid = t1.rowid
);
select * from t42 order by id;
DATEFIELD ID
--------- ----------
12-JUL-12 1
14-JUL-12 2
16-JUL-12 3
21-JUL-12 4
23-JUL-12 5
25-JUL-12 6
Since this is a synthetic key, making it match the order of another column seems a bit pointless, but I guess doesn't do any harm.
To complete the task:
alter table t42 modify id not null;
alter table t42 add constraint t42_pk primary key (id);
First of all, create new field and allow null values.
Then, update field from other table or query. Best approach is to use merge statement.
Here a sample from documentation:
MERGE INTO bonuses D
USING (SELECT employee_id, salary, department_id FROM employees
WHERE department_id = 80) S
ON (D.employee_id = S.employee_id)
WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01
DELETE WHERE (S.salary > 8000)
WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus)
VALUES (S.employee_id, S.salary*.01)
WHERE (S.salary <= 8000);
Finally, set as non null this new field and promote it to primary key.
Here sample sentences:
ALTER TABLE
customer
MODIFY
(
your_new_field varchar2(100) not null
)
;
ALTER TABLE
customer
ADD CONSTRAINT customer_pk PRIMARY KEY (your_new_field)
;
One simple way is to create a new table, with new column an all other columns:
create table newt (
newtID int primary key not null,
. . .
)
Then insert all the old data into it:
insert into newt
select row_number() over (order by <CreatedAt>), t.*
from t
(You can substitute all the columns in, instead of using "*". Having the columns by name is the better practice. This is shorter, plus, I don't know the column names.)
If you alter the table to add the column, then the column will appear at the end. I find that quite awkward for the primary key. If you do that, though, you can update it as:
with t as (select row_number() over (order by <CreatedAt>) as seqnum, t.*
from t
)
update t
set newtID = seqnum