I want to be able to create new tables based on any SQL select statement. I have tried the following which I got the format from another question and it does not work (there are similar questions but not one that I found actually works). I keep getting an error on the SQL statement.
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
This is the CREATE TABLE statement:
CREATE TABLE MyNewTable
AS
SELECT *
FROM dbo.Bat
This will copy the entire table including rows
SELECT *
INTO newTableName
FROM dbo.Bat
Add WHERE 1 = 0 to copy just the table structure
If it is SQL Server (the dbo schema, default in SQL Server indicates it is SQL Server), you can do following.
select * into MyNewTable from dbo.Bat;
The SELECT INTO statement does not copy your table constraints.
You statement is a valid Oracle and MySQL statement though.
CREATE TABLE ... AS SELECT is simple (by deliberately ignoring for example the concepts of storage)
To create a table with all its lines
code:
CREATE TABLE XX AS SELECT * FROM YY ;
the result of command in mysql
Related
I need to copy tables from a linked server onto my local machine. I am working in SQL management studio. The linked server is Oracle based. My end goal is to set up a stored proc that deletes a table if it exists and creates a new table in its place with refreshed data. This will be done for many tables as needed. The issue with the below code is that I get the error:
Incorrect syntax near the keyword 'SELECT'.
I am stuck at creating the table.
CREATE TABLE test AS
SELECT DUMMY
FROM OPENQUERY (LServer, '
Select *
from sourceT
');
The data in the dummy table is just one column with a single value "x". I have seen posts that suggest using a certain notation in naming the linked server table, like <server.database.schema.tablename> but this doesn't seem to work,even if I just run the select statement using the openquery. If I just run the select part in the script above, this does work.
CREATE TABLE test AS
Is valid in Oracle but not SQL Server
You want
-- if the table already exists drop it
DROP TABLE IF EXISTS test;
-- now create a table and load into it
SELECT DUMMY
INTO test
FROM OPENQUERY (LServer, '
Select *
from sourceT')
This question already has an answer here:
MS Access: Syntax error in CREATE TABLE statement
(1 answer)
Closed 1 year ago.
I want to create a table with selected columns from an existing larger table The syntax below is what is recommended but ACCESS throws an error msg. What gives?
CREATE TABLE new_table_name AS
SELECT column1, column2
FROM existing_table_name
WHERE ....;
Every brand of SQL database can choose how complete its implementation of the SQL language is. MS Access in particular fails to support a lot of language features that are common in other brands of database.
You just have to verify the syntax you want to use is supported. You can do this by referring to the syntax documentation, for example: CREATE TABLE Statement. The syntax and description makes no mention of CREATE TABLE ... AS SELECT ...
But SELECT.INTO Statement shows that you can use the SELECT ... INTO newtable FROM ... form. The documentation says this will create newtable in the process.
MS Access doesn't have a "create table as select syntax". Instead, you can use the "select into" syntax:
SELECT column1, column2
INTO new_table_name
FROM existing_table_name
WHERE ...
In MS Access, I created a query by create Menu-> Query Design (with name Query3).
I want use it in an SQL command in another query but when I run it got this error:
Syntax error on query expression 'select f1'
SQL command
INSERT INTO boors (boors.Nemad, boors.Volumn, boors.Price,
boors.LastPrice, boors.LastPerc, boors.LastPr,
boors.LastPer, boors.MinPrice, boors.MaxPrice,
boors.distance, boors.inout, boors.Power)
values (select f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12 FROM Query3)
It appears that you are mixing the SQL used for inserting values and inserting from a table/query. As you are doing the latter, your SQL should look like:
INSERT INTO boors (Nemad, Volumn)
SELECT F1, F2
FROM Query3
Regards,
Can somebody help me?
I am try execute the command below in Oracle 11 and I get this error:
SQL Error [905] [42000]: ORA-00905: keyword not found.
Code:
SELECT *
INTO SAJ.ETMP_TESTE
FROM SAJ.ESAJOBJETO O
WHERE CDOBJETO = 'P800000000J03'
I read the Oracle docs and haven't found any obvious error in my statement.
https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/selectinto_statement.htm
My objective is to create the table ETMP_TESTE with structure from ESAJOBJETO.
I checked user permission and user has permission to action.
You need create table and not select into for create a table based on the result of a query
CREATE TABLE SAJ.ETMP_TESTE
AS SELECT *
FROM SAJ.ESAJOBJETO O
WHERE CDOBJETO = 'P800000000J03'
This will create an empty table named ETMP_TESTE, with the structure of the SAJ.EASJOBJETO table.
CREATE TABLE ETMP_TESTE AS
SELECT *
FROM SAJ.EASJOBJETO
WHERE 1 = 0;
This does not handle contraints and primary keys and things like that, but it will get you the table structure. The 1 = 0 makes sure no data is copied.
If you need primary keys and the like, look into extracting the DDL for EASJOBJETO. Most SQL IDEs have that functionality built in. You can edit it to correct the table name and run the script and get everything.
I am running this SQL query in MS Access:
CREATE TABLE EX_P_TEMP
AS (SELECT
EXPORT_POSTING_ID, IMPORT_POSTING_ID, DIS,
MILE_SAVED, IMPORT_AVAILABLE
FROM POTENTIAL_PAIRS);
Error:
Syntax error in CREATE TABLE statement.
Where did I go wrong?
That doesn't look like valid Access syntax. If you are trying to create a new table based on the results of a SELECT, try this intead:
SELECT EXPORT_POSTING_ID, IMPORT_POSTING_ID, DIS, MILE_SAVED, IMPORT_AVAILABLE
INTO EX_P_TEMP
FROM POTENTIAL_PAIRS;