I need to select these fields into a temp table using SELECT INTO instead of creating the temp table and all of its columns first. The District DBA wants to just add or remove fields without having to update the table every time:
BEGIN TRAN
SELECT DISTINCT
@C1 AS C1 ,
@C2 AS C2 ,
@C3 AS C3 ,
@C4 AS C4 ,
@C5 as C5,
@C6 as C6,
@C7 as C7,
@C8 as C8,
@C9 as C9,
@C10 as C10,
@C11,
@C12,
@C13,
@C14,
@C15,
C16,
C17,
C18,
C19,
C20,
C21,
0,0,0,0, 0, 0, 0, @C22
FROM ( SELECT Field1 ,
Field2,
Field3,
Field4,
Field5,
Field6
FROM ( SELECT Field1,
UPPER(ISNULL(Field2, 'UNDEFINED')) AS Field2 ,
UPPER(ISNULL(Field3, 'UNDEFINED')) AS Field3,
UPPER(ISNULL(Field4,
'UNDEFINED')) AS Field4 ,
UPPER(ISNULL(Field5, 'UNDEFINED')) AS Field5,
UPPER(ISNULL(Field6,
'UNDEFINED')) AS Field6
FROM dbo.MyTableOne
WHERE Booktype = 'Fiction'
GROUP BY Field1,
UPPER(ISNULL(Field2, 'UNDEFINED')) ,
UPPER(ISNULL(Field3, 'UNDEFINED')) ,
UPPER(ISNULL(Field4,
'UNDEFINED')) ,
UPPER(ISNULL(Field5, 'UNDEFINED')) ,
UPPER(ISNULL(Field6,
'UNDEFINED'))
UNION ALL
SELECT Field1,
Field2, ,
Field3,
Field4,
Field5,
Field6,
FROM dbo.MyTableTwo
WHERE Booktype = 'Fiction'
UNION ALL
SELECT 'TitlesAll' as Field1,
Field2,
Field3,
Field4,
Field5,
Field6
FROM dbo.MyTableThree
Where Booktype = 'Fiction'
) X
) Y
This should be fairly easy - I have used SELECT INTO many times.
What I have tried:
I have tried including the INTO #temptable before the first "FROM" and then including it in every "FROM", but that did not work. I also used google search and I checked the MDSN website.