0

I am creating a stored procedure. Currently queries are being stored in variables. For example:

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

I want to remove that 'DATASOURCE.' from the query if a condition is true. So far I've tried:

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

Why isn't this working?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AleAng
  • 71
  • 1
  • 9

4 Answers4

3

You aren't assigning the output of REPLACE() to anything.

@sample = REPLACE(@sample, 'DATASOURCE.', '')
Mike D.
  • 4,034
  • 2
  • 26
  • 41
  • Ok, actually you were right. I had an if statement with an else statement below it and I changed it to this in the if statement but forgot to do it in the else statement. It's working now! – AleAng Sep 06 '16 at 14:36
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
th1rdey3
  • 4,176
  • 7
  • 30
  • 66
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.','')

I hope this works.

Vikram
  • 197
  • 2
  • 6
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.','')
Marinus
  • 157
  • 6