Question

The following shows two attempts at trying to insert data into a temp table using both a union query along with two extra columns (fldBF and sCount)...

CASE 1:

SELECT *, 1 AS fldBF, 
          ROW_NUMBER() OVER (PARTITION BY fldPK, fldCIA ORDER BY fldPK) AS sCount 
INTO #tmpTable 
FROM V_qryCSPGA 
WHERE fldPK IN(SELECT DISTINCT thePK 
      FROM FN_qryAllDTPK()) 
UNION ALL   
SELECT * 
FROM FN_qryCSGBA() 
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK())
ORDER BY fldPK, fldCIA, fldNDat;

CASE 2:

SELECT * INTO #tmpTable        
FROM V_qryCSPGA 
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK()) 
UNION ALL 
SELECT *, 1 AS fldBF, 
          ROW_NUMBER() OVER (PARTITION BY fldPK, fldCIA ORDER BY fldPK) AS sCount
FROM FN_qryCSGBA() 
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK())
ORDER BY fldPK, fldCIA, fldNDat;

In either case I receive the following error... 'All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.' Is there anyway for me to circumvent this without having to do a whole other insert of some sort?

Was it helpful?

Solution

You need to make sure both select queries are returning equal number of columns. As per comments, if you need to include extra columns, you can add static values to the other select query. So,

Adding (-1) as static values your CASE 1 would be like;

SELECT *, 1 AS fldBF, 
          ROW_NUMBER() OVER (PARTITION BY fldPK, fldCIA ORDER BY fldPK) AS sCount 
INTO #tmpTable 
FROM V_qryCSPGA 
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK()) 

UNION ALL   

SELECT *, -1 AS fldBF, -1 AS sCount --NOTE: Two static fields 
FROM FN_qryCSGBA() 
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK())
ORDER BY fldPK, fldCIA, fldNDat; 

You could do the same thing to the second query.

OTHER TIPS

Your queries are not equivalent. As the error message says, both select statements must contain the same columns. In your first example, only your first select statement has fldBF and sCount. In your second example, only your second query has fldBF and sCount. Because you are using the SELECT * syntax, you also can experience this issue if one table has more/less columns than the other.

You didn't post what the two input tables look like, so I'm going to assume they are slightly different tables. My suggestion would be to use a query like the following.

You may be able to remove the IN statements and use a join, but again I don't know what your tables look like.

SELECT 
    ROW_NUMBER() OVER(ORDER BY field1) AS custField1, 
    1 AS myInt, 
    * 
INTO #tmp_tbl 
FROM (
    SELECT 
        field1, 
        field2, 
        field3, 
        'X' AS field4 
    FROM V_qryCSPGA 
    WHERE my_pk IN (SELECT DISTINCT some_pk FROM My_Fn())
    UNION ALL
    SELECT 
        'A' AS field1, 
        field5 AS field2, 
        field3, 
        field4 
    FROM FN_qryCSGBA()
) X;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top