Temporary table both exists and doesn't exist [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to drop a temporary table in SQL Server if it exists and create a new temp table if it doesn't exist. The command IF OBJECT_ID('tempdb.. #my_temp') IS NULL seems unable to fine the temp table and then when the query tries to create the table, I receive an error that the temp table already exists.
This is my query:
IF OBJECT_ID('tempdb.. #my_temp') IS NOT NULL
BEGIN
DROP TABLE #my_temp
END
IF OBJECT_ID('tempdb.. #my_temp') IS NULL
BEGIN
print 'trying to create a new temp table'
CREATE TABLE #my_temp
( [ID] int NOT NULL PRIMARY KEY CLUSTERED ,
) ON [PRIMARY]
END
If I run this query twice, on the second time I receive this output:
trying to create a new temp table
Msg 2714, Level 16, State 6, Line 12
There is already an object named '#my_temp' in the database.
My SQL Server is version 2019 for your info.

Remove the space between .. and the table name
IF OBJECT_ID('tempdb..#my_temp') IS NOT NULL
BEGIN
DROP TABLE #my_temp
END
IF OBJECT_ID('tempdb..#my_temp') IS NULL
BEGIN
print 'trying to create a new temp table'
CREATE TABLE #my_temp
( [ID] int NOT NULL PRIMARY KEY CLUSTERED ,
) ON [PRIMARY]
END

Related

Adding AUTO_INCREMENT using ALTER in sql [duplicate]

This question already has answers here:
How to create id with AUTO_INCREMENT on Oracle?
(18 answers)
Closed 4 years ago.
I've been trying to add AUTO_INCREMENT on a created table , but the ALTER table query is not working
My Table:
Query and Error:
And also
ALTER TABLE professor ADD sno INT IDENTITY;
is not working
Can you please try:
alter table professor add sno integer generated by default on null as identity;
EDIT: Sorry, the OP is asking for 11g, as the code above works after 12.
Then you need to use sequence. Please see below:
ALTER TABLE professor ADD sno INT;
CREATE SEQUENCE dept_seq START WITH 1;
And you need to set a trigger for your sequence like:
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON departments
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
Reference: How to create id with AUTO_INCREMENT on Oracle?

Oracle SQL ALTER TABLE error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have code like this:
ALTER TABLE "VMA_CSDD" ADD CONSTRAINT "VMA_CSDD_PK" PRIMARY KEY ("ID") ENABLE
ALTER TABLE "VMA_CSDD" MODIFY ("ID" NOT NULL ENABLE)
ALTER TABLE "VMA_CSDD" MODIFY ("CSDD_NAME" NOT NULL ENABLE)
It gives me an error:
ALTER TABLE "VMA_CSDD" MODIFY ("ID" NOT NULL ENABLE)
ORA-01735: invalid ALTER TABLE option
How do I fix this?
Option ENABLE is not correct with column definition you have to also add types
ALTER TABLE "VMA_CSDD" MODIFY ("ID" int NOT NULL )
ALTER TABLE "VMA_CSDD" MODIFY ("CSDD_NAME" varchar(100) NOT NULL )

INSERT INTO suppliers casue Schema Creation Failed: ORA-00922: missing or invalid option [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What's causing this error?
Schema Creation Failed: ORA-00922: missing or invalid option
.........................
CREATE TABLE suppliers
(
sid int,
sname varchar(20),
address varchar(30)
)
CREATE TABLE product
(
pid int,
pname varchar(20),
color varchar(30)
)
CREATE TABLE catalog
(
sid int,
pid int,
cost int
)
INSERT INTO suppliers
(sid, sname,address)
VALUES
(1, "name1","address1"),
(2, "name2","address2");
SQL Fiddle
Also, your INSERT statement will probably fail in Oracle (though this is not the reason why you are getting ORA-00922)
Change it to:
INSERT ALL
INTO suppliers(sid, sname,address) VALUES (1, 'name1','address1')
INTO suppliers(sid, sname,address) VALUES (2, 'name2','address2')
SELECT * FROM dual;

Creating table in Oracle yields a "Missing right parenthesis" error [duplicate]

This question already has answers here:
How to create id with AUTO_INCREMENT on Oracle?
(18 answers)
Closed 8 years ago.
I am trying to create a table, but keep getting the following error message: Warning: oci_execute(): ORA-00907: missing right parenthesis in ... on line 14
The following is the code that deals with this issue:
$stid = oci_parse($conn, 'CREATE TABLE tags (
id INT NOT NULL auto_increment,
PRIMARY KEY(id),
name VARCHAR2(64) NOT NULL)')
or die(oci_error($conn));
oci_execute($stid) or die(oci_error($conn));
Line 14 is oci_execute($stid) or die(oci_error($conn));.
I am new to Oracle and don't understand this error. I used Google and found numerous posts on StackOverflow too, but none of those answers were able to solve this problem and properly create a new table.
What am I doing wrong here?
I believe your used to creating table in MySQL.
Oracle don't have an auto_increment as MySQL.
$stid = oci_parse($conn, 'CREATE TABLE tags (
id INT NOT NULL,
name VARCHAR2(64) NOT NULL),
PRIMARY KEY(id)')
Oracle used object Sequence to create an auto_increment value.
Example on how to create sequence assuming you have the right permission.
CREATE SEQUENCE "CCAD"."AUTH_GROUP_SQ"
MINVALUE 1
MAXVALUE 999999999999
INCREMENT BY 1
START WITH 91
CACHE 20
NOORDER NOCYCLE;
Together with Insert trigger.
create or replace TRIGGER "AUTH_GROUP_TR"
BEFORE INSERT ON "AUTH_GROUP"
FOR EACH ROW
WHEN (new."ID" IS NULL)
BEGIN
SELECT "AUTH_GROUP_SQ".nextval
INTO :new."ID" FROM dual;
END;

UPDATE Statement not working. what's wrong [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm SORRY everyone, It wasn't allowing me to comment. you are right. It's the ALTER I'm having issues with. How do I add the Cumulative GPA column to the table (using the alter statement) where the gpa is displayed between 0.00 and 4.00
CREATE database "IS4440_DuBoseJasmine"
CREATE TABLE StudentInformation (
StudentID CHAR(7) not null,
StudentSSN CHAR(9) null,
StudentFirstName VARCHAR(50) null,
StudentLastName VARCHAR(50) null,
StudentMiddleName VARCHAR(50) null,
StudentHomeCountry CHAR(2) not null
)
/*2*/
ALTER TABLE StudentInformation ADD Cumulative GPA ;
/*3*/
INSERT INTO StudentInformation (StudentID, StudentLastName, StudentFirstName, StudentMiddleName, StudentHomeCountry)
VALUES ('1352154', 'DuBose', 'Jasmine', 'Leigh', 'US')
INSERT INTO StudentInformation (StudentID, StudentLastName, StudentFirstName, StudentMiddleName, StudentHomeCountry)
VALUES ('1234565', 'Smith', 'Johnny', 'Apple', 'GB');
/*4*/
UPDATE StudentInformation SET StudentSSN = 123456789
WHERE StudentID = 1352154;
The update works fine (demonstrated on SQL Fiddle), it is the ALTER TABLE statement that fails. This line:
ALTER TABLE StudentInformation ADD Cumulative GPA;
should be:
ALTER TABLE StudentInformation ADD [Cumulative GPA] INT -- OR WHATEVER TYPE IT SHOULD BE;
ALTER TABLE Documentation
As an aside, although this may just be an example, if it isn't you will want to USE your database before you create the table:
CREATE database "IS4440_DuBoseJasmine";
GO
USE IS4440_DuBoseJasmine;
CREATE TABLE ...
Otherwise you will just be creating your table in whatever database you are connected to.
Your update query is fine, Your ALTER statement what fails, You are adding new column to your table without any datatype or constrains, try this to alter your table
You can use Numeric(3,2) or Decimal(3,2), that means total three digits two after decimal point so a max of 9.99 .
ALTER TABLE StudentInformation ADD CumulativeGPA Numeric(3,2) NULL
To alter your table to add new column make that column NULLABLE and while naming your column do not give spaces.
To make column type Not Nullable
ALTER TABLE StudentInformation ALTER COLUMN CumulativeGPA DECIMAL(3,2) NULL ;