This ?before is the database ( [database].[schema].[table]). This way you can use sp_MSforeachdbor, as I prefer, use the view sys.databasesto prepare dynamic queries.
Beware, both methods can affect system databases.
Take a look at this solution:
DECLARE @query nvarchar(MAX)='';
SELECT @query = @query + 'USE '+QUOTENAME(name)+';ALTER TABLE dbo.profileTable ADD profileStatus int;'
FROM sys.databases
WHERE OBJECT_ID(QUOTENAME(name)+'.dbo.profileTable', 'U') IS NOT NULL
EXEC(@query)
It adds a column col1 intto each dbo.profileTable in each database.
source
share