1

The following code

IF OBJECT_ID('tempdb..#temp') IS NOT NULL
        DROP TABLE #temp

SELECT *
INTO #temp
FROM (SELECT * FROM tbl)

Gives me a syntax error right after last parenthesis

I don't understand why at all

1
  • 1
    Which RDBMS is this for? Please add a tag to specify whether you're using mysql, postgresql, sql-server, oracle or db2 - or something else entirely. Commented Sep 14, 2015 at 16:31

2 Answers 2

3

You don't need subquery:

SELECT * 
INTO #temp
FROM tbl;

Or add alias:

SELECT t.*
INTO #temp
FROM (SELECT * FROM tbl) AS t

You can also use CTE for more complex subqueries:

;WITH cte AS
(
   SELECT *
   FROM tbl
)
SELECT cte.*
INTO #temp
FROM cte;
Sign up to request clarification or add additional context in comments.

2 Comments

Wel that isn't the whole code actually the select * from tbl is actually a sub query
@Kamster You need to add alias
2

The SELECT * FROM tbl should have an alias defined.

SELECT t.*
INTO #temp
FROM (SELECT * FROM tbl) t

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.