Select syntax as a subquery to insert into - sql

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.

Related

Change SSRS parameter value into column name in query

I am trying to, without stored procedures, create a SQL query in an SSRS report and take a parameter value and use it as a column name. My question is how to I change the parameter value into the actual column name? I have tried the following query with the table identifier (i.e. t.#FilterColumn1) and without the table identifier (i.e. #FilterColumn1).
DECLARE #FilterColumn1 AS nvarchar(250)
SET #FilterColumn1 = 'Column1'
SELECT DISTINCT
t.#FilterColumn1
FROM [JetNavDwh2017].[dbo].[TableName] t WITH(NOLOCK)
When I do not use the table identifier the result is the the column name I defined. When I use the table identifier I receive the error message below, but I have not found a conclusive way to resolve the issue based on the error. Thank you in advance for your help!
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '#FilterColumn1'.
Msg 319, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

Subqueries for PostgreSQL returns syntax error near select

I am a beginner to database, and I am trying to run a subquery to insert data from one table to another one with identical schema.
insert into tbl_technologies_used (t_name_tech, t_category_tech, i_rating)
values (
select t_name_tech, t_category_tech, i_rating
from tbl_technologies_proposed
);
But I got this error:
ERROR: syntax error at or near "select"
LINE 1: ... (t_name_tech, t_category_tech, i_rating) values (select t_n...
How can I fix this issue? I have checked my code again and again, but I can't find out the error.
If the source of an INSERT is a SELECT, you can't use VALUES:
insert into tbl_technologies_used (t_name_tech, t_category_tech, i_rating)
select t_name_tech, t_category_tech, i_rating
from tbl_technologies_proposed
The values clause provides a static set of rows which is not needed as the rows to be inserted come from your SELECT statement.

Create table from a table in SQL Server

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

I want to create a new table from a SQL select statement?

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

insert multiple rows in single SQL

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')