How to write multiple T-sql in Oledb/ or any other dts component in SSIS

I'm writing ssis package ( using oledb source editor ) to query the db then save the result in disk drive as *.csv, the problem is oledb only can be use to run single T-sql command, so if I have a query like below, how do I run this query in ssis then save a results in single file destination ?

select DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 1 AND DomName <> ''
group by DomName order by count(*) desc
GO


select DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 2 AND DomName <> ''
group by DomName order by count(*) desc
GO

select DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 11 AND DomName <> ''
group by DomName order by count(*) desc
GO

select DomName, count(*) as count from TableTest with ( nolock )
where CatID = 2 and trgigerID = 2 AND DomName <> ''
group by DomName order by count(*) desc
GO

select DomName, count(*) as count from TableTest with ( nolock )
where CatID = 2 and trgigerID = 5 AND DomName <> ''
group by DomName order by count(*) desc
GO

select DomName, count(*) as count from TableTest with ( nolock )
where CatID = 4 and trgigerID = 6 AND DomName <> ''
group by DomName order by count(*) desc
GO

ssis-oledb.jpg
motioneyeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Olaf DoschkeSoftware DeveloperCommented:
one word: union

sql1 union all sql2 union all sql2...

Bye, Olaf.
motioneyeAuthor Commented:
Hi Olaf,
I'm cracking my head, well then u one word helping much indeed :)
motioneyeAuthor Commented:
Tested with the query, union / union all doesn't work with error below

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'Union'.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

kamindaCommented:
I also think union is the best shot here. Looks like you are getting a syntax error, please do share your new query
motioneyeAuthor Commented:
I tested with this two first


select DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 1 AND DomName <> ''
group by DomName order by count(*) desc
Union all
select DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 2 AND DomName <> ''
group by DomName order by count(*) desc
GO
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You cannot apply order by to the select part of the union, only to the overall union. Remove the first "order by".
However, in your case where you select from the same table with different criteria only, I would switch to a single statement with some "hidden" group by (hidden because the group columns are not appearing in the select list, as you have already used with domname):
select DomName, count(*) as count from TableTest with ( nolock )
where DomName <> '' and
     (CatID = 1 and trgigerID in (1, 2, 11)
      or
      CatID = 2 and trgigerID in (2, 5)
      or
      CatID = 4 and trgigerID in (6)
     )
group by DomName, CatID, trgigerID
order by count(*) desc

Open in new window

This will provide a count for each unique (domname, CatID, trgigerID) combination.
motioneyeAuthor Commented:
I tested and it return me with different result set, the results is no loner as what has been produced by the 6 T-sql command


select DomName, count(*) as count from TableTest with ( nolock )
where DomName <> '' and
     (CatID = 1 and trgigerID in (1, 2, 11)
      or
      CatID = 2 and trgigerID in (2, 5)
      or
      CatID = 4 and trgigerID in (6)
     )
group by DomName, CatID, trgigerID
order by count(*) desc
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I can't see why it should be different - with one exception, which is the ordering. But that would apply the same when using union.
Best to check with a single DomName. Just add a fixed DomName to the where clause for debugging.
Olaf DoschkeSoftware DeveloperCommented:
Sorry, didn't think about that aspect.

I don't agree with putting together the where clauses into one, as you implicitly not only group by DomName, but also the side conditions about CatID and triggerID. This is mangled together in a single select, eg you only have one record per DomName then.

You could introduce a computed column to group by additionally:

select Cast(1 as int) As groupid, DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 1 AND DomName <> ''
group by DomName
Union all
select Cast(2 as int) As groupid, DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 2 AND DomName <> ''
group by DomName
order by groupid asc, count desc
GO

Bye, Olaf.

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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Olaf,
Look at the group by: since CatID and trgigerID are contained in it, you get one record for each tuple, as I have already described.

motioneye,
Without displaying the grouping columns, or using some constructed value like above you do not have control over the different "branches" of the union (or my version of SQL).
motioneyeAuthor Commented:
Hi Olaf,
Thanks for your help, The query was return me the same values as my earlier post.

Hi Qlemo,
Thanks for your scripts too, actually I can see a reasons why both of you writing the code in different style,m indeed this is what I want :) since I can compare the data and convince the management how should they look into report :)

Olaf DoschkeSoftware DeveloperCommented:
Right, Qlemo, but introducing a groupid gives you more control about what's inside each group of record counts. You could also have where clauses which produce overlapping groups and this would stop working with an overall where clause approach. If this makes sense is depending on the meaning of the data of course.

For example, what would be, if you had these where clauses:
1. CatID in (1,2) and trgigerID in (1, 2, 11)
2. CatID in (2,3) and trgigerID in (2, 5)
3. CatID in (4) and trgigerID in (6)

Putting this together in one wehre and grouping by CatID, triggerID then won't help.

I just want to stay with appending the seperate results together, and this should do. Full query:

select CAST(1 as int) as groupid,  DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 1 AND DomName <> ''
group by DomName
union all
select CAST(2 as int) as groupid, DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 2 AND DomName <> ''
group by DomName
union all
select CAST(3 as int) as groupid, DomName, count(*) as count from TableTest with ( nolock )
where CatID = 1 and trgigerID = 11 AND DomName <> ''
group by DomName
union all
select CAST(4 as int) as groupid, DomName, count(*) as count from TableTest with ( nolock )
where CatID = 2 and trgigerID = 2 AND DomName <> ''
group by DomName
union all
select CAST(5 as int) as groupid, DomName, count(*) as count from TableTest with ( nolock )
where CatID = 2 and trgigerID = 5 AND DomName <> ''
group by DomName
union all
select CAST(6 as int) as groupid, DomName, count(*) as count from TableTest with ( nolock )
where CatID = 4 and trgigerID = 6 AND DomName <> ''
group by DomName
order by groupid asc, count desc

Open in new window


@motioneye, what is not working with this?

Bye, Olaf.
motioneyeAuthor Commented:
Hi Olaf,
As per subject and reason why I post this, due to ssis ole db source editor cannot accept multiple sql statement,  
Olaf DoschkeSoftware DeveloperCommented:
This is one statement now.

Bye, Olaf.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Olaf,
"introducing a groupid gives you more control" is what I said in http:#a35081457, but we still do not know if that is necessary at all, as it adds to the complexity. No doubt it is needed if the selection and grouping criteria are non-unique (overlapping). But if there is no overlap, always prefer the most simple approach.
motioneyeAuthor Commented:
Hi Olaf,
Yes thanks, your query written one statement, really helpful indeed :)
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.