0

I am trying to add a temp table to my query so that I can query that temp table, I have searched the internet but I couldn't get a solution. this is my query

;WITH cte AS (
        SELECT ID, g.Name
        FROM game.Game g WITH(NOLOCK
        WHERE ID IN (SELECT Data FROM system.Split(1, ','))
        UNION ALL 
        SELECT g.ID,  g.Name
        FROM game.Game g WITH(NOLOCK) 
            JOIN cte ON g.ParentID = cte.ID
    )

    SELECT  c.ID,
        c.Name

    FROM cte c
        INNER JOIN list.Type gt WITH(NOLOCK) ON c.TypeId = gt.TypeID

    WHERE c.ID NOT IN (SELECT Data FROM system.Split(1, ','))
        AND c.ID IN (SELECT ID FROM game.code WITH(NOLOCK)
        WHERE ID = c.ID
            AND StatusCode IN ('OP', 'CL', 'SU')
            AND isDisplay = 'True' 
            AND GETDATE() BETWEEN DisplayStart AND DisplayEnd
            AND GETDATE() < ISNULL(ResultDateTime, ResultExpected) 
        )

which gives me the following when I run it

ID    | Name
1111  | BaseBall
2222  |BasketBall
45896 |Relay

now I tried to create a temp table as follows

Create Table #temp(
        ID int,
        Name varchar
        )
        ;WITH cte AS (
        SELECT ID, g.Name
        FROM game.Game g WITH(NOLOCK)
        WHERE ID IN (SELECT Data FROM system.Split(1, ','))
        UNION ALL 
        SELECT g.ID,  g.Name
        FROM game.Game g WITH(NOLOCK) 
            JOIN cte ON g.ParentID = cte.ID
    )
    insert into #temp  // i wanted to set these values in the temp table
    SELECT  c.ID,
        c.Name

    FROM cte c
        INNER JOIN list.Type gt WITH(NOLOCK) ON c.TypeId = gt.TypeID

    WHERE c.ID NOT IN (SELECT Data FROM system.Split(1, ','))
        AND c.ID IN (SELECT ID FROM game.code WITH(NOLOCK)
        WHERE ID = c.ID
            AND StatusCode IN ('OP', 'CL', 'SU')
            AND isDisplay = 'True' 
            AND GETDATE() BETWEEN DisplayStart AND DisplayEnd
            AND GETDATE() < ISNULL(ResultDateTime, ResultExpected) 
        )

every time I try to store this information in the temp table it gives me an error 'Column name or number of supplied values does not match table definition.' But I only have two values in. What am I doing wrong that I cant see?

3
  • 2
    Why are you using NOLOCK? Commented May 11, 2018 at 14:41
  • What is length of varchar column in Temp table u have given ? Commented May 11, 2018 at 14:42
  • 1
    To expand and @Larnu's comment...blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere And do you have a column named 1? That split function looks pretty suspect to me. Commented May 11, 2018 at 14:54

1 Answer 1

2

First, why not just use select into?

IF OBJECT_ID('TempDB..#temp') IS NOT NULL  
BEGIN
    DROP TABLE #temp
END
select c.ID, c.Name
into #temp
from . . .

Then you don't need to define #temp as a table.

Next, your definition is bad, because Name has only one character. This would be fixed with select into.

However, I don't know why you are getting the particular error you are getting. The numbers of columns appears to match.

Sign up to request clarification or add additional context in comments.

1 Comment

Be aware while executing this as in same session it will throw error.

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.