When I try to execute this code :
insert into myTable
select col1, col2
from #temp_Table
I got this error:
Invalid Object Name #temp_Table
I am writing this code in SAP Business one as a SQL query and the temp table itself is defined already in the same query.
When I only run
select col1, col2
from #temp_Table
it works fine. Only as insert into does not.
Any suggestion what the problem could be?
Related
This question already has answers here:
How to create table using select query in SQL Server?
(4 answers)
Closed 9 years ago.
I want to create a table from select query result in SQL Server, I tried
create table temp AS select.....
but I got an error
Incorrect syntax near the keyword 'AS'
Use following syntax to create new table from old table in SQL server 2008
Select * into new_table from old_table
use SELECT...INTO
The SELECT INTO statement creates a new table and populates it with
the result set of the SELECT statement. SELECT INTO can be used to
combine data from several tables or views into one table. It can also
be used to create a new table that contains data selected from a
linked server.
Example,
SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM tablename
Inserting Rows by Using SELECT INTO
Standard Syntax,
SELECT col1, ....., col# -- <<== select as many columns as you want
INTO [New tableName]
FROM [Source Table Name]
Please be careful,
MSSQL: "SELECT * INTO NewTable FROM OldTable"
is not always the same as
MYSQL: "create table temp AS select.."
I think that there are occasions when this (in MSSQL)
does not guarantee that all the fields in the new table are of the same type as the old.
For example :
create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable
does not always yield:
create table newTable (field1 varchar(10), field2 integer, field3 float)
but may be:
create table newTable (field1 varchar(10), field2 integer, field3 integer)
Please try:
SELECT * INTO NewTable FROM OldTable
Try using SELECT INTO....
SELECT ....
INTO TABLE_NAME(table you want to create)
FROM source_table
Select [Column Name] into [New Table] from [Source Table]
I'm working on a script, just experimenting and developing. A lot of what I do involves temp tables, where I will select ... into #SomeTable from ..., at least in the initial experimentation/early dev stage.
Then I'll look at the results, make some changes, and go again. But to make things a little easier on myself, I also have a drop table if exists #SomeTable, so I can just rerun the code. So far so good. But, if I add a column to the temp table and then try to access it, I get the error Invalid column name 'newColumn'. I can avoid the error by executing JUST the drop statement, and then executing the whole script, but I would rather not have to do that. As I understand it, there is some caching of temp tables that goes on, and I wonder if that's the culprit. In any case, is there any way to fix this?
Edit: Here is a short script demonstrating the problem. Run the script, then uncomment both comments and run it again:
drop table if exists #DemoTable
select column1 = '1'
,column2 = '2'
-- ,column3 = '3'
into #DemoTable
select column1
,column2
-- ,column3
from #DemoTable
In SSMS you can just add a GO statement to separate the code in 2 batches.
This way the drop is executed before the 2nd part of the script is checked for errors.
drop table if exists #DemoTable
GO
select column1 = '1'
,column2 = '2'
-- ,column3 = '3'
into #DemoTable
select column1
,column2
-- ,column3
from #DemoTable
I have tried to follow the steps mentioned in the question.
Please find the following query samples using Northwind Database.
Added a new column to the temp table and also updated it sucessfully.
-- Create the temporary table #temp_Employees from a physical table called 'Employee' in schema 'dbo' in database 'Northwind'
SELECT EmployeeID, FirstName, LastName , BirthDate
INTO #temp_Employees
FROM [Northwind].[dbo].[Employees]
-- SELECT data from temptable '[#temp_Employees]'
SELECT * FROM #temp_Employees;
-- Add a new column '[City]' to table '[#temp_Employees]'
ALTER TABLE #temp_Employees
ADD [City] NVARCHAR(15) NULL
GO
-- SELECT data from temptable '[#temp_Employees]'
SELECT * FROM #temp_Employees;
-- UPDATE the new column '[City]' in the table '[#temp_Employees]'
UPDATE T
SET T.[City]=E.[City]
FROM #temp_Employees T INNER JOIN [Northwind].[dbo].[Employees] E ON T.EmployeeID=E.EmployeeID;
-- SELECT data from temptable '[#temp_Employees]'
SELECT * FROM #temp_Employees;
-- Drop the temptable if it already exists
IF OBJECT_ID('tempDB..#temp_Employees', 'U') IS NOT NULL
DROP TABLE #temp_Employees;
GO
-- SELECT data from temptable '[#temp_Employees]'
SELECT * FROM #temp_Employees;
Thank you.
insert into ASSET_MAIN_CATEGORIES values(select max(sno) from ASSET_MAIN_CATEGORIES,
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1);
I wanted to insert max of SNO as column value. But It is showing "Missing Expression Error"
How can I achieve this.
Any Help would be appreciated.
There are at least ways of doing it:
INSERT INTO ... VALUES with embedded select in parentheses so that database will evaluate it:
insert into ASSET_MAIN_CATEGORIES values(
(select max(sno) from ASSET_MAIN_CATEGORIES),
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1
);
INSERT INTO ... SELECT since all other data is static:
insert into ASSET_MAIN_CATEGORIES
select
max(sno),
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1
from ASSET_MAIN_CATEGORIES
;
Note that if you do not specify which columns you are populating in ASSET_MAIN_CATEGORIES database assumes that you're feeding values for all of them in the order that they were created in this table.
You need to surround select max with brackets, because it's a SQL statement:
insert into ASSET_MAIN_CATEGORIES values((select max(sno) from ASSET_MAIN_CATEGORIES), 'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1);
If you have an error, prefer to add column names in insert statement
this will work :-
i have checked with my tables its working ,sample code to understand:
insert into b(col1,col2) select 7,(select max(col2) from b)
from dual;
what code you require:-
insert into ASSET_MAIN_CATEGORIES(col1,col2,....,col10)
select (select max(col2) from ASSET_MAIN_CATEGORIES), 'PROD','AC
HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1)
from dual;
I am trying to insert a set of records into a table using
Insert into tbl1 Select * from tbl
One of the records failed due to check constrain in tbl1. But i want to insert the other records which have passed the check constraint and others i want to catch them as exception. Could someone please help.
In that case, you need to be more selective about your SELECT - exclude those rows that are trouble from your selection:
INSERT INTO dbo.tbl1(Col1, Col2, ...., ColN)
SELECT Col1, Col2, ....., ColN
FROM dbo.tbl
WHERE (some condition here to exclude those rows that don't match the `CHECK` constraint.....)
UPDATE : The issue was col1 was hiereachyid type and even a select didnt work for it.
Hi all,
I am getting this error -
Objects exposing columns with CLR types are not allowed in distributed queries. Please use a pass-through query to access remote object '"RemoteDb"."dbo"."RemoteTable"'.
I have already setup the linked server [RemoteServer.dev.com].I was trying to perform an bulk insert from a remote table into the current table something like this -
INSERT INTO [CurrentDb].[dbo].[Mytable]
(
col1,
col2
)
SELECT
col1,col2
FROM [RemoteServer.dev.com].[RemoteDb].[dbo].[RemoteTable]
Can anyone please help me out..thanks.
As the error indicates, you need a pass-through query here because of the datatypes. Try this:
INSERT INTO [CurrentDb].[dbo].[Mytable]
(
col1,
col2
)
SELECT col1, col2
FROM OPENQUERY([RemoteServer.dev.com], 'SELECT col1, col2 FROM [RemoteDb].[dbo].[RemoteTable]')