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.
Author |
Topic |
a.shahnazi
Starting Member
3 Posts |
Posted - 2009-07-29 : 02:05:43
|
I wrote a script for finding number of objects :DECLARE db_name_cur CURSORFORWARD_ONLYFOR SELECT Name FROM master.dbo.sysdatabasesCREATE TABLE #temp (DatabaseName NVARCHAR(128),UserTable INT ,SyetemTable INT,PrimaryKey INT)DECLARE @db_name nvarchar(128)DECLARE @SQL nvarchar(150)DECLARE @UTab INTDECLARE @STab INTDECLARE @PK INTOPEN db_name_curFETCH NEXT FROM db_name_cur INTO @db_name WHILE @@fetch_status = 0 BEGIN SET @SQL ='use ' + @db_name exec (@SQL) SET @UTab = (select count(*) from sysobjects where xtype = 'U' ) SET @STab = (select count(*) from sysobjects where xtype = 'S') SET @PK = (select count(*) from sysobjects where xtype = 'PK') INSERT #temp VALUES ( @db_name ,@UTab,@STab,@PK ) FETCH NEXT FROM db_name_cur INTO @db_name ENDSELECT * FROM #tempCLOSE db_name_curDEALLOCATE db_name_curDROP TABLE #tempThis script dosn't work correct and just fetchs information of current database in Query Analyzer.Does anyone have any idea ?RegardsAli |
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-29 : 04:17:13
|
Hichange to dynamic....like thisDECLARE @SQL VARCHAR(1000)SET @SQL = 'select count(*) from '+@db_name+'..sysobjects where xtype = ''U'' 'EXEC(@SQL)-------------------------R.. |
 |
|
a.shahnazi
Starting Member
3 Posts |
Posted - 2009-07-30 : 00:24:41
|
Thanks for your advicethis is the final : SET @SQL = N'SELECT @UTab = COUNT(*) FROM ' + @db_name + '..sysobjects where xtype = ' + '''U''' EXEC sp_executesql @query = @SQL, @params = N'@UTab INT OUTPUT' , @UTAB = @UTAB OUTPUTRegardsAli |
 |
|
|
|
|