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
How to Generate All Possible Data Combinations in SQL
Psycopg2 Equivalent of MySQLdb.Escape_String
Invalid Column Name on SQL Server Update After Column Create
For Autoincrement Fields: Max(Id) VS Top 1 Id Order by Id Desc
Transact-SQL Shorthand Join Syntax
SQL Oracle Left Join and Subquery Error: Ora-00905: Missing Keyword
Laravel Eloquent to Join Table and Count Related
With Hibernate, How to Query a Table and Return a Hashmap with Key Value Pair Id>Name
Is Order by Clause Allowed in a Subquery
Order of Ands in Where Clause for Greatest Performance
What Is the SQL for 'Next' and 'Previous' in a Table
Autoincrement Fields on Databases Without Autoincrement Field
Group by Values That Are in Sequence
Recursive Stored Functions in MySQL
SQL to Find Time Elapsed from Multiple Overlapping Intervals
Do Ddl Statements Always Give You an Implicit Commit, or Can You Get an Implicit Rollback