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);
Related
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;
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.
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
I am writing a very basic insert statement. I noticed that the error message "Invalid column name" appears if the VALUE is a character besides a number.
The data type is VARCHAR(50) on all three columns.
Database: MS SQL 2012
Query:
INSERT INTO [test-db].[dbo].[Computer]
([id]
,[name]
,[office])
VALUES
(123
,john
,main)
GO
ERROR:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'john'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'main'.
Change your query to this
INSERT INTO [test-db].[dbo].[Computer]
([id]
,[name]
,[office])
VALUES
(123
,'john'
,'main')
GO
The varchar needs quotes around it to work.
i've succesfully created db and tables, but when i try populate one of a table, like this
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0),
(0,'productName2',0),
(1,'productName3',9),
(1,'productName4',7),
(1,'productName5',3),
(1,'productName6',10),
(0,'productName7',0),
(1,'productName8',6),
(1,'productName9',12),
(1,'productName10',20);
GO
i got an error:
Msg 102, Level 15, State 1,
Line 2 Incorrect syntax near ','.
firstly which ',' is meant, secondly - what is wrong?
PS: i use MS Management Studio v 9.0 if it is needed...
Versions of SQL Server 2005 and below do not support the multiple VALUE clause syntax
SQL Server 2005 is version 9...
See How do I insert multiple rows WITHOUT repeating the "INSERT INTO dbo.Blah" part of the statement? for more
if you are using SQL SERVER 2005 and below, the query wouldn't work because it doesn't support multiple value clause insert statement. You should insert it one by one.
Like this one below,
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0)
GO
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName2',0)
GO