March 7, 2013 at 2:29 pm
I have a database with a large number of views. I would like to to be able to loop through the views and run a select on first record in each table from each of the views. Any advice or suggestions would be greatly appreciated.
Charlie
March 7, 2013 at 3:18 pm
This should get you started..
declare @strSQL varchar(max)
declare @viewName varchar(255)
declare listViews cursor for
select name from sys.views
open listViews
fetch next from listViews into @viewName
while @@fetch_status = 0
begin
set @strSQL = 'select top 1 * from ' + @viewName
exec (@strSQL)
fetch next from listViews into @viewName
end
close listViews
deallocate listViews
March 7, 2013 at 3:23 pm
Cursors, loops and dSQL fall under the last choice column but this is one of those cases...
For tables you would do this:
EXEC sp_MSforeachtable'SELECT TOP 1 * FROM ?'
For views:
SET NOCOUNT ON;
GO
IF OBJECT_ID('tempdb..#views') IS NOT NULL
DROP TABLE #views;
;WITH views AS
(SELECTROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n,
TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS)
SELECT n, table_name
INTO #views
FROM views;
GO
--SELECT * FROM #views
DECLARE @n int=1,
@tbl varchar(100),
@sql varchar(400)='SELECT TOP 1 * FROM ';
WHILE @n<=(SELECT MAX(n) FROM #views)
BEGIN
SELECT @tbl=(SELECT TABLE_NAME FROM #views WHERE n=@n);
EXEC('SELECT '''+@tbl+''' AS [THE TABLE]');
EXEC(@sql+@tbl);
SELECT @n=@n+1
END
DROP TABLE #views;
-- Itzik Ben-Gan 2001
March 11, 2013 at 8:40 am
Erin,
Thank you very much for the information!
Charlie
March 11, 2013 at 8:41 am
Alan,
Thank you for the code.... it worked great!
Charlie
March 11, 2013 at 9:04 am
rummings (3/11/2013)
Alan,Thank you for the code.... it worked great!
Charlie
Any time.
-- Itzik Ben-Gan 2001
March 14, 2013 at 8:07 am
What about using sp_MSforeachview?
March 14, 2013 at 8:11 am
What about using:
exec sp_MSforeachview 'select top 1 from ?'
March 14, 2013 at 9:50 am
Keep in mind that all of the code samples are using top 1 with no order by. If you require the "first" record to be meaningful you will need to add this order by. If on the other hand you just want any given row from the view this will work.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 14, 2013 at 11:05 am
kevaburg (3/14/2013)
What about using:exec sp_MSforeachview 'select top 1 from ?'
That is not a stored proc that comes from SQL Server. If you can run this:
exec sp_MSforeachview 'select top 1 from ?
Its because someone created that locally.
-- Itzik Ben-Gan 2001
March 14, 2013 at 1:01 pm
Alan.B (3/14/2013)
kevaburg (3/14/2013)
What about using:exec sp_MSforeachview 'select top 1 from ?'
That is not a stored proc that comes from SQL Server. If you can run this:
exec sp_MSforeachview 'select top 1 from ?
Its because someone created that locally.
LOL a quick search revealed this: http://www.sqlservercentral.com/scripts/T-SQL+Aids/30373/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 14, 2013 at 1:16 pm
Oooooops! It would appear I am talking out of holes normally reserved for other purposes!
That is exactly the procedure I saw but I didn't read it properly! I take back what I said but it would be a nice procedure to have I think.....
On a serious note though, I have noticed that Intellisense finds sp_MSforeachtable and sp_MSforeachdb. Does that mean they are both documented and supported procedures. If so then my job has suddenly become a little simpler!
March 14, 2013 at 1:24 pm
kevaburg (3/14/2013)
Oooooops! It would appear I am talking out of holes normally reserved for other purposes!That is exactly the procedure I saw but I didn't read it properly! I take back what I said but it would be a nice procedure to have I think.....
On a serious note though, I have noticed that Intellisense finds sp_MSforeachtable and sp_MSforeachdb. Does that mean they are both documented and supported procedures. If so then my job has suddenly become a little simpler!
Both of those you mentioned are available. However they are both undocumented and unsupported. You can find lots of places online that discuss them and you can always crack them open yourself to have a look.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 14, 2013 at 10:17 pm
Alan.B (3/7/2013)
Cursors, loops and dSQL fall under the last choice column but this is one of those cases...For tables you would do this:
EXEC sp_MSforeachtable'SELECT TOP 1 * FROM ?'
BWAA-HAAA!!!! Oh, be careful now, Alan. You've just stated perhaps one of the greatest oxymorons of them all. Have you ever looked under the hood of sp_MSForEachTable? It's a monstorous LOOP! 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
March 15, 2013 at 7:45 am
Jeff Moden (3/14/2013)
Alan.B (3/7/2013)
Cursors, loops and dSQL fall under the last choice column but this is one of those cases...For tables you would do this:
EXEC sp_MSforeachtable'SELECT TOP 1 * FROM ?'
BWAA-HAAA!!!! Oh, be careful now, Alan. You've just stated perhaps one of the greatest oxymorons of them all. Have you ever looked under the hood of sp_MSForEachTable? It's a monstorous LOOP! 😉
Actually Jeff he said that cursors and loops fall under the last category but that this IS one time it is acceptable.
I would of course recommend that anybody who wants to use any of the sp_MSForEach procs to crack them open and look at what they are doing.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply