Do we have Sequence in Sybase - sequence

I see tutorial talking about Sequence with syntax and example.
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1601/doc/html/saiq-create-sequence-statement.html
But when I try creating sequence in Syabse through Squirrel, I get below error.
Error: 'SEQUENCE' is not a recognized CREATE option.
SQLState: ZZZZZ
ErrorCode: 155
Then how do we create auto increment unique identifier in Sybase.
How do we use Identity in Sequence. How come sequence available in one Sybase version and not in another.
I connect to sybase using below dependency.
<dependency>
<groupId>com.sybase.jdbcx</groupId>
<artifactId>jconn3</artifactId>
<version>6.0</version>
</dependency>

A sequence as an object does not exist in Sybase ASE (assumption). The equivalent functionality is done using identity columns.
create table test_tab (
test_tab_id int identity,
test_tab_name varchar(30) not null )
go
insert into test_tab (test_tab_name) values ('Hello')
go
insert into test_tab (test_tab_name) values ('World')
go
select * from test_tab
go
Result
(1 row affected)
(1 row affected)
test_tab_id test_tab_name
1 Hello
2 World
(2 rows affected)

One more point to add on identity is:
Once you truncate the table and insert data the old value of the identity is taken and then incremented.
Example:
select * from test_tab
go
2 rows
1 hello
2 world
Truncate the table test_tab
go
insert into test_tab (test_tab_name) values ('Hai')
go
insert into test_tab (test_tab_name) values ('done')
go
select * from test_tab
go
You will get as below
3 hai
4 done

can we assign the sequence per combination
E.g.
1 ABC
2 ABC
3 ABC
4 ABC
1 KLO
2 KLO
3 KLO

Related

Query to combine value of another column value in insert command of same table

I'm really sorry if you don't understand my question, my english is not good .
kindly correct it if can
I create a table called 'Class'
create table class(
candidate_id int identity,
candidate_name varchar(50),
candidate_course varchar(15) default 'SQL Server');
I just want to insert only candidate_name column
Insert into class(Candidate_name)
values('User');
If I execute this command i'll get
Candidate_id
Candidate_name
Candidate_course
1
user
SQL Server
As, identity column will generate no.s so is it possible combine both with candidate_name while inserting .
Just like other languages Print("Hello world "+S) where s='Stack' output would be
Hello world Stack
Expecting output like this
Candidate_id
Candidate_name
Candidate_course
1
user1
SQL Server
2
user2
SQL Server
3
user3
SQL Server
nth
user nth
SQL Server
kindly help . Hope i'm clear
You can concatenate those 2 values simply using '+'.
Also, we need to convert the candidate_id to varchar, since it is integer.
Query
select candidate_id,
Candiate_name + cast(candidate_id as varchar(10)) as candidate_name,
Candidate_course
From class;
It is not possible to concatenate an identity with a user defined value provided in a raw insert statement. That being said there are a couple of things that you can do.
Define the table with a generated column.
You can actually define a column in your table that is generated from other columns.
create table class(
candidate_id int identity,
candidate_name varchar(50),
candidate_course varchar(15) default 'SQL Server',
candidate_name_id AS CONCAT(candidate_name, '_', candidate_id)
);
Insert into class(Candidate_name)
VALUES
('User'),
('User'),
('User');
SELECT *
FROM dbo.class AS c
This produces the following table.
candidate_id
candidate_name
candidate_course
candidate_name_id
1
User
SQL Server
User_1
2
User
SQL Server
User_2
3
User
SQL Server
User_3
For further reference on 'Computed Columns'
https://learn.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-ver16
Define The Table With A Sequence
Instead of using an identity you can use a sequence which gives you a lot more flexibility. You can generate the next value for the sequence and use it in your insert statements. I actually recommend this since you can generate the sequence before hand and use it in other parts of your code.
CREATE SEQUENCE SQ_class INCREMENT BY 1 START WITH 1 AS INT;
create table class(
candidate_id int DEFAULT NEXT VALUE FOR SQ_class,
candidate_name varchar(50),
candidate_course varchar(15) default 'SQL Server'
);
Insert into class(candidate_id, Candidate_name)
VALUES
(NEXT VALUE FOR SQ_class, CONCAT('User', '_', NEXT VALUE FOR SQ_class)),
(NEXT VALUE FOR SQ_class, CONCAT('User', '_', NEXT VALUE FOR SQ_class)),
(NEXT VALUE FOR SQ_class, CONCAT('User', '_', NEXT VALUE FOR SQ_class));
SELECT *
FROM dbo.class AS c
Which gives you the following result
candidate_id
candidate_name
candidate_course
1
User_1
SQL Server
2
User_2
SQL Server
3
User_3
SQL Server
in the same column you can achieve that, and as the other answer shows you can generate it on every sele as it is redundat, to have another column.
But you can make a generated column
MS SQL Server 2017 Schema Setup:
create table class(
candidate_id int identity,
candidate_name varchar(50),
fnew_candidate AS (candidate_name + cast(candidate_id as varchar(10))) PERSISTED,
candidate_course varchar(15) default 'SQL Server');
Insert into class(Candidate_name)
values('User');
SQL Fiddle
Query 1:
SELECT * FROM class
Results:
| candidate_id | candidate_name | fnew_candidate | candidate_course |
|--------------|----------------|----------------|------------------|
| 1 | User | User1 | SQL Server |
You may use a Trigger to update the inserted candidate_name automatically as the following:
CREATE TRIGGER ClassInsert ON Class
FOR INSERT AS
Begin
Update Class Set candidate_name = (candidate_name + Cast(candidate_id as nvarchar(5)))
Where candidate_id = (Select Max(candidate_id) From Class);
End
See a demo from db<>fiddle.

Usage of REGEXP_SIMILAR in Teradata

I'm working on Teradata and trying to use REGEXP_SIMILAR function.
*** Teradata Database Release is 14.10.03.10
*** Teradata Database Version is 14.10.03.06
Here's my sample data.
create table test_table(
test_col varchar(20)
);
insert into test_table values('lorem');
insert into test_table values('984kd');
insert into test_table values('ier7j');
insert into test_table values('34535');
insert into test_table values('lore9');
insert into test_table values(' 09sd');
I want to see the records which start with a number.
select test_col, regexp_similar(test_col, '^\d+','i')
from test_table;
test_col regexp_similar(test_col,'^\d+','i')
-------------------- -----------------------------------
lore9 0
lorem 0
09sd 0
ier7j 0
984kd 0
34535 1
But, the above query shows a match only for '34535' row and not for '984kd'. Seems like ^ character(also $) don't have the desired effect.
Isn't REGEXP_SIMILAR similar to REGEXP_LIKE of Oracle?
Can someone explain why is this happening and how to solve this.
^\d+.*
Try this.This will give the result.

SQL Server 2012 sequence

I create a table and sequence in order to replace identity in the table I use SQL Server 2012 Express but I get this error while I tried to insert data to the table
Msg 11719, Level 15, State 1, Line 2
NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns,
views, user-defined functions, user-defined aggregates, user-defined
table types, sub-queries, common table expressions, or derived
tables.
T-SQL code:
insert into Job_Update_Log(log_id, update_reason, jobid)
values((select next value for Job_Log_Update_SEQ),'grammer fixing',39);
This is my table:
create table Job_Update_Log
(
log_id int primary key ,
update_reason nvarchar(100) ,
update_date date default getdate(),
jobid bigint not null,
foreign key(jobid) references jobslist(jobid)
);
and this is my sequence:
CREATE SEQUENCE [dbo].[Job_Log_Update_SEQ]
AS [int]
START WITH 1
INCREMENT BY 1
NO CACHE
GO
Just get rid of the subselect in the VALUES section, like this:
insert into Job_Update_Log(log_id,update_reason,jobid)
values (next value for Job_Log_Update_SEQ,'grammer fixing',39);
Reference: http://msdn.microsoft.com/en-us/library/hh272694%28v=vs.103%29.aspx
Your insert syntax appears to be wrong. You are attempting to use a SELECT statement inside of the VALUES section of your query. If you want to use SELECT then you will use:
insert into Job_Update_Log(log_id,update_reason,jobid)
select next value for Job_Log_Update_SEQ,'grammer fixing',39;
See SQL Fiddle with Demo
I changed the syntax from INSERT INTO VALUES to INSERT INTO ... SELECT. I used this because you are selecting the next value of the sequence.
However, if you want to use the INSERT INTO.. VALUES, you will have to remove the SELECT from the query:
insert into Job_Update_Log(log_id,update_reason,jobid)
values(next value for Job_Log_Update_SEQ,'grammer fixing',39);
See SQL Fiddle with Demo
Both of these will INSERT the record into the table.
Try this one:
–With a table
create sequence idsequence
start with 1 increment by 3
create table Products_ext
(
id int,
Name varchar(50)
);
INSERT dbo.Products_ext (Id, Name)
VALUES (NEXT VALUE FOR dbo.idsequence, ‘ProductItem’);
select * from Products_ext;
/* If you run the above statement two types, you will get the following:-
1 ProductItem
4 ProductItem
*/
drop table Products_ext;
drop sequence idsequence;
------------------------------

How to create conditional unique constraint

Having a table:Table1 in which a column Code accepts nullables values how can we insure that values are unique for non nullable values except for codes that start with 'A' which can be duplicated maximum twice?
Table1
Id | Code
----------
1 | NULL --[ok]
2 | A123 --[ok]
3 | A123 --[ok]
4 | B100 --[ok]
5 | C200 --[ok]
6 | B100 --[not ok already used]
7 | NULL --[ok]
What i have tried is creating an indexed view, the solution work fine for NULL values but not for the second case i mentioned (skipped actualy)
Create view v_Table_unq with schemabinding as(
select code from
dbo.Table1
where code is not null and code not like 'A%'
)
go
create unique clustered index unq_code on v_Table_unq(code)
Thanks for help
Table Creation
CREATE TABLE CheckConstraint
(
Name VARCHAR(50),
)
GO
Function Creation
create FUNCTION CheckDuplicateWithA() RETURNS INT AS BEGIN
DECLARE #ret INT =0 ;
SELECT #ret = IsNull(COUNT(Name), 0) FROM CheckConstraint WHERE Name like '[A]%' group by Name having COUNT(name) >= 1;
RETURN IsNUll(#ret, 0);
END;
GO
create FUNCTION CheckDuplicateOtherThenA() RETURNS INT AS BEGIN
DECLARE #ret INT =0 ;
SELECT #ret = IsNull(COUNT(Name), 0) FROM CheckConstraint WHERE Name not like '[A]%' group by Name having COUNT(name) >= 1;
RETURN IsNUll(#ret, 0);
END;
GO
Constraints
alter TABLE CheckConstraint
add CONSTRAINT CheckDuplicateContraintWithA CHECK (NOT (dbo.CheckDuplicateWithA() > 2));
go
alter TABLE CheckConstraint
add CONSTRAINT CheckDuplicateConmstraintOtherThenA CHECK (NOT (dbo.CheckDuplicateOtherThenA() > 1));
go
Result Set
insert into CheckConstraint(Name)Values('b') -- Passed
insert into CheckConstraint(Name)Values('b') -- Failed
insert into CheckConstraint(Name)Values('a') -- Passed
insert into CheckConstraint(Name)Values('a') -- Passed
insert into CheckConstraint(Name)Values('a') -- Failed
Why would you want a unique contraint? Why cant add this logic in the proc which inserts the data in the table?If you do not have a single point of insertion/updation etc?Why cant put it in instead of or after trigger?That would be much better as you can handle it well and could return proper errror messages.This will have less overhead than having a index view which will add to overhead.If you need unique constraint for the records which doesnt start with 'A' then you can have a persisted column and have a unique constraint on that.
Off course you will have overhead of having persisted computed column with index..But if you just need unique contsraint you can use that.For values which starts with 'A' this could be a null value.

sql delete from rows value

The database includes a table with a rows:
html/css/java/php
How with sql query delete "java"?
The result would be:
html/css/php
See http://www.w3schools.com/sql/sql_delete.asp for details on DELETE.
DELETE FROM TableName WHERE yourColumn = 'java'
try this (SQL Server syntax, question does not specify which database):
DECLARE #YourTable table (YourColumn varchar(500))
INSERT INTO #YourTable VALUES ('html/css/java/php')
INSERT INTO #YourTable VALUES ('html/css/java')
INSERT INTO #YourTable VALUES ('java/php')
INSERT INTO #YourTable VALUES ('java')
INSERT INTO #YourTable VALUES ('html/css/php')
UPDATE #YourTable
SET YourColumn=REPLACE(
REPLACE(
REPLACE(YourColumn,'/java','')
,'java/',''
)
,'java',''
)
select * from #YourTable
OUTPUT
YourColumn
-------------------
html/css/php
html/css
php
html/css/php
(5 row(s) affected)
I'm not entirely sure what you're asking, because your question isn't very clear.
If "html", "css", "java", and "php" are different values for the same column, this is what you want:
DELETE FROM table_name WHERE column_name = 'java';
You'll need to replace "table_name" with the name of your table and "column_name" with the name of your column.
If there are 4 rows then Aioobe's answer would hold good. If you want to update the column but leave out Java then you should use:
UPDATE table_name SET column_name = Replace(column_name,'java/','')
IF you want to retrieve the information leaving out that data then use:
SELECT Replace(column_name,'java/','') column_name FROM table_name
HTH
I'm not sure what you want to achieve... If you need to delete column 'java', it should look like
ALTER TABLE table1 DROP COLUMN java
Here's another guess:
SQL> select * from t23
2 /
WHATEVER
----------------------------------------------------------------
html/css/java/php
SQL> update t23
2 set whatever = replace(whatever, '/java', null)
3 where whatever like '%/java%'
4 /
1 row updated.
SQL> select * from t23
2 /
WHATEVER
----------------------------------------------------------------
html/css/php
SQL>
There are other ways to do the same thing. For instance some flavours of database support using RegEx in a similar fashion