How SQL Query Result Insert in Temp Table

How SQL query result insert in temp table?

Look at SELECT INTO. This will create a new table for you, which can be temporary if you want by prefixing the table name with a pound sign (#).

For example, you can do:

SELECT * 
INTO #YourTempTable
FROM YourReportQuery

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

Inserting data into a temporary table

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

How to insert the output of a SQL Query into a temporary table?

In create temp table, you need to specify the columns and datatypes too. Temp table is created just like a persistant table.
However you can use Select Into statement, which will create the table automatically based on the given Select statement
Try This:

USE MyDatabase
GO

IF OBJECT_ID('tempdb..#TempTable') Is Not null
Drop Table #TempTable

SELECT [Col A], [Col B], [Col C]
INTO #TempTable -- <<<<<
FROM MYLIST

WHERE [MONTH OF STAY] = '2018-03-01'

AND [Property] = 'ABC'

SELECT * FROM #temptable

In above code, you need to check if table with the given name is exists or not? If exists, then you will need to drop it, prior to execute the Select Into. Because Select Into will automatically create the table for you, and if table with same name exists, then you will get error.

Different results while inserting into temp table and table variable

In relational database, your table is a set. It means that the ORDER BY and your GROUP BY of you insert is not needed.

INSERT INTO @sortedArticleIds
SELECT article_id, NULL AS groupBy
FROM #articleIds

Here you are updating a table, so we don't need an ORDER BY clause.

But when you will query your table, prefer a query like this.

SELECT *
FROM @sortedArticleIds
GROUP BY article_id
ORDER BY MIN(sortBy) DESC;

Insert result into temp table mariadb

You have very complicated logic for something that seems rather simple. I think this does what you want:

INSERT INTO machinenametable (id, name)
SELECT 1,
(CASE WHEN md.identifier LIKE 'PP%' THEN 'PP'
WHEN md.identifier LIKE 'SHL' THEN 'SHL'
ELSE 'SL'
END)
FROM machine m
WHERE @serial = m.serial;

The CTE do nothing useful. And the use of IF instead of CASE is convoluted. And using SELECT for constants is simply unnecessary.

Converting SQL query result to temp table

Not sure why you have the nested SELECTs. This is what you're more likely after, as you then don't have to alias your subquery:

SELECT Url,
Id
INTO #newtable
FROM Blob
WHERE Id IN (SELECT BlobId FROM XrefBlobProjectMeeting)
AND Extension NOT IN ('xlsx', 'xls', 'avi', 'jpg', 'mp4', 'wmv', 'png')
AND (RefContentTypeId IN (11, 13, 14, 35));

You'd be ever better off, however, changing the IN to an EXISTS as well though:

SELECT [Url],
Id
INTO #newtable
FROM Blob B
WHERE EXISTS (SELECT 1
FROM XrefBlobProjectMeeting E
WHERE E.BlobID = B.ID)
AND Extension NOT IN ('xlsx', 'xls', 'avi', 'jpg', 'mp4', 'wmv', 'png')
AND RefContentTypeId IN (11, 13, 14, 35);


Related Topics



Leave a reply



Submit