Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating an SQL Temp Table?


Creating an SQL Temp Table?

Author
Message
meadow0
meadow0
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 48
Hi guys,

I had reached out to everyone earlier today to ask for help, and I was able to take a lot away from that, so thank you in advance.

I'm looking to turn the query that I created into a "Temp Table".

I've searched far and wide on the internet and can find little to no literature on it.

Can anyone help me get started with creating a temp table?

The previous query was as follows:

SELECT A.Portfolio, B.PortfolioID, SUM(B.MarketValue) AS SumOfMV,
COUNT(B.SecID) AS [# of Securities]
From ERTutAccounts A
INNER JOIN ERTutPositions B ON A.PortfolioID = B.PortfolioID
GROUP BY A.Portfolio, B.PortfolioID
HAVING COUNT(B.SecID) > 70 OR SUM(B.MarketValue) > 30000000

Before attempting to build the above query into a temp table, I simply attempted a really trivial table to no avail. Can someone tell me what I'm doing wrong and the right place to begin?

CREATE TABLE #Test
(PortfolioID int,
Portfolio varchar(200))

INSERT INTO #Test (PortfolioID, Portfolio)
SELECT PortfolioID, Portfolio
FROM ERTutAccounts
WHERE PortfolioID = 1
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16632 Visits: 17024
What is the issue you are having? The query you posted looks like it should work fine, assuming the base table exists.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
meadow0
meadow0
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 48
Hi Sean,

The query itself works fine, I'm having an issue converting this into a temp table. I'm not even quite sure how temp tables work in theory, but I'm attempting to convert the functional query into a temp table.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16632 Visits: 17024
meadow0 (3/5/2013)
Hi Sean,

The query itself works fine, I'm having an issue converting this into a temp table. I'm not even quite sure how temp tables work in theory, but I'm attempting to convert the functional query into a temp table.


I don't think I understand what you mean about converting a query into a temp table. Temp tables work just like a permanent table but they will go away when the connection it closed, or it is dropped.

You could have your original query generate and populate a temp table if you want quite easily.


SELECT A.Portfolio, B.PortfolioID, SUM(B.MarketValue) AS SumOfMV,
COUNT(B.SecID) AS [# of Securities]
INTO #YourTempTable
From ERTutAccounts A
INNER JOIN ERTutPositions B ON A.PortfolioID = B.PortfolioID
GROUP BY A.Portfolio, B.PortfolioID
HAVING COUNT(B.SecID) > 70 OR SUM(B.MarketValue) > 30000000



If you execute this you will now have a temp table called #YourTempTable that has the results of your above query. You can now treat it like any other table for the duration of your process.

--EDIT--
Misplaced quotes.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search