嵌套替换命令的数量是否有限制?

时间:2015-12-22 16:22:36

标签: sql sql-server replace nested

由于信息存储在我们表格的ASSIGNEES列中的方式,(见下文)

ASSIGNEES
Application__bDevelopers e502488 e531146 e551624
Application__bDevelopers e502488 Server__bAdmins e547653
e547653
e252403 e537149
e255401 e466840 Desktop__bSupport
e263899 e284429 Harbor__uSchool__bIT
e451155 e566548 c115478 e403190 e518202 Clinical__bApplications
Server__bAdmins e547653 Desktop__bSupport
IMAC__b__PInstall__bMove__bAdd__bChange__p e403190
....

在尝试生成结果集时,我必须使用以下嵌套替换命令:

......    
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(ASSIGNEES COLLATE SQL_Latin1_General_CP1_CS_AS,'__b',' ') 
,'__P','(')
,'__p',')')
,'__3','#')
,'__7','&')
,'__d','.')
,'__f','/')
,'__u','-')
,'__A','+')
,'__M',',')
,'e135164','Angela Martin')
,'e214652','Rosie Mayer')
,'e917868','Jon Bugen')
,'e619276','Phil Garcia')
,'e127172','Elie Esco')
,'e970795','Mary Morales')
......

我的问题是:可以像这样嵌套的替换命令的数量是否有限制?我有近1000个用户ID(例如,e123456)需要翻译成用户名。)

必须有更好的方法来做到这一点。也许"交叉申请"?我对所有建议持开放态度。谢谢!

2 个答案:

答案 0 :(得分:2)

嵌套限制?你的问题真的让我很感兴趣!

以下示例显示您可以嵌套最多244个级别的功能。当我再添加一个LEFT时,我得到错误:

Msg 191,Level 15,State 1,Line 33 SQL语句的某些部分嵌套得太深。重写查询或将其分解为较小的查询。

您是否考虑将数据分成多个字段?您会发现与之互动更容易。

实施例

/* There are ten functions to row. 
 * Tested up to nest level 244 without error.
 * 245 returns "Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries."
 * Tested SQL Server 2008 R2.
 */
SELECT                               
    LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT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T(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(
    LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(
    LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(
    LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(LEFT(  -- 240
    LEFT(LEFT(LEFT(LEFT(                               -- 244
        'x'
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)
    , 1), 1), 1), 1)
;

答案 1 :(得分:1)

这太可怕了。你想硬编码每个用户名的映射!?

无论您想做什么,上述替换都可以在一个REGEX内完成。

您应该通过正则表达式清理它并使用映射表(id,username)。