SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 urgent for me
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

chbala85
Starting Member

49 Posts

Posted - 07/15/2013 :  02:05:36  Show Profile  Reply with Quote
Hi,

i have database test007DB and i need count all table rows then insert into test99 table using ssis packages .

test99: tableName countRows
t1 20
t2 30
t3 25

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/15/2013 :  02:16:31  Show Profile  Reply with Quote
use catalog view INFORMATION_SCHEMA.TABLE for getting table details.
Create a variable Tables of type object in SSIS to hold result inside execute sql task with statement as

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLE WHERE TABLE_TYPE+'BASE TABLE'
and map variable in result mapping tab.
Use a for each loop for iteration in SSIS
Create a variable @[User::TableName] to get tablenames inside loop
Then use query as "INSERT DestinationTable SELECT '" + @[User::TableName] + "',COUNT(*) FROM " + @[User::TableName]
set connection to your db
executing package will give you table names with counts in your table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/15/2013 :  02:32:07  Show Profile  Reply with Quote
So steps will be like

1. Create a execute sql task with statement as

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLE WHERE TABLE_TYPE = 'BASE TABLE'

Set CommandType = 'Full Resultset'

and create an object variable called Tables and map it in variable mapping tab.

2. Use a for each loop to iterate through the object variable and create a string variable TableName to get each iteration value

3. create a variable sqlquery to hold query, set EvaluateAsExpression property tru and set expression as

"INSERT DestinationTable SELECT '" + @[User::TableName] + "',COUNT(*) FROM " + @[User::TableName]

4. Use a execute sql task inside loop
set command type as from variable and map it to SQLAuery variable created above

5. execute package to get table populated with required data

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000