将自定义文本+变量添加到sql中的varchar

时间:2013-10-29 14:45:16

标签: sql sql-server triggers

我正在尝试使用单个命令行输出制作一个txt文件。 但由于某种原因,它不想将文本和测试名称添加到其中.. 这是我的代码:

    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(缺少testname) @test不会添加到文件中。

据我所知,我的表中的数据是正确的 有什么想法吗?

提前致谢&亲切的问候,

戴夫

2 个答案:

答案 0 :(得分: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

基本上,我的猜测是你必须为每个案例设置变量,否则它将保持不变。

答案 1 :(得分:0)

现在似乎没有任何调整。 我不知道为什么没有,但代码是正确的。