How do I do select into from another table - sql

Basically I want to run a SQL statement like this, but I can't due to errors.
Not sure why?
INSERT INTO allevent (eventname)
VALUES (select username from registered)
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.

No need of parentheses around the SELECT statement in your query.
INSERT INTO allevent (eventname)
SELECT username FROM registered
Parentheses with VALUES required, where some places like below:
INSERT INTO tablename (fieldname)
VALUES ('field 01'), ('field 02');
Please find the syntax for the INSERT statement.

Related

SQL Server: incorrect syntax near the keyword

I am trying to run the following T-SQL code which contains 8160 individual numbered accounts and receive the the following errors, with Line 10 being the first instance of INSERT and line 8162 being the last instance of INSERT
Code:
SELECT
AXACNO AS [AGREEMENT],
AX15C1 AS [BLOCKCODE]
FROM
BIDW_Staging.BPFCentrac.ACCEXTPF
WHERE
AXACNO IN (
INSERT INTO ##AN_INSERT VALUES ('0000000000000000')
INSERT INTO ##AN_INSERT VALUES ('1111111111111111')
INSERT INTO ##AN_INSERT VALUES ('2222222222222222')
I get these errors:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'INSERT'
Msg 102, Level 15, State 1, Line 8162
Incorrect syntax near ')'
First you need to insert your data into your ##AN_INSERT table. Each INSERT is a separate statement and should be terminated with a ;.
INSERT INTO ##AN_INSERT VALUES ('0000000000000000');
INSERT INTO ##AN_INSERT VALUES ('1111111111111111');
INSERT INTO ##AN_INSERT VALUES ('2222222222222222');
Then you can select using this table:
select
AXACNO AS [AGREEMENT],
AX15C1 AS [BLOCKCODE]
from BIDW_Staging.BPFCentrac.ACCEXTPF
Where AXACNO in
(SELECT <whatevercolumnthisis> FROM ##AN_INSERT);

how to insert values through select in ssms ? and i am getting error through which i tried

I tried inserting values into a table that I created but while inserting into them through select statement I am getting an error message
the code which I tried for insert is
create table dbo.watermarktable
(tablename varchar(255),
watermarkvalue datetime,
);
insert into dbo.watermarktable(tablename,watermarkvalue)
("reference_value", select max(created_date) from dbo.reference_value_genc);
but the values were not taken and thrown an error message
and the error message is
Incorrect syntax near 'reference_value'.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
Completion time: 2021-10-28T14:29:43.2357252+01:00
You can either use VALUES or SELECT with INSERT INTO. So Change your insert like this
insert into dbo.watermarktable(tablename,watermarkvalue)
SELECT 'reference_value', max(created_date) from dbo.reference_value_genc;
Your syntax is a bit off, change it to this:
INSERT INTO dbo.watermarktable (tablename,watermarkvalue)
SELECT 'reference_value', max(created_date)
FROM dbo.reference_value_genc;

Create a table with SQL from Query result in SSMS

I am trying to create a new table from a query result in SSMS
I have tried:
CREATE TABLE table2 AS
(SELECT * FROM table1
WHERE code = 'x'
ORDER BY code;)
However I am getting an error telling me:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'ORDER'.
Can anyone help?
Try this:
SELECT *
INTO NEW_TABLE
FROM table1
WHERE code = 'x'
ORDER BY code;

How to Use the OUTPUT of an Insert Query for another insert query

I'm trying to insert 2 records into 2 tables by using a subquery, but it gives me a syntax error.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'INSERT'.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.
The query that I'm trying to execute is
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
VALUES
(
(INSERT INTO [FileTransformations] OUTPUT INSERTED.FileTransformationId DEFAULT VALUES),
2
)
Is this possible?
You need to store the output in a variable first. So, I think you want something like this:
DECLARE #variable TABLE (value INT)--change type depending on your need
INSERT INTO [FileTransformations]
OUTPUT INSERTED.FileTransformationId INTO #variable(value)
VALUES(.....)
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
SELECT value, 2 FROM #variable
See How do I use an INSERT statement's OUTPUT clause to get the identity value? for more info on using output variables and Insert into table from table variable? for then inserting from that table variable.
Another option, if you just want the id of the last inserted record is to use scope_identity(). So you could write the above as:
INSERT INTO [FileTransformations]
VALUES(.....)
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
VALUES scope_identity(), 2

Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ')' while creating the table in sql

Whenever I try to create table in my selected database I get
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'
Can anybody tell me whats the error and how to overcome.
What's the error in it ?
CREATE TABLE first_name
(
)
The error is that every table needs to have at least a single column.
CREATE TABLE users
(
id int,
firstname varchar(255),
lastname varchar(255)
)