Insert Data into Temp Table with Query

Insert Data Into Temp Table with Query

SELECT *
INTO #Temp
FROM

(SELECT
Received,
Total,
Answer,
(CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) AS application
FROM
FirstTable
WHERE
Recieved = 1 AND
application = 'MORESTUFF'
GROUP BY
CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) data
WHERE
application LIKE
isNull(
'%MORESTUFF%',
'%')

How To Insert Data Into Temp Table From Different Databases

you can use row_number() to generate a sequence number and use it to join the rows from Token with Notice

Seems like there is no relationship on how to link ChildValue with Table_No. Based on the expected result provided, I am assuming it will goes by it's value

SELECT t.ChildValue, n.TABLE_NO, '22' AS YEAR, ''
FROM
(
SELECT ChildValue,
RN = ROW_NUMBER() OVER (ORDER BY ChildValue)
FROM [DB1].[Version].[Token]
WHERE DeletedTransactionKey IS NULL
) t
INNER JOIN
(
SELECT TABLE_NO,
RN = ROW_NUMBER() OVER (ORDER BY TABLE_NO)
FROM [DB2].[dbo].[Notice]
) n
ON t.RN = n.RN

Insert data into temp table using select statement?

You need to give columns a name (here I have named the columns a and b):

Select a, b into #temp 
from
(
select a = 1, b = 2
Union
select 2, 4
Union
Select 8, 12
) as t

select * from #temp

a b
-----
1 2
2 4
8 12

Only the first SELECT clause of a UNION needs the explicit column names.

Inserting data into a temporary table

INSERT INTO #TempTable (ID, Date, Name) 
SELECT id, date, name
FROM physical_table

How to create Temp table with SELECT * INTO tempTable FROM CTE Query

Sample DDL

create table #Temp
(
EventID int,
EventTitle Varchar(50),
EventStartDate DateTime,
EventEndDate DatetIme,
EventEnumDays int,
EventStartTime Datetime,
EventEndTime DateTime,
EventRecurring Bit,
EventType int
)

;WITH Calendar
AS (SELECT /*...*/)

Insert Into #Temp
Select EventID, EventStartDate, EventEndDate, PlannedDate as [EventDates], Cast(PlannedDate As datetime) AS DT, Cast(EventStartTime As time) AS ST,Cast(EventEndTime As time) AS ET, EventTitle
,EventType from Calendar
where (PlannedDate >= GETDATE()) AND ',' + EventEnumDays + ',' like '%,' + cast(datepart(dw, PlannedDate) as char(1)) + ',%'
or EventEnumDays is null

Make sure that the table is deleted after use

If(OBJECT_ID('tempdb..#temp') Is Not Null)
Begin
Drop Table #Temp
End


Related Topics



Leave a reply



Submit