I am trying to insert multiple rows with SQL statement.
For that i refered this question.
According to top scored answer in this qestion, i made following query:
INSERT INTO login
(LogInID,Password)
UNION ALL
SELECT 'Name1','pass1'
UNION ALL
SELECT 'Name2','pass2'
But when i try to execute this one, it gives me error:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'UNION'.
What can be the mistake in this query?
Is this a wrong approach?
Please help me.
NOTE: I am using SQL SERVER 2005
you have to remove UNION ALL before the first SELECT.
INSERT INTO login (LogInID,Password)
SELECT 'Name1','pass1'
UNION ALL
SELECT 'Name2','pass2'
Even though it does not provides an answer to your original question I think it's worth knowing that SQL Server provides another syntax using the VALUES syntax:
insert into login values
('Name1','pass1'),
('Name2','pass2'),
('Name3','pass3')
Related
Below is the query I tried to execute in SQL Server:
CREATE TABLE List
AS
(SELECT * FROM counts)
I get this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('
I did some research on google to understand and isolate the error, but, failed to do so. Could anyone please help me understand the error. Thank you
SQL-Server's syntax to create a table from a query is by adding an into clause:
SELECT *
INTO list
FROM counts
So, kind of an SQL Newbie here and I am trying to get something to work on Microsoft SQL Server 2005 (yay for old outdated databases powering businesses still).
I can get it to work properly on my local dev machine (running SQL Server 2019) but when I run it on the 2005 server it errors out.
Query:
MERGE CustomDB.[dbo].StockCounts AS [Target]
USING (SELECT ID,
ProductNo
FROM CompanyDBReplication.[dbo].STOCKPRODUCT) AS [Source] (ID,
ProductNo)
ON [Target].ID = [Source].ID
WHEN NOT MATCHED THEN
INSERT (id,
ProductNo,
CountDate,
CountID)
VALUES ([Source].ID,
[Source].ProductNo,
NULL,
NULL);
Error:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.'.
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'AS'.
Now I don't know enough about the differences here of why this would error out nor how I would go about searching this (I really don't do SQL ever and sort of had to Google this to make it work in the first place).
Basically I want to copy/merge items from a source database into the target database and add new ones that might get added to the source if they are not found in the target.
If someone can help me either fix this one to work on SQL Server 2005 or propose/give me an example of a different solution that will accomplish the same thing and work on SQL Server 2005 that would be awesome and I would forever be indebted.
The source of where I got this solution is here: https://stackoverflow.com/a/34892729/5877943
Merge statement appeared only in MSSQL 2008, you can use an outer join instead.
Something like this.
INSERT INTO CustomDB.[dbo].StockCounts (
id,
ProductNo,
CountDate,
CountID
)
SELECT
[Source].ID,
[Source].ProductNo,
NULL,
NULL
FROM CompanyDBReplication.[dbo].STOCKPRODUCT) AS [Source]
LEFT JOIN CustomDB.[dbo].StockCounts AS [Target] ON [Target].ID = [Source].ID
WHERE [TARGET].Id IS NULL;
I am am using sql server 2005 and doing a simple insert into and getting an incorrect syntax error. I See nothing wrong with my code Can someone give me some ideas what could be wrong with it?
insert into inonhd
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
('CRV109','1','11','01','1'),
('CRV110','0','11','01','0')
the error is Incorrect syntax near ','.
You must add each row in separate command.
insert into inonhd
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
('CRV109','1','11','01','1')
and:
insert into inonhd
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
('CRV110','0','11','01','0')
It is really important to note that the syntax in the question is fine for more recent versions of SQL Server. This is acceptable:
insert into inonhd(fpartno, fpartrev, flocation, fonhand, fcudrev)
Values ('CRV109','1','11','01','1'),
('CRV110','0','11','01','0');
If you want to do this in one statement, you can use select . . . union all:
insert into inonhd(fpartno, fpartrev, flocation, fonhand, fcudrev)
select 'CRV109','1','11','01','1' union all
select 'CRV110','0','11','01','0';
Of course, multiple inserts are another possibility.
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
Here's what I'm trying to do in SQL Query Analyzer.
Insert into db_name1.dbo.tb_name1 (select * from db_name2.dbo.tb_name2)
but I keep getting this error message
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ')'.
I wonder what went wrong. Does subquery isn't work in SQL server 2000?
What is the right syntax for this one? I would like to transfer all the values from a tables column of a database to another table of another database without creating any duplicate of its element but merely overwrites the current values.
This is your query as you think it should be interpeted:
Insert into db_name1.dbo.tb_name1
(select * from db_name2.dbo.tb_name2)
This is the query as the compiler sees it:
Insert into db_name1.dbo.tb_name1(select * from db_name2.dbo.tb_name2)
That is, the open parenthese after the table name means "the list of columns is starting here". But you don't have a list of columns. And select is not a valid column name. Hence the error.
This is easily fixed by removing the parentheses:
Insert into db_name1.dbo.tb_name1
select * from db_name2.dbo.tb_name2
However, it is more correct to include the column names being inserted:
Insert into db_name1.dbo.tb_name1(col1 . . .)
select * from db_name2.dbo.tb_name2;
It is a good idea to always include column names in insert statements. If you had done this, you wouldn't have had to ask this question.