I am looking to return a view name and all record ID's where billingAddress != shippingAddress to further review. I need to query all views in one database. This is my thought process and if their is a better way or faster way to write the query by all means help me out!
What I am stuck on is how to return the view name with the recordID?
Create Table #T (ID Int Identity Not Null, ViewNames VARCHAR(1000)
Create Table #2 (viewNames varchar(1000))
Insert Into #T (ViewNames)
Select '['+C.Table_Catalog+'].['+C.Table_Schema+'].['+C.Table_Name+']' TableName
FROM Information_Schema.Columns c
Join information_schema.Tables T on C.Table_Catalog = T.Table_Catalog
AND C.Table_Schema = T.Table_Schema
AND C.Table_Name = T.Table_Name
Where T.Table_Type = 'View'
Group By '['+C.Table_Catalog+'].['+C.Table_Schema+'].['+C.Table_Name+']'
---Now this is the piece that I am stuck on as I do not know how to insert the view name into the table as well on each iteration
Declare @N int, @Str nvarchar(2000), @viewname nvarchar(2000), @MaxID int
Set @N = 1
Select @MaxID = Max(ID)
From #T
While (@N<@MaxID)
Begin
Select @viewname= viewname
From #T
Set @Str = ' Insert Into #2(viewname)
Select Top 1 '''+@viewname+'''
From '+@viewname+'
where exists(Select recordID from '+@viewname+' where [shipaddress] != [billaddress] ) '
Exec sp_eecutesql @str
Set @N = @N + 1
End Select * from #t