I have a database with the name "Union". I am trying execute SQL for this database in the MAINT table but since 'union' is a SQL command it is throwing errors. I can get the query to run when executing from Union database. Would dynamic SQL be able to fix my problem or should I change the database name?
I keep getting incorrect syntax near keyword 'UNION' here is what I have so far,
DECLARE @sql varchar(max)
DECLARE @Database varchar(5)
Set @Database = 'UNION'
SELECT @sql = 'SELECT '+@Database+' as ''Database'', '+@Database+'.hsi.useraccount.username as ''User Name'',
'+@Database+'.hsi.useraccount.realname as ''Real Name''
FROM '+@Database+'.hsi.useraccount
WHERE '+@Database+'.hsi.useraccount.username NOT LIKE ''%deactivated%'' and '+@Database+'.hsi.useraccount.username not like ''%administrator'' and '+@Database+'.hsi.useraccount.username not like ''%internal%'''
execute(@sql)
[Union])