但是使用Replace i在条件和连接条件之后更改了表模式,但是一个条件失败。
任何人都可以帮助我
示例:
Declare @Var Nvarchar(Max)
Set @Var='sampleid in (SELECT t1.id from EmployeesView t1 join Emp t2 t1.id=t2.id WHERE t1.l5 IN (SELECT t2.id from org t2,
org t3,org t4 WHERE t2.l >= t3.f AND t2.l <= t3.r AND t3.id = ''40'') AND t1.lstnm = ''kc'')'
Select Replace(Replace(Convert(Varchar(Max),@Var),'from ','from AB.'),'join ','join AB.')
O / P:
sampleid in (SELECT t1.id from AB.EmployeesView t1 join AB.Emp t2 t1.id=t2.id WHERE t1.l5 IN (SELECT t2.id from AB.org t2,
org t3,org t4 WHERE t2.l >= t3.f AND t2.l <= t3.r AND t3.id = '40') AND t1.lstnm = 'kc')
但是预期的O / P:
sampleid in (SELECT t1.id from AB.EmployeesView t1 join AB.Emp t2 t1.id=t2.id WHERE t1.l5 IN (SELECT t2.id from AB.org t2,
TM.org t3,TM.org t4 WHERE t2.l >= t3.f AND t2.l <= t3.r AND t3.id = '40') AND t1.lstnm = 'kc')
答案 0 :(得分:2)
试试这个:
Select Replace(Replace(Replace(Replace(Convert(Varchar(Max),@Var),'from ','from AB.'),'join ','join AB.'), 'org', 'TM.org'), 'AB.TM', 'AB')
答案 1 :(得分:0)
最好的方法是功能
创建功能TM.GetSplit ( @SqlTxt varchar(Max) )返回nvarchar(Max) 如 开始 声明@txt nvarchar(Max),@ originalTxt nvarchar(Max),@ orgtxt nvarchar(max),@ split nvarchar(max),@ start int,@ end int,@ len int
Select @originalTxt = @SqlTxt, @orgtxt=@SqlTxt
Set @len=LEN(@orgtxt)
While @len > 0
Begin
Set @len=LEN(@orgtxt)
Select @start=CHARINDEX('from',@orgtxt)
Select @end=CHARINDEX('where',@orgtxt)
If(@start != 0 and @end!=0)
Begin
set @split = Substring(@orgtxt, @start, @end-@start)
Set @orgtxt = SUBSTRING(@orgtxt,@end + 5 ,@len-@end)
Set @txt=@split
Select @split=Replace(Replace(Replace(Convert(Varchar(Max),@split),'from ','from AB.'),'join ','join AB.') ,',',', AB.')
Select @originalTxt=REPLACE(@originalTxt,@txt,@split)
Set @txt=@originalTxt
End
Else
Begin
break
End
End
If @txt is null
Begin
Select @txt=Replace(Replace(Convert(Varchar(Max),@originalTxt),'from ','from AB.'),'join ','join AB.')
End
return @txt
结束