2013-10-29 19 views
0

我試圖用一個命令行輸出一個txt文件。 但是出於某種原因不希望文本和測試名稱添加到它.. 這裏是我的代碼:在sql中添加自定義文本+變量到varchar

declare @command AS varchar(200) 
declare @cmd AS varchar(200) 
declare @input AS varchar(200) 
declare @timeinput AS varchar(200) 
declare @test AS varchar(1000) 
declare @failureid as int 

select @timeinput = (select time from inserted) 
select @input = (select action from inserted) 
select @failureid = (select failureId from inserted) 

select @test = (select name from dbo.Alert where FailureId = @failureid) 


if (@input = 'start') 
BEGIN 
set @command = ('EnableTest' + @test) 
END 

if (@input = 'stop') 
BEGIN 
set @command = ('DisableTest' + @test) 
END 

if (@input = 'pauze') 
set @command = ('PauseTest' + @test + @timeinput) 
END 

SET @cmd = 'echo ' + @command + ' > F:\commandfolder\testing.HMS' 
EXECUTE Master.dbo.xp_CmdShell @cmd 

,並在HMS文件的輸出:DisableTest(與測試名失蹤) @test不會添加到文件中。

據我所知,我的表中的數據是正確的 任何想法?

預先感謝&親切的問候,

戴夫

回答

0

怎麼樣:

declare @command AS varchar(200) 
declare @cmd AS varchar(200) 
declare @input AS varchar(200) 
declare @timeinput AS varchar(200) 
declare @test AS varchar(1000) 
declare @failureid as int 
select @timeinput = (select time from inserted) 
select @input = (select action from inserted) 
select @failureid = (select failureId from inserted) 
select @test = (select name from dbo.Alert where FailureId = @failureid) 
if (@input = 'start') 
BEGIN 
set @failureid = (select failureId from inserted where action= @input) 
set @test = (select name from dbo.Alert where FailureId = @failureid) 
set @command = ('EnableTest' + @test) 
END 

if (@input = 'stop') 
BEGIN 
set @failureid = (select failureId from inserted where action= @input) 
set @test = (select name from dbo.Alert where FailureId = @failureid) 
set @command = ('DisableTest' + @test) 
END 

if (@input = 'pauze') 
begin 
set @failureid = (select failureId from inserted where action= @input) 
set @test = (select name from dbo.Alert where FailureId = @failureid) 
set @command = ('PauseTest' + @test + @timeinput) 
END 

SET @cmd = 'echo ' + @command + ' > F:\commandfolder\testing.HMS' 
EXECUTE Master.dbo.xp_CmdShell @cmd 

Basicaly,我的猜測是,你必須設置變量爲每一個案件otherswise它仍然一樣。

+0

我會去看看會發生什麼 –

0

它現在似乎正在工作,沒有任何調整。 我不知道爲什麼它沒有,但代碼是正確的。

相關問題