Oracle 10g declare number field > 2000 - oracle10g

Hy folks,
I need to declare a number field on a oracle database that needs to start on 2000.
How can I do it?
Thanks!

I'm not sure if you mean that it must be greater than 2000 or "start on 2000", but anyway:
CREATE TABLE mytable (
mynumbercolumn NUMBER,
CONSTRAINT min2000 CHECK (mynumbercolumn >= 2000)
);

You want to declare a sequence that starts at 2000?
CREATE SEQUENCE yoursequencename
MINVALUE 2000
START WITH 2000
INCREMENT BY 1;
Skeleton syntax is
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;

Related

how to change postgresql sequence maximum value

I have following sequence created
CREATE SEQUENCE public.test_seq
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1
NO CYCLE;
once it is created, i want to change the maximum value ?
do i need to alter the name and create a same name sequence with maximum value ?
Try the following command:
alter sequence test_seq maxvalue 99999;

increment sequence in PostgreSQL stored procedure

How to auto increment sequence number once for every run of a stored procedure and how to use it in the where condition of an update statement?
I already assigned a sequence number to the next value in each run, but I'm not able to use it in the where condition.
CREATE OR REPLACE FUNCTION ops.mon_connect_easy()
RETURNS void
LANGUAGE plpgsql
AS $function$
declare
_inserted_rows bigint = 0;
sql_run bigint = 0;
--assigning the sequence number to the variable
select nextval('ops.mon_connecteasy_seq') into run_seq_num;
-- use for selection iteration_id. this is hwere I'm getting stuck
update t_contract c
set end_date = ce.correct_end_date, status='Active',
orig_end_date =ce.correct_end_date
from ops.t_mon_ConnectEasy ce
where c.contract_id = ce.contract_id
and run_seq_num = ??;
nextval() advances the sequence automatically before returning the resulting value. You don't need anything extra. Just use the function in your query directly:
update t_contract c
set end_date = ce.correct_end_date
, status = 'Active'
, orig_end_date = ce.correct_end_date
from ops.t_mon_ConnectEasy ce
where c.contract_id = ce.contract_id
and iteration_id = nextval('ops.mon_connecteasy_seq');
Be aware that concurrent transactions also might advance the sequence, creating virtual gaps in the sequential numbers.
And I have a nagging suspicion that this might not be the best way to achieve your undisclosed goals.

PostgreSQL: Constant for smallint maximum value?

Does PostgreSQL have a constant (like MAXFLOAT in Objective-C) for the maximum value that a smallint can be?
I know the PostgreSQL numeric types documentation says it's 32767, but I'd rather use a constant than hard coding a value that could change.
I'm using this number to prevent an error when incrementing a smallint, e.g.:
UPDATE populations
SET count = count + 1
WHERE city_id = 3
AND count < 32767;
Create it:
create function MAX_SMALLINT() returns smallint immutable language sql as '
select 32767::smallint;
';
Use it:
UPDATE populations
SET count = count + 1
WHERE city_id = 3
AND count < MAX_SMALLINT();
In extension of Neil's suggestion, you could use this:
create function MAX_SMALLINT() returns smallint immutable language sql as '
select ((1 << ((pg_column_size(1::smallint) << 3) - 1)) - 1)::smallint;
';
But honestly, I don't believe smallint will ever be anything else than 2 bytes in Postgres.

How can we use prefix as zero in SQL?

How can we use zero's in prefix of the number column feedback_refno like
select #refno = '0001'
i need to insert this value into that column feedback_refno but it is inserting like 1 only..but i need those prefix before those 1
I have tried like this
declare #refno int
select max(feedback_refno)+1 from EDK_Customer_Feedback(nolock)
if not exists(select feedback_refno from EDK_Customer_Feedback(nolock))
Begin
select #refno = '0001'
end
else
Begin
select #refno
End
insert into EDK_Customer_Feedback values(#refno)
I need the result like 0002 then 0003 like that but it is giving like 2 then 3..
Any suggestion?
try this
SELECT RIGHT('000'+ CONVERT(varchar,feedback_refno),4) AS NUM FROM EDK_Customer_Feedback;
#refnois of type int, so leading zeros wont work. If you change it to varchar(4) you can use these two answers:
In SQL Server 2000 how do you add 0's to beginning of number to fill nchar(n)
In SQL Server 2000 how do you add {n} number of 0's to a nchar(n)?

How to get min/max of two integers in Postgres/SQL?

How do I find the maximum (or minimum) of two integers in Postgres/SQL? One of the integers is not a column value.
I will give an example scenario:
I would like to subtract an integer from a column (in all rows), but the result should not be less than zero. So, to begin with, I have:
UPDATE my_table
SET my_column = my_column - 10;
But this can make some of the values negative. What I would like (in pseudo code) is:
UPDATE my_table
SET my_column = MAXIMUM(my_column - 10, 0);
Have a look at GREATEST and LEAST.
UPDATE my_table
SET my_column = GREATEST(my_column - 10, 0);
You want the inline sql case:
set my_column = case when my_column - 10 > 0 then my_column - 10 else 0 end
max() is an aggregate function and gets the maximum of a row of a result set.
Edit: oops, didn't know about greatest and least in postgres. Use that instead.