Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SSIS and Import/Export (2005)
 Count rows in access db

Author  Topic 

lols
Posting Yak Master

174 Posts

Posted - 2007-10-30 : 09:48:35
Hello,

I am doing a SSIS package to transfer rows from access db to sql server using foreach file. Before loading the data, I want to count the number of rows in a table of the access db. How do I do that?

thank you in advance

Kristen
Test

22859 Posts

Posted - 2007-10-30 : 09:59:55
You could probably use OPENQUERY or OPENROWSET to run a query (e.g. "SELECT COUNT(*) FROM MyAccessTable") against the Access database

Kristen
Go to Top of Page

lols
Posting Yak Master

174 Posts

Posted - 2007-10-30 : 10:57:19
I tried this :

SELECT count(*) FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'c:\northwind.mdb';'user';'pass', Customers)
GO

Since I am using the foreach file, i am using a variable to get the filename of the access db. How do i pass a variable to the OPENROWSET command instead of the hardcoded mdb path?

thanks for the help
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-30 : 11:27:35
I think this will be more efficient (your way will have to pass all columns of all rows in the table between Access and SQL Server, and then make the Count)

SELECT MyCount FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'c:\northwind.mdb';'user';'pass', 'SELECT COUNT(*) AS MyCount FROM Customers')

To make that "dynamic" you need something like

DECLARE @strSQL varchar(8000)
SELECT @strSQL = 'SELECT MyCount FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'', ''' + @MyTableName + ''';''user'';''pass'', ''SELECT COUNT(*) AS MyCount FROM Customers'')'
PRINT @strSQL -- Debug use to check that the SQL is sensible
EXEC (@strSQL)

Kristen
Go to Top of Page

lols
Posting Yak Master

174 Posts

Posted - 2007-10-30 : 12:59:51
you have been very helpful :) I will check it out
Go to Top of Page
   

- Advertisement -