目前我有一个在存储过程中生成的动态查询,它有一个bug。不知怎的,有一个连续的重复' AND'在其中生成。
想知道是否有办法删除符号重复' AND'来自动态查询字符串。
例如:
var str = 'Select * from employee A where A.age > 30 AND AND A.role = ''developer'''
更新
以下建议的替换不起作用 请参阅以下查询:
DECLARE
@str NVARCHAR(MAX)
SET @str = 'fasdf asdfasf asfasdfasafsdf AND AND asdfasdfasd AND dfasdfa'
SET @str = REPLACE(@str, 'AND AND', 'AND')
PRINT @str
谢谢!
答案 0 :(得分:2)
这样的东西?
/****** Object: StoredProcedure [dbo].[RemoveConsecutiveDuplicateTokens] Script Date: 30/06/2016 09:30:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[RemoveConsecutiveDuplicateTokens]
@instr varchar(max) ,
@outstr varchar(max) OUTPUT
as
declare @workstr varchar(max) = ltrim(@instr),
@newstr varchar(max),
@oldtoken varchar(max),
@newtoken varchar(max)
while @workstr is not null
begin
if @oldtoken is null
begin
set @oldtoken = SUBSTRING(@workstr,1,charindex(' ',@workstr))
set @workstr = ltrim(Stuff(@workstr, CharIndex(@oldtoken,@workstr), Len(@oldtoken), ''))
set @newstr = @oldtoken + ' '
end
set @newtoken = SUBSTRING(@workstr,1,charindex(' ',@workstr))
if @newtoken <> @oldtoken
begin
if @newtoken <> char(39)
begin
set @oldtoken = @newtoken
set @newstr = @newstr + @newtoken + ' '
set @workstr = ltrim(Stuff(@workstr, CharIndex(@newtoken,@workstr), Len(@newtoken), ''))
end
end
else
begin
set @workstr = ltrim(Stuff(@workstr, CharIndex(@newtoken,@workstr), Len(@newtoken), ''))
end
if charindex(' ',@workstr) = 0
break
end
set @newtoken = SUBSTRING(@workstr,1,len(@workstr))
if @newtoken <> @oldtoken
begin
if @newtoken <> char(39)
begin
set @oldtoken = @newtoken
set @newstr = @newstr + @newtoken + ' '
set @workstr = ltrim(Stuff(@workstr, CharIndex(@newtoken,@workstr), Len(@newtoken), ''))
end
end
else
begin
set @workstr = ltrim(Stuff(@workstr, CharIndex(@newtoken,@workstr), Len(@newtoken), ''))
end
select @outstr = @newstr
return
答案 1 :(得分:1)
首先,你做错了。修复生成此错误sql的逻辑。
但是出于研究/学习的目的,你就是这样做的。
// implement the method createPropertiesFromJSON().
// It should read your JSON file (i.e. your configuration) and
// transform it to a Properties object.
Properties propertiesFromJson = createPropertiesFromJSON();
StdSchedulerFactory.initialize(propertiesFromJson);
Scheduler scheduler = StdSchedulerFactory.getScheduler();
答案 2 :(得分:1)
我忘记了我多么不喜欢SUBSTRING,但那时我一直在努力将<starting_position>
视为真正的以开头的位置。
然而,真正的野兽是如何在@@TRANCOUNT
的上下文中在SQL Server中实现字符串操作。
考虑声明
PRINT QUOTE_NAME(REPLACE('My____Table', '__', '_'))
我们希望使用正确的命名标准,但函数返回:
`[My__Table]`
为什么呢?因为REPLACE
跳过重复的长度。为了证明这一点,我们再添加一个'_'CHAR(95)
,我们得到这个回报:
`[My___Table]`
因此,简单地使用WHILE语句嵌入它就足以满足我们的需求。 注意我将空格替换为'_'以便于阅读
DECLARE @instr varchar(max)
SET @instr = 'SELECT * from employee A where A.age > 30 AND AND A.role = ''developer'''
DECLARE @workstr varchar(max) = REPLACE(LTRIM(@instr), ' ', '_'),
@tokenque VARCHAR(MAX),
@newstr INT = 0,
@token varchar(max),
@flag_break INT = 0
-- removes the extra "spaces"
WHILE CHARINDEX('__', @workstr) <> 0
BEGIN
SET @workstr = REPLACE(@workstr, '__' , '_')
END
SET @tokenque = @workstr
WHILE (CHARINDEX('_', @tokenque) <> 0)
BEGIN
SET @token = SUBSTRING(@tokenque, 1, CHARINDEX('_', @Tokenque) - 1 )
IF @token <> '''' -- (') delimiter skipped
BEGIN
WHILE CHARINDEX(@token + '_' + @token, @workstr) <> 0
BEGIN
SET @workstr = REPLACE(@workstr, @token + '_' + @token, @token)
END
SET @tokenque = SUBSTRING(@tokenque, LEN(@token) + 2, LEN(@tokenque) )
END
ELSE SET @tokenque = SUBSTRING(@tokenque, LEN(@token) + 2, LEN(@tokenque) )
PRINT @tokenque --if you want to see the progression
END
PRINT REPLACE(@workstr, '_', ' ')
<强>结果:强>
'SELECT * from employee A where A.age > 30 AND A.role = 'developer'
答案 3 :(得分:0)
使用REPLACE函数并将'AND AND'替换为'AND'。示例
SELECT REPLACE('Select * from employee A where A.age > 30 AND AND A.role = ''developer'' ','AND AND','AND');