避免参数查询中的空格

时间:2014-10-25 07:40:35

标签: sql-server

我有这个:

DECLARE @Client NVARCHAR(25),
        @sqlStr NVARCHAR(MAX);

SET @sqlStr = 'INSERT INTO #GetDisplayName (displayName) SELECT displayName FROM OPENROWSET
            (''ADSDSOObject'', ''adsdatasource''; ''mxi\santos'';''Consultant1'', 
             ''SELECT displayName FROM ''''LDAP://mxiadsrv/DC=mxi,DC=com'''' 
             where sAMAccountName=''''' + @Client + ''''''')'

SET @sqlStr = REPLACE(REPLACE(REPLACE(@sqlStr,' ',' %'),'% ',''),'%','')

在我的一个循环中,我得到了:

INSERT INTO #GetDisplayName (displayName) SELECT displayName FROM OPENROWSET     ('ADSDSOObject', 'adsdatasource'; 'mxi\santos';'Consultant1',       
'SELECT displayName FROM ''LDAP://mxiadsrv1/DC=mxi,DC=com''       
where sAMAccountName=''fallot ''')

问题是sAMAccountName ='' fallot ''')部分中的额外空间

我该如何避免这种情况?非常感谢!

1 个答案:

答案 0 :(得分:1)

where sAMAccountName=''''' + rtrim(@Client) + ''''''')'