MSSQL Table variable with Union

Hello there,

How can I union 2 table variable results together to get one result output. I was able to do this using View but for some reasons(passing variables to View not allowed) I had to replace my Views with Functions to be able to use in SSIS package.

My View query looked like this

SELECT
      productcode
    , productname
    , province
     , SUM(SalesQtyinclDis) AS SalesQty
    , SUM(SalesDiscount) AS SalesDiscount
    , ISNULL(SUM(salesReturn),0) AS SalesReturn
    , ISNULL( SUM(discountReturn),0) AS SalesDiscountReturn
FROM (
      SELECT
          productcode
        , productname
        , province
        , salesReturn    
        , discountReturn as discountReturn
        , cast(null as int) as SalesQtyinclDis
        , cast(null as decimal(12,3)) as SalesDiscount
      FROM ViewProductSaleReturnNDiscount
      UNION ALL
      SELECT
          productcode
        , productname
        , province
        , null    
        , null
        , SalesQtyinclDis
        , SalesDiscount as SalesDiscount
      FROM ViewProvinceProductSalesNDiscount
) sq
GROUP BY productcode
       , productname
       , province

Open in new window


Now my two functions are working and they look like so

SELECT
    *
FROM
   ProductSalesNDiscountByProvince('2015-07-23', '2015-08-23')
  

Open in new window

 
 SELECT
    *
FROM
   ProductSaleReturnNDiscountByProvince('2015-07-23', '2015-08-23')

Open in new window


cheers
Zolf
zolfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
Just UNION the functions. It might help to specifiy the column names as opposed to using SELECT *
SELECT * FROM ProductSalesNDiscountByProvince('2015-07-23', '2015-08-23')
UNION
SELECT * FROM ProductSaleReturnNDiscountByProvince('2015-07-23', '2015-08-23')

Open in new window

0
zolfAuthor Commented:
Thanks for your comment, but if you see my UNION Query of the View I have a Select statement encapsulating the 2 VIEWS. How will this be done here for table variable
0
MlandaTCommented:
I'm not really understanding the problem then. The point is that you can stull use the functions in the very say way you were using other views.

The only change is the parameters. The functions are filtering by date. Where your previous views were not. You could either, just use a very wide date range, OR program your function to accept null inputs on both parameters, which would then make it ignore the data filters.
SELECT
      productcode
    , productname
    , province
     , SUM(SalesQtyinclDis) AS SalesQty
    , SUM(SalesDiscount) AS SalesDiscount
    , ISNULL(SUM(salesReturn),0) AS SalesReturn
    , ISNULL( SUM(discountReturn),0) AS SalesDiscountReturn
FROM (
      SELECT
          productcode
        , productname
        , province
        , salesReturn    
        , discountReturn as discountReturn
        , cast(null as int) as SalesQtyinclDis
        , cast(null as decimal(12,3)) as SalesDiscount
      FROM ProductSaleReturnNDiscountByProvince('2015-07-23', '2015-08-23')
      UNION ALL
      SELECT
          productcode
        , productname
        , province
        , null    
        , null
        , SalesQtyinclDis
        , SalesDiscount as SalesDiscount
      FROM ProductSalesNDiscountByProvince('2015-07-23', '2015-08-23')
) sq
GROUP BY productcode
       , productname
       , province

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

zolfAuthor Commented:
I get this error

Msg 207, Level 16, State 1, Line 14
Invalid column name 'salesReturn'.
Msg 207, Level 16, State 1, Line 15
Invalid column name 'discountReturn'.
0
MlandaTCommented:
1 - I hope I put the right functions in the right order.
2 - Do the functions return the columns salesReturn and discountReturn?

Does this version work?
SELECT
      productcode
    , productname
    , province
     , SUM(SalesQtyinclDis) AS SalesQty
    , SUM(SalesDiscount) AS SalesDiscount
    , ISNULL(SUM(salesReturn),0) AS SalesReturn
    , ISNULL( SUM(discountReturn),0) AS SalesDiscountReturn
FROM (
      SELECT
          productcode
        , productname
        , province
        , salesReturn    
        , discountReturn as discountReturn
        , cast(null as int) as SalesQtyinclDis
        , cast(null as decimal(12,3)) as SalesDiscount
      FROM ProductSalesNDiscountByProvince('2015-07-23', '2015-08-23')
      UNION ALL
      SELECT
          productcode
        , productname
        , province
        , null    
        , null
        , SalesQtyinclDis
        , SalesDiscount as SalesDiscount
      FROM ProductSaleReturnNDiscountByProvince('2015-07-23', '2015-08-23')
) sq
GROUP BY productcode
       , productname
       , province

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zolfAuthor Commented:
cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.