這是SQL服務器存儲過程中的代碼。我已經聲明瞭常量@other_db在查詢中使用它。對於我能夠做到的簡單查詢,如下所述。不斷查詢存儲過程
DECLARE @other_db VARCHAR(50)
SET @other_db = 'BABMwork8_22_1210_0012'
UPDATE BABMwork8_22_1210_0012.dbo.concessions SET double_ones = '' WHERE double_ones = 'Yes'
EXEC('UPDATE @other_db.dbo.concessions SET double_ones = '+''+' WHERE double_ones = '+'Yes')
而對於查詢下面提到的,我想使用常量@other_db更換BABMwork8_22_1210_0012的價值。
UPDATE concessions SET double_ones = 'Yes'
WHERE (concession IN(SELECT concession FROM BABMwork8_22_1210_0012.dbo.concessions WHERE
(manuell_archive_delete! = 'Delete' OR manuell_archive_delete IS NULL) AND
(double_action = 'ok_invoice' OR double_action is NULL))
OR concession IN(SELECT Concession FROM BABMwork8_22_1210_0012.dbo.invoicing WHERE
(can_invoiced is NULL OR can_invoiced != 'No') AND
(double_action = 'ok_invoice' OR double_action is NULL)))
AND (double_action = 'ok_invoice' OR double_action is NULL)
AND (manuell_archive_delete! = 'Delete' OR manuell_archive_delete IS NULL) AND
source!= 'PARK'
儘管我嘗試使用EXEC它變得越來越複雜。我有幾個查詢,如果有任何簡單的方法來使用常量。
感謝您的幫助!
你將如何執行此查詢?如果手動,我認爲'sqlcmd'模式可能會有所幫助。 http://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/ – 2011-01-05 11:25:56
@Martin - 我的前端是PHP。點擊一個按鈕後,在後臺調用存儲過程。 – satya 2011-01-05 11:31:11
你必須使用動態SQL和'EXEC'或'sp_executesql',因爲我認爲你正在做這件事。數據庫名稱無法參數化,並且不支持數據庫同義詞(如果併發不是問題,這可能會有所幫助) – 2011-01-05 11:39:43