Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in oracle so I am not able to write this query in Oracle its showing me some compile time error
main issues are:
1) Not able to use @@identity option which is in sql and return last inserted id (as its auto insert value)


SQL
ALTER PROCEDURE [dbo].[Insert_Update_Students](
@SelectedCourse varchar(4000) = NULL
,@StudentID int = NULL
 output
,@UserID int = NULL
,@YogaBefore bit = NULL
,@YourComment varchar(250) = NULL
)
AS  BEGIN
SET NOCOUNT ON;
IF(@StudentID IS NULL OR @StudentID <= 0)
BEGIN
INSERT INTO Students( 
[SelectedCourse]
,[UserID]
,[YogaBefore]
,[YourComment]
)
VALUES(
@SelectedCourse
,@UserID
,@YogaBefore
,@YourComment
)
set @StudentID=@@identity;
END
ELSE
BEGIN
UPDATE Students SET[SelectedCourse]=@SelectedCourse
,[UserID]=@UserID
,[YogaBefore]=@YogaBefore
,[YourComment]=@YourComment
 where
 [StudentID]=@StudentID

END
END
Posted
Updated 1-Sep-14 2:23am
v2

1 solution

Oracle Sequence is the alternative of Identity in SQL Server. You need to create a sequence using below SQL Command
CREATE SEQUENCE student_seq
  MINVALUE 1
  MAXVALUE 999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

and then you can easily use sequence in following way
SQL
INSERT INTO Students( StudentId,
,SelectedCourse
,UserID
,YogaBefore
,YourComment
)
VALUES(
student_seq.NEXTVAL,
,@SelectedCourse
,@UserID
,@YogaBefore
,@YourComment

UPDATE Students SET[SelectedCourse]=@SelectedCourse
,[UserID]=@UserID
,[YogaBefore]=@YogaBefore
,[YourComment]=@YourComment
 where
 [StudentID]=student_seq.CURRVAL
)
 
Share this answer
 
v2
Comments
Awadhendra Tripathi 1-Sep-14 8:48am    
Ok, that's fine but i am created identity column from sql developer application or oracle now i want to write insert and update query in a single statement and after insertion i want last id of inserted row,
Like in my case when you pass studentid it will go for updation but if it null then it will insert a new record and revert it to me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900