I have tried to insert data with simple insert statement but it is not increasing auto increment field ID.
My Query is
Insert into [Product]([Name],[Category]) values('abc','xyz')
and my table structure is:
Mstr_Product Id number AutoNumber Name Text Category Text
I have found solution
Insert into Product select 'abc','xyz'
It will automatically increase auto ID
Related
I have a table in MS access having more than 30 thousand rows.But in this table some values are duplicate so i delete duplicate rows and during this process autonumber fields also delete.Now i want to reset existing autonumber values using any query or vba code.
I use below mentioned query for reset values but query starts next value from one not resetting existing values.
ALTER TABLE [urlcheck] ALTER COLUMN [ID] COUNTER(1,1);
Currently auto number field values are just like this 1,2,5,7,9 now i want to reset values in this format 1,2,3,4,5
I'm trying to insert a Number as an ID in a column in my database by a loop which sets the new ID by a Sequence. When I try to insert it, the row hasn't been created or the field where my number should be is null.
This only appears when I'm trying to insert the value of the current number of the sequence. Any other number can be inserted. The id isn't a primary key or foreign key, just a normal field. I have tried to insert it in a few different methods like to select it from a field or to hardcode the value in the insert script. If its hardcoded with, the value of the sid, no row is inserted.
insert into SCHULP_BEZIEHUNG (R_ID, B_ID,S_ID,WDH,HOTEL)
values(
SEQ_SCHULP_ID.NEXTVAL
,l_selected(i)
,(select distinct SSID from SCHULP_SCHULUNGEN where SID = :P2_ID and SSID is not null)
,5
,'test');
My expected result is that it inserts the nummber of SID in the column S_ID
Images of the Database:
What is this:
This only appears when I'm trying to insert the value of the current number of the sequence.
related to? If seq_schulp_id.nextval which - in your attempt - was actually seq_schulp_id.currval, then yes, it won't work until you fetch nextval first because currval isn't yet defined within the current session.
Another objection goes to :P2_ID which is an Apex page item. If you want to be able to use it, it isn't enough that you see it on the screen - it has to be put into session state. The simplest way to do that is to submit the page first, then run the process which will insert data.
On the other hand, you said that you hardcoded value (which value? :P2_ID?) but you still didn't manage to insert value into S_ID column. It means that the whole query:
select distinct SSID from SCHULP_SCHULUNGEN where SID = :P2_ID and SSID is not null
returned nothing.
You posted incomplete code (what is l_selected(i)?); I can't tell whether there's something else that might be wrong.
I have this table:
Table1:
id text
1 lala
And i want take first row and copy it, but the id 1 change to 2.
Can you help me with this problem?
A SQL table has no concept of "first" row. You can however select a row based on its characteristics. So, the following would work:
insert into Table1(id, text)
select 2, text
from Table1
where id = 1;
As another note, when creating the table, you can have the id column be auto-incremented. The syntax varies from database to database. If id were auto-incremented, then you could just do:
insert into Table1(text)
select text
from Table1
where id = 1;
And you would be confident that the new row would have a unique id.
Kate - Gordon's answer is technically correct. However, I would like to know more about why you want to do this.
If you're intent is to have the field increment with the insertion of each new row, manually setting the id column value isn't a great idea - it becomes very easy for there to be a conflict with two rows attempting to use the same id at the same time.
I would recommend using an IDENTITY field for this (MS SQL Server -- use an AUTO_INCREMENT field in MySQL). You could then do the insert as follows:
INSERT INTO Table1 (text)
SELECT text
FROM Table1
WHERE id = 1
SQL Server would automatically assign a new, unique value to the id field.
I have a large access table . I need to have a column which will have value as row number
how can I write an sql query fill an empty column inserted by me with row numbers
Write a sequence for your table . Then have default value as sequence .
So, whenever the column does not get users values , it will take the sequence values.
Im sorry, im a little confused at what you're getting at, but could you not just do an alter table?
ALTER TABLE my_table
ADD row_number int;
Make the Field DataType AutoNumber it will do it for you automatically.
I want to create a table in sql server and fill it up with data (people's info) every person should have a unique ID different than the auto incremented ID's by sql server
For example i need the ID for the first person inserted like this: 2016xxxx
how to fix the 2016 and randomly generate the numbers after that to be filled instead of xxxx
should i use a regular expression ?
You can also create a computed column like below
CREATE TABLE tableName
(
PkAutoId INT PRIMARY KEY IDENTITY(1,1),
PersonUniqueNo AS (CAST(DATEPART(YEAR,GETDATE()) AS VARCHAR) + RIGHT(RIGHT(CAST(RAND() AS VARCHAR),4) + CAST(PkAutoId AS VARCHAR),4))
)
Computed Column "PersonUniqueNo" is 8 Digit Unique Number comprising of Current Year And Conceited value of Random number and Primary Key Id for 4 Length, Total length will be 8 as asked.
You could create a function that would get the next value for you and use that instead of an AUTO_INCREMENT field.
I wouldn't recommend it tho. You shouldn't format the data like that before inserting it. That sort of thing should be done on the way out, preferably by the front-end code. Or you can just write a query and create a view ...
However if you must do that here is the complete answer with the code:
Is there a way to insert an auto-incremental primary id with a prefix in mysql database?