我可以使用以下查询检索单个数据库
Select Table_catalog[Instance Name],
Table_name[Database Table],
Column_name[Column],
Data_type[Column Type]
FROM information_schema.columns
where Data_type in ('varchar')
Order by Table_name,Data_type
但我想为服务器中的所有可用数据库列出它
Declare @I int =1,@Qry nVarchar(1000),@DatabaseName nvarchar(100)
Declare @TempTable Table
(
Sno Int Identity(1,1),
DatabaseName Varchar(100)
)
Insert into @TempTable
Select name
from sys.databases
where database_id>4
Select * from @TempTable
While(@i<=(Select max(sno) from @TempTable))
Begin
Select @DatabaseName=DatabaseName from @TempTable where sno=@i
Select @DatabaseName
set @Qry='Use '+ @DatabaseName
**exec sp_executesql @Qry**
set @Qry= '
Select Table_catalog[Instance Name],
Table_name[Database Table],
Column_name[Column],
Data_type[Column Type]
FROM information_schema.columns
where Data_type in (''datetime'',''date'',''time'',''smalldatetime'')
Order by Table_name,Data_type'
exec sp_executesql @Qry
Set @i=@i+1
End
它不起作用,因为使用数据库名称实际上并未更改数据库并重复当前使用的数据库的结果集..对此有任何建议,