create table in sql - sql

I am getting an error when I attempt to run this query:
create table New_Table as
select NAme
from File_name, FileType
where File_name.name = FileType.Name
Here is the error I am getting:
"ORA-00604: error occurred at recursive SQL level 2
ORA-01422: exact fetch returns more than requested number of rows"
Any idea why?

Is NAme a separate field from file_name.name and filetype.name? If not, you would need to specify file_name.name or filetype.name in your SELECT statement.

Related

How to create a stored procedure to copy data from a query to a temporary table?

I have need of inserting data to a temporary table from an existing table/query. The following produces the error detailed below.
CREATE TABLE SPTemporary
AS
BEGIN
SELECT * into #temppT
FROM SampleTable
END
Throws this error:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'begin'.
Correct your syntax, use procedure instead of table :
create procedure SPTemporay
as
begin
select * into #temppT
from SampleTable
end
However, if you want only copy of data then only subquery is enough :
select st.* into #temppT
from SampleTable st
One method is:
select st.*
into SPTemporay
from SampleTable st
One select can only put data in one table. It is unclear which one you really want SPTemporary or #temppT. You can repeat the select if you really need the same data in two tables.
EDIT:
If you want a stored procedure, you could do:
create procedure SPTemporay
as begin
select *
into #temppT
from SampleTable
end;
This is rather non-sensical, because the temporary table is discarded when the stored procedure returns.
I think the syntax is wrong, it should be like that:
create table SPTemporay
as
select * from SampleTable
I hope this helps.

msg 8152 level 16 state 2 string or binary data would be truncated

I am getting this error while executing a procedure
Msg 8152, Level 16, State 2, Procedure SP_xxxxx, Line 92 String or
binary data would be truncated.
I have created a temp table which I will load the data from main table once in procedure and I would be using this table not the main table as main table has huge volume of data and many unnecessary columns.
When I run the below code from sql server management studio then there is no error but when I run this code from a procedure then their is the above error message.
Insert into abc_TMP // tmp for procedure with required columns
Select
Item,
Description,
size,
qty,
stock,
Time ,
Measure
from abc // main table has many columns
One way to check this issue, is to see length of each value
Assume you have table like below
create table t
(
col1 varchar(10),
col2 varchar(10)
)
Inserts into the table will fail,if you try to insert more than 10 characters,if you try to insert them in a batch, you will not get offending value.
So you need to check its length like below , prior to insert
;with cte
as
(
select
len(col1) as col1,
len(Col2) as col2
from table
)
select * from cte where col2>10
There has been number of requests raised with Microsoft to enhance error message and they have finally fixed this issue in SQL2019.
Now you can get the exact value causing the issue
References:
https://voiceofthedba.com/2018/09/26/no-more-mysterious-truncation/
I suspect that you are looking at the wrong line of code in the stored proc.
When you open the proc, i.e. ALTER... you have a header on the stored proc that will throw the line number out.
If you run this, replacing proc_name with your procedure name:
sp_helptext proc_name
That will give you the code that the procedure will actually run, with accurate line numbers if you paste it into a new window.
Then you'll see where the actual error is happening.
If you want a simple way to prove this theory, put a bunch of Print 'some sql 1', Print 'some sql 2' lines in around the code you think is causing the error and see what is output when the error is thrown.

View Statement on SQL Server returns "Invalid Object Name"

When executing the following script on my SQL Server:
CREATE VIEW joiny AS
SELECT EventTime
FROM [dbo].[Table_1]
I get the following error:
Invalid object name 'Table_1'.
I cannot figure out why this is an error. Could anyone point me in the right direction? I tried with and without the [] as well as the "dbo".
This error can mean that the table [dbo].[Table_1] simply does not exist in the database you are using.
CREATE VIEW tabtest
AS
SELECT *
FROM dbo.TabTest
Results in:
Msg 208, Level 16, State 1, Procedure tabtest, Line 4 Invalid object
name 'dbo.TabTest'.
Because I do not have a table named dbo.tabtest in my database.\
If you are trying to create a view in a different database than the one where your table exists, then you need to use 3-part naming when you specify the table, like this:
CREATE VIEW joiny AS
SELECT EventTime
FROM [MyDatabaseName].[dbo].[Table_1]

SQL query regarding Sequence

I made a sequence in Net Beans. But when I tried to insert data in the table I get an error.
My code is:
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen');
The error is:
[Exception, Error code 30,000, SQLState 42X04] Column
'SEQ_PERSON.NEXTVAL' is either not in any table in the FROM list or
appears within a join specification and is outside the scope of the
join specification or appears in a HAVING clause and is not in the
GROUP BY list. If this is a CREATE or ALTER TABLE statement then
'SEQ_PERSON.NEXTVAL' is not a column in the target table.
The error message Error code 30,000, SQLState 42X04 indicates you are using Derby DB not Oracle. That being the case you need to use Derby syntax for getting the next value. So your insert should look like this:
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (NEXT VALUE FOR seq_person,'Lars','Monsen')

Deleting the Global Temp table and then creating the Global temp table with same name is giving ambiguity error

Below is the SQL statement for Deleting the global temp table and creating it again.
if object_ID('tempdb..##TempTable123') is not null
begin drop table tempdb..##TempTable123 end
GO
Select * into ##TempTable123 from ##TempTable1
After executing the Select statement, I am getting the following Error
"The reference to temp table name '##TempTable123' is ambiguous and cannot be resolved. Use either '##TempTable123' or '##TempTable123'."
Try to add tempdb.. to the select:
Select * into tempdb..##TempTable123 from tempdb..##TempTable1