1

I am trying to turn my sql results into a temp table but it keeps on returning the following error:

Expecting As or Id or quoted-ID.

I know the sql result is correct but when I add SELECT * INTO #newtable FROM, the sql query doesn't work. What is wrong?

SELECT *
INTO #newtable
FROM 
    (
    SELECT
        Url,Id 
    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))
    )
14
  • try adding onto the end: as x Commented Jul 13, 2018 at 14:15
  • is this MySQL or sql server? the answers can differ Commented Jul 13, 2018 at 14:17
  • 1
    @RaymondNijland INSERT INTO ... SELECT and SELECT...INTO behave very differently. Commented Jul 13, 2018 at 14:22
  • 1
    @RaymondNijland it doesn't. it says it is "basically the same". I actually complete disagree with that. For SyBase (SQL Server now) if you were to run INSERT INTO dbo.MyTable SELECT 1 AS i; The statement will fail, however, SELECT 1 AS i INTO dbo.MyTable; will work fine. Conversely CREATE TABLE MyTable (i int); INSERT INTO dbo.MyTable SELECT 1 AS i; will work fine, but CREATE TABLE MyTable (i int); SELECT 1 AS i INTO dbo.MyTable; will not. Commented Jul 13, 2018 at 14:27
  • 1
    The SELECT..INTO syntax will create the object as part of the statement. if the object already exists, the statement fails. for INSERT INTO...SELECT the complete opposite is true; the object must already exist, and if it does not, the statement will fail. Commented Jul 13, 2018 at 14:29

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Answering your comment on your question, @Yash , as it's in relation to the answer. This would create the temporary table #newtable. Any table with a name beginning with # is a temporary table, and those with a single # will only persist for the duration of the connection that created it.
Thank you that makes sense.
1

I have tried your problem in your way only and it is working for me. Solution is like given below;

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))

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.