How can I insert the output of below table into newly created [DBname].[dbo].[Tablename]
?
The below T-SQL query outputs two columns: server
and name
.
T-SQL 1
if object_id ('tempdb..#temp1') is not null
drop table tempdb..#temp1
create table #temp1
([server] nvarchar(200),
[name] nvarchar(200),
)
insert into #temp1
SELECT (select @@SERVERNAME) as Server,name FROM master.sys.databases
select * from #temp1
I want to insert the output of T-SQL 1 into a newly created table [DBname].[dbo].[Tablename]
.I already created two columns server
and name
.
So far I tried like below:
Insert into [DBname].[dbo].[Tablename]
select
if object_id ('tempdb..#temp1') is not null
drop table tempdb..#temp1
create table #temp1
([server] nvarchar(200),
[name] nvarchar(200),
)
insert into #temp1
SELECT (select @@SERVERNAME) as Server,name FROM master.sys.databases
select * from #temp1
I used the Stack Overflow Q & A referenced below, but somehow could not do what I need:
How to do INSERT into a table records extracted from another table