我有一個名爲「Union」的數據庫。我試圖在MAINT表中爲這個數據庫執行SQL,但是因爲'union'是一個SQL命令,它會拋出錯誤。我可以從聯合數據庫執行時運行查詢。動態SQL能夠解決我的問題,還是應該更改數據庫名稱?SQL命令名稱是數據庫名稱;使用動態SQL?
我不斷收到不正確的語法附近關鍵字「UNION」這裏是我到目前爲止,
DECLARE @sql varchar(max)
DECLARE @Database varchar(5)
Set @Database = 'UNION'
SELECT @sql = 'SELECT '[email protected]+' as ''Database'', '[email protected]+'.hsi.useraccount.username as ''User Name'',
'[email protected]+'.hsi.useraccount.realname as ''Real Name''
FROM '[email protected]+'.hsi.useraccount
WHERE '[email protected]+'.hsi.useraccount.username NOT LIKE ''%deactivated%'' and '[email protected]+'.hsi.useraccount.username not like ''%administrator'' and '[email protected]+'.hsi.useraccount.username not like ''%internal%'''
execute(@sql)
用方括號的名稱('[聯盟]') – Siyual
@Siyual:還不如讓他有什麼看法的答案,並得到適當的信貸爲了它。 –