Insert into Tamil_language values (1, N'மோதிரம்')
Insert into Tamil_language values (2, 'மோதிரம்')
Select * from Tamil_language
Output
i j
----------- ----------
1 மோதிரம்
2 ???????
How to do this using store procedure in Oracle Database?
How To Add 'N' Prefix For the Store procedure Parameter?
Please Help me if any one know this.
Related
Can anyone give me some idea to generate native oracle database alphanumeric sequence with column length 4, like 000A,000B and so on. I need 200k or more records of this sequence.
Any ideas/solutions are highly appreciated.
Thanks in advance
If you want a SELECT statement to generate hexadecimal values then:
SELECT TO_CHAR(LEVEL, 'FM0000X')
FROM DUAL
CONNECT BY LEVEL <= 200000;
If you want a SEQUENCE then they only generate numeric values; however, you can wrap its output in the same TO_CHAR function:
CREATE SEQUENCE table_name__id__seq;
CREATE TABLE table_name (
id VARCHAR2(5)
);
INSERT INTO table_name (id)
VALUES (TO_CHAR(table_name__id__seq.NEXTVAL, 'FM0000X'));
SELECT * FROM table_name;
Outputs:
ID
00001
db<>fiddle here
I have three columns in MyTable and I need to calculate % of total by each group.
Code:
SELECT AppVintage, Strategy, Apps
FROM mytable
GROUP BY AppVintage,Strategy,Apps
Each date appears 4 times, with a different class for each line and then a total of applications for each line.
The table looks something like this:
Code for the sample data set:
CREATE TABLE mytable(
AppVintage VARCHAR(6) NOT NULL PRIMARY KEY
,Strategy VARCHAR(28) NOT NULL
,Apps INTEGER NOT NULL
);
INSERT INTO mytable(AppVintage,Strategy,Apps) VALUES ('Nov16','300',10197);
INSERT INTO mytable(AppVintage,Strategy,Apps) VALUES ('Nov-16','ORIG',29023);
INSERT INTO mytable(AppVintage,Strategy,Apps) VALUES ('Nov-16','400',7219);
INSERT INTO mytable(AppVintage,Strategy,Apps) VALUES ('Nov-16','500',9452);
INSERT INTO mytable(AppVintage,Strategy,Apps) VALUES ('Dec-16','300',12517);
INSERT INTO mytable(AppVintage,Strategy,Apps) VALUES ('Dec-16','ORIG',37762);
INSERT INTO mytable(AppVintage,Strategy,Apps) VALUES ('Dec-16','400',8992);
INSERT INTO mytable(AppVintage,Strategy,Apps) VALUES ('Dec-16','500',11229);'
What I need is to add a column that calculates percentage of apps per strategy for each appvintage.
Is there a way to do this? Thanks in advance!
You can use window functions to do this (Assuming SQL Server 2008+)
SELECT
AppVintage,
Strategy,
Apps,
Apps/SUM(Apps) OVER (PARTITION BY AppVintage) as Percent_Apps
FROM myTable
We are importing an external excel files to our SQL database but we need to do some integrity checks using sql script
Here is my sample data
Row_no Student_area Student_subject Code
1 Science Science and Tech ABC
2 Science Science and Teck ABC
3 Arts Pschycolgy DEF
4 Arts Pscycology DEF
I need to identifythe anomalies
How do I do that?
Cheers
Oracle SQL has many neat features but it does not exhibit human style intelligence (yet). So it cannot identify "anomalies" in data. We must declare the rules for correctness.
In your case you need to define a set of correct values, preferably as reference data tables:
create table student_area (student_area varchar2(30));
insert into student_area values ('Science');
insert into student_area values ('Arts');
create table student_subject (student_area varchar2(30),
student_subject varchar2(128),
subject_code varchar2(3));
insert into student_subject values ('Science', 'Science and Tech', 'ABC');
insert into student_subject values ('Arts', 'Psychology', 'DEF');
Now you are ready to evaluate the contents of your file. The easiest way to do this is to convert the Excel file to CSV and build an external table over it. This is a special type of table where the data resides in an external OS file rather than in the database. Find out more.
If you create an external table with a column mapped to every column in the spreadsheet you can identify the anomalies like this:
select * from external_table ext
where ext.student_area not in ( select s.student_area
from student_area s )
/
select * from external_table ext
where (ext.student_area, ext.student_subject) not in
( select s.student_area, s.student_subject
from student_subject s )
/
I need to know if there is any way to have a SEQUENCE or something like that, as we have in Oracle. The idea is to get one number and then use it as a key to save some records in a table. Each time we need to save data in that table, first we get the next number from the sequence and then we use the same to save some records. Is not an IDENTITY column.
For example:
[ID] [SEQUENCE ID] [Code] [Value]
1 1 A 232
2 1 B 454
3 1 C 565
Next time someone needs to add records, the next SEQUENCE ID should be 2, is there any way to do it? the sequence could be a guid for me as well.
As Guillelon points out, the best way to do this in SQL Server is with an identity column.
You can simply define a column as being identity. When a new row is inserted, the identity is automatically incremented.
The difference is that the identity is updated on every row, not just some rows. To be honest, think this is a much better approach. Your example suggests that you are storing both an entity and detail in the same table.
The SequenceId should be the primary identity key in another table. This value can then be used for insertion into this table.
This can be done using multiple ways, Following is what I can think of
Creating a trigger and there by computing the possible value
Adding a computed column along with a function that retrieves the next value of the sequence
Here is an article that presents various solutions
One possible way is to do something like this:
-- Example 1
DECLARE #Var INT
SET #Var = Select Max(ID) + 1 From tbl;
INSERT INTO tbl VALUES (#var,'Record 1')
INSERT INTO tbl VALUES (#var,'Record 2')
INSERT INTO tbl VALUES (#var,'Record 3')
-- Example 2
INSERT INTO #temp VALUES (1,2)
INSERT INTO #temp VALUES (1,2)
INSERT INTO ActualTable (col1, col2, sequence)
SELECT temp.*, (SELECT MAX(ID) + 1 FROM ActualTable)
FROM #temp temp
-- Example 3
DECLARE #var int
INSERT INTO ActualTable (col1, col2, sequence) OUTPUT #var = inserted.sequence VALUES (1, 2, (SELECT MAX(ID) + 1 FROM ActualTable))
The first two examples rely on batch updating. But based on your comment, I have added example 3 which is a single input initially. You can then use the sequence that was inserted to insert the rest of the records. If you have never used an output, please reply in comments and I will expand further.
I would isolate all of the above inside of a transactions.
If you were using SQL Server 2012, you could use the SEQUENCE operator as shown here.
Forgive me if syntax errors, don't have SSMS installed
Using SQL Server 2008, have three tables, table a, table b and table c.
All have an ID column, but for table a and b the ID column is an identity integer, for table c the ID column is a varchar type
Currently a stored procedure take a name param, following certain logic, insert to table a or table b, get the identity, prefix with 'A' or 'B' then insert to table c.
Problem is, table C ID column potentially have the duplicated values, i.e. if identity from table A is 2, there might already have 'A2','A3','A5' in the ID column for table C, how to write a T-SQL query to identify the next available value in table C then ensure to update table A/B accordingly?
[Update]
this is the current step,
1. depends on input parameter, insert to table A or table B
2. initialize seed value = ##Identity
3. calculate ID value to insert to table C by prefix 'A' or append 'B' with the seed value
4. look for record match in table C by ID value from step 3, if didn't find any record, insert it, else increase seed value by 1 then repeat step 3
The issue being at a certain value range, there could be a huge block of value exists in table C ID, i.e. A3000 to A500000 existed now in table C ID, the database query is extemely slow if follow the existing logic. Needs to figure out a logic to smartly get the minimum available number (without the prefix)
it is hard to describe, hope this make more sense, I truly appreciate any help on this Thanks in advance!
This should do the trick. Simple self extracting example will work in SSMS. I even made it out of order just in case. You would just change your table to be where #Data is and then change Identifier field to replace 'ID'.
declare #Data Table ( Id varchar(3) );
insert into #Data values ('A5'),('A2'),('B1'),('A3'),('B2'),('A4'),('A1'),('A6');
With a as
(
Select
ID
, cast(right(Id, len(Id)-1) as int) as Pos
, left(Id, 1) as TableFrom
from #Data
)
select
TableFrom
, max(Pos) + 1 as NextNumberUp
from a
group by TableFrom
EDIT: If you want to not worry about production data you could add this last part amending what I wrote:
Select
TableFrom
, max(Pos) as LastPos
into #Temp
from a
group by TableFrom
select TableFrom, LastPos + 1
from #Temp
Regardless if this was production environment you are going to have to hit part of it at some time to get data. If the datasets are not too large and just varchar(256) or less and only 5 million rows or less you could dump that entire column from tableC to a temp table. Honestly query performance versus imports change vastly from system to system.
Following your design there shouldn't be any duplicates in Table C considering that A and B are unique.
A | B | C
1 1 A1
2 2 A2
B1
B2