Cursor to Display list of All Databases Name,Tables Name and ColumnNames
Below we have scripts of 3 cursor that will return list of all Databases, tables and column Name.The first cursor will Return list of ColumnNames with the Table
the Second Cursor will Return list of only TablesName
the Third Cursor will Return list of DatabaseNames
Cursor to Display TableName and ColumnName
declare @TBLName nvarchar(100)
declare @columnName nvarchar(100)
declare C_DBNames cursor for
select TABLE_NAME ,COLUMN_NAME from INFORMATION_SCHEMA .COLUMNS
open C_DBNames fetch C_DBNames into @TBLName ,@columnName
while(@@FETCH_STATUS=0)
begin
print @TBLName + '-' + @columnName
fetch C_DBNames into @TBLName,@columnName
end
close C_DBNames ;
deallocate C_DBNames;
Cursor to Display only tableNames
declare @TBLName nvarchar(100)
declare C_DBNames cursor for
select TABLE_NAME from INFORMATION_SCHEMA .tables
open C_DBNames fetch C_DBNames into @TBLName
while(@@FETCH_STATUS=0)
begin
print @TBLName
fetch C_DBNames into @TBLName
end
close C_DBNames ;
deallocate C_DBNames;
Cursor to Display only Database Name
declare @DatabaseName nvarchar(100)
declare C_DBNames cursor for
select name from master.dbo.sysdatabases
--Select name from master.
--where name not in ('master','tempdb','model','msdb') order by name
open C_DBNames fetch C_DBNames into @DatabaseName
while(@@FETCH_STATUS=0)
begin
print @DatabaseName
fetch C_DBNames into @DatabaseName
end
close C_DBNames ;
deallocate C_DBNames;
Note: Above are examples of Cursor to show Database, tables and column Name you can get list of Database,Table and ColumnNames without cursor just by running queries used in Cursors.
No comments:
Post a Comment