How to get last inserted value of a Specific column in SQL? - sql

Is there any way where we could us IDENT_CURRENT() to retrieve a specific value from a specific column

We will consider that you need to retrieve product_code from product table-
SELECT LAST (product_code) FROM product;
Or else you can try this
SELECT Ident_Current(product_code) AS Alias from product;

according to https://msdn.microsoft.com/en-us/library/ms175098.aspx
You could use
SELECT Ident_Current('column_name') AS Alias;
For example if you are trying to retrieve a product Id from your company_product table you could do:
SELECT IDENT_CURRENT('company_product.comp_prod_id') as CURRENT_PRODUCT_ID;
This would give you the last entered primary key if your primary key column name was comp_prod_id and it would set the alias CURRENT_PRODUCT_ID to your result
The correct format might be
SELECT IDENT_CURRENT ('tablename.columnname');
But I am not sure as I do not have anything to test it on at my disposal

Related

SQL Server database - get last record

How do I we get last row in SQL Server if we don't have primary id and numeric column? For example we only have a name and a few other columns in the table
If you mean the highest name, alphabetically,
select max(columnName) from tableName
If you mean the last row added to the table, it's not possible unless you have a column with the date/time inserted, or some other value like an identity column.
Maybe if you have a column for insertedDate you could do something like this:
select top (1) * from tableName order by insertedDate DESC
If not, for future use if you don't want an ID column, maybe you can add a default constraint for inserted date to pick from the system date, so you wouldn't have to add anything when you insert.
With no definition of 'last row'... :
select top 1 *
from t
order by Name desc

update rows one by one with max value + 1 in SQL

here is my situation,
I have 2 tables,
1st table has all records, and it has IDs
2nd table has new records and it doesnt have ID, yet.
I want to generate ID for 2nd table with max(id) + 1 from 1st table.
when i do this, it makes all rows same id number, but i want to make it unique increment number.
e.g
select max(id) from table1 then it gives '997040'
I want to make second table rows like;
id
997041
997042
997043
997044
i think i need to use cursor or whileloop, or both, but i could not create the actual query.
sorry about bad explanation, i am so confused now
Use ROWNUM to generate incrementing row numbers. E.g.:
SELECT someConstant + ROWNUM FROM source.
CREATE TABLE table_name
(
ID int IDENTITY(997041,1) PRIMARY KEY
)
I hope this sql query would work!!
Or refer http://www.w3schools.com/sql/sql_autoincrement.asp

Select last value in a specific column? (PostgreSQL)

Running into some issues when trying to retrieve the last value in a specific column, from a table and assign it into a variable.
Looking for the last int in a column "id" that is a primary key basically.
So I have a variable like "lastValue" in a select statement like :
select last(id) into lastValue from test_table
Not sure on an exact, or best way to accomplish this.
(on mobile, please forgive formatting)
A typical way to solve this is with order by and limit:
select id
from test_table
order by id desc
limit 1;
Of course, in this case, you could simply use:
select max(id)
from test_table;
But the first method allows you to choose whichever variables you want from the row with the maximum value.

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.

Concatenate value to generated unique ID

I have an existing table named Employee and have to add a new column Employee_Id.
EmployeeId should be 10 digit unique number and always have to start with 10.
For example
1000000000
1000000001
1023456789
So I need to add bunch of unique 10 digit Id's to an existing table which already has data.
Can anyone help me sort this out.
SELECT 1000000000 + ROW_NUMBER() OVER( ORDER BY YourColumn ) AS 'rownumber',*
FROM Employee
This will get the data from the existing table, with an extra column for the new id.
If you are using SQL Server, IDENTITY would help you.
http://msdn.microsoft.com/en-us/library/ms186775.aspx
Update: You should see the discussion here in order to alter table.
Adding an identity to an existing column
If you already have and integer PK, you may add a calculated column (persisted or not). It value will be PK + 1000000000, provided no pk is bigger than 1000000000.
ALTER TABLE YOURTABLE
ADD COLUMNNAME INTEGER IDENTITY(1000000000, 1)
It will not generate all with 10.. but it will generate al the possibilities, if it get passed you will get 11.. but that would be a lot of numbers to reach that.
Hope it helps