为什么REPLACE不能在我的SQL查询中工作?

时间:2016-09-06 00:14:52

标签: sql-server tsql stored-procedures

我正在创建一个存储过程。目前,查询存储在变量中。例如:

@sample = 'SELECT * FROM WS_CoolApp_ASample'
@sample += 'Where DATASOURCE.FName = @finder AND DATASOURCE.Age = 23'

我想删除那个' DATASOURCE。'如果条件为真,则从查询中查询。到目前为止,我已经尝试过:

IF (true)
    REPLACE(@sample, "DATASOURCE.", "")

为什么这不起作用?

4 个答案:

答案 0 :(得分:3)

您没有将REPLACE()的输出分配给任何内容。

@sample = REPLACE(@sample, 'DATASOURCE.', '')

答案 1 :(得分:0)

<!-- language: lang-sql -->
DECLARE @sample as nvarchar(500)

set @sample = 'SELECT * FROM WS_CoolApp_ASample';
set @sample += 'Where DATASOURCE.FName = @finder AND DATASOURCE.Age = 23';

-- assign the replaced string back to @sample
set @sample = REPLACE(@sample, 'DATASOURCE.', '')

PRINT @sample

-- prints -> SELECT * FROM WS_CoolApp_ASampleWhere FName = @finder AND Age = 23

答案 2 :(得分:0)

DECLARE @sample nvarchar(max)
SET @sample = 'SELECT * FROM WS_CoolApp_ASample'
SET @sample += 'Where DATASOURCE.FName = @finder AND DATASOURCE.Age = 23'

SELECT REPLACE(@sample,'DATASOURCE.','')

我希望这有效。

答案 3 :(得分:0)

DECLARE @sample nvarchar(max)
SET @sample = 'SELECT * FROM WS_CoolApp_ASample '
SET @sample += 'Where DATASOURCE.FName = @finder AND DATASOURCE.Age = 23'
if(@bit_var = 1)
  SET @sample = REPLACE(@sample,'DATASOURCE.','')