Inserting Data into a Temporary Table

Inserting data into a temporary table

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

Insert into table from temporary table

You could use an insert-select statement:

INSERT INTO test
SELECT @col1, @col2, col3
FROM #temp

How to insert data into a temporary table using an existing table and new columns

I think this is your problem:

SET @Link2 = (SELECT USER.ID FROM USER WHERE USER.Registered = 'Yes')

What if there are six of them? A single variable can't hold all of them. You can do:

SELECT TOP(1) @Link2 = USER.ID FROM USER WHERE USER.Registered = 'Yes' ORDER BY [SOMETHING];

If the goal is to create a temp table with a full [WEBSITE_LINK], you can do that without all those variables:

BEGIN
CREATE TABLE #TempTable
(
[ID] [varchar](10),
[FIRST_NAME] varchar(50),
[LAST_NAME] varchar(50),
[WEBSITE_LINK] varchar(200)
)

INSERT INTO #TempTable
SELECT DISTINCT u.ID
, [FIRST_NAME] = u.FIRSTNAME
, [LAST_NAME] = u.LASTNAME
, [WEBSITE_LINK] = 'http://www.mywebsite.com/user/' +
CAST(u.ID AS VARCHAR(10)) +
'/document.doc'
FROM [USER] u
WHERE u.Registered = 'Yes'

-- Do something with these values...

DROP TABLE #TempTable
END

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

Is it possible to insert into temporary table in spark?

We can't insert data into the temporary table but we can mimic the insert with union all (or) union(to remove duplicates).

Example:

#create temp view
spark.sql("""create or replace temporary view temp_view_t as select 1 as no, 'aaa' as str""")

spark.sql("select * from temp_view_t").show()
#+---+---+
#| no|str|
#+---+---+
#| 1|aaa|
#+---+---+

#union all with the new data
spark.sql("""create or replace temporary view temp_view_t as select * from temp_view_t union all select 2 as no, 'bbb' as str""")

spark.sql("select * from temp_view_t").show()
#+---+---+
#| no|str|
#+---+---+
#| 1|aaa|
#| 2|bbb|
#+---+---+

#to eliminate duplicates we can use union also.
spark.sql("""create or replace temporary view temp_view_t as select * from temp_view_t union select 1 as no, 'aaa' as str""")

spark.sql("select * from temp_view_t").show()
#+---+---+
#| no|str|
#+---+---+
#| 1|aaa|
#| 2|bbb|
#+---+---+

How to insert into a table from temp table?

Correct syntax:

insert into MyTable(Id,Name)
select t.ID, t.Name
From #temp t

Always read manual

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


Related Topics



Leave a reply



Submit