2014-10-02 65 views
0

在繼續執行腳本之前是否有辦法驗證腳本?驗證您是否使用了正確的數據庫

所以

DECLARE @strServernameToClear as VarChar(100) 
DECLARE @strCurrentServer as VarChar(100) 
DECLARE @strDatabaseToClear as VarChar (100) 
DECLARE @strDatabase as VarChar (100) 


SET @strDatabase = (SELECT @@database) << THIS IS WHERE IM HAVING TROUBLE. How to indicate which database it is running against 

IF @strCurrentServer <> @strServernameToClear 
BEGIN 
    PRINT '*****************WARNING *****************' 
    PRINT 'WRONG DB NAME: This server is: ' + @strCurrentServer 
    PRINT 'If this is the correct server, set @strServernameToClear to ' + @strCurrentServer 
    RETURN 

END 

ELSE 

EXECUTE ORDER 66 
+0

什麼數據庫服務器您使用的?每個語法都會有不同的語法來獲取當前的數據庫名稱...... – SnakeDoc 2014-10-02 16:11:07

+0

這最好在應用程序級別處理。 – 2014-10-02 16:16:04

+0

這是一個2008R2服務器 – Angie 2014-10-02 16:16:23

回答

0

我認爲這會爲你工作:

select @strDatabase = d.name 
from sys.sysprocesses p 
join sys.databases d on d.database_id = p.dbid 
where p.spid = @@SPID 
相關問題