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
 Transact-SQL (2005)
 Help with Query

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2008-05-16 : 11:06:48
I want to be able to loop through roughtly 250 tables and retreive the records whose rn_create_user or rn_edit_user do not equal the user_id found in the user table (an obvious join). In my loop, I will also need to check if the rn_create_user or rn_edit_user COLUMNS exist (in some cases they do not), and second, if there is a value in these columns.

I first looked at generating dynamic sql through a previous post but do not think it needs to be this complicated.

Much thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-16 : 11:29:03
Have you tried using sp_Msforeachtable stored procedure? more detail here:-

http://www.databasejournal.com/features/mssql/article.php/3441031
Go to Top of Page

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2008-05-16 : 11:36:49
I have the following using the sp_MSforeachtable Stored Procedure.
I would like to modify the below logic to exclude tables where the
rn_edit_user or rn_create_user is not defined and also where they are
defined but not set.

I also get the message "The query has exceeded the maximum number of result sets that can be displayed in the results grid. Only the first 100 result sets are displayed in the grid.". How do I display values where there are more than 100 values in the grid? Can this be done or is this a limitation of SQL Server?

EXECUTE sp_MSforeachtable 'select o.*
from ? as o
where not exists(select * from users u
where (u.users_id = o.rn_create_user) or (u.users_id = o.rn_edit_user)
)';
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-16 : 11:51:50
quote:
Originally posted by qman

I have the following using the sp_MSforeachtable Stored Procedure.
I would like to modify the below logic to exclude tables where the
rn_edit_user or rn_create_user is not defined and also where they are
defined but not set.

I also get the message "The query has exceeded the maximum number of result sets that can be displayed in the results grid. Only the first 100 result sets are displayed in the grid.". How do I display values where there are more than 100 values in the grid? Can this be done or is this a limitation of SQL Server?

EXECUTE sp_MSforeachtable 'select o.*
from ? as o
where not exists(select * from users u
where (u.users_id = o.rn_create_user) or (u.users_id = o.rn_edit_user)
)';


it seems like your resultset is fetching a lot of rows while looping through tables. What you could try is to reduce the records returned by using top x clause.
Go to Top of Page

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2008-05-16 : 13:24:25
It still fails even after adding the top 5 check.
Also, I would like to exclude tables that do not have a 'rn_edit_user or 'rn_edit_user' column defined in its table definition.

EXECUTE sp_MSforeachtable 'select top 5 o.*
from ? as o
where not exists(select * from users u
where (u.users_id = o.rn_create_user) or (u.users_id = o.rn_edit_user)
)';

Msg 207, Level 16, State 1, Line 4
Invalid column name 'rn_edit_user'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'rn_create_user'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'rn_edit_user'.
The query has exceeded the maximum number of result sets that can be displayed in the results grid. Only the first 100 result sets are displayed in the grid.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-16 : 13:37:54
May be this:-

EXECUTE sp_MSforeachtable '
IF COL_LENGTH(''?'',''rn_create_user'') >0 AND COL_LENGTH(''?'',''rn_edit_user'')>0
select top 5 o.*
from ? as o
where not exists(select * from users u
where (u.users_id = o.rn_create_user) or (u.users_id = o.rn_edit_user)
)';
Go to Top of Page
   

- Advertisement -