日期:2014-05-17  浏览次数:20852 次

RTrim和LTrim,怎么不起作用?SQL小问.
这是我在SSMS写的临时SQL代码:
--没有去掉空格.
select FBranch,LEN(FBranch) from T_StuffBasic where FId = '00001'
--取消右边空格.
select RTRIM(FBranch),LEN(RTRIM(FBranch)) from T_StuffBasic where FId = '00001'
--先右后坐去掉空格.
select LTrim(RTrim(FBranch)),LEN(LTrim(RTrim(FBranch))) from T_StuffBasic where FId = '00001'
--先左后右去掉空格.
select RTRIM(LTRIM(FBranch)),LEN(RTRIM(LTRIM(FBranch))) from T_StuffBasic where FId = '00001'


运行结果都一样,为:

这是我在存储过程写的,其实和上面的最后两条一样:
ALTER PROCEDURE USP_GetT_StuffBasicFilterExtraFiledByField	--过滤从别的表获得数据的字段值.
@FieldName nvarchar(20),
@FieldVal nvarchar(50)
AS
SET NOCOUNT ON 
declare @query nvarchar(max)
set @query = 'select FId,FStuffName,LTrim(RTrim(FFolk)),FFolk,FBirthday,FAge,LTrim(RTrim(FKultur)),FKultur,FMarriage,
FSex,LTrim(RTrim(FVisage)),FVisage,FIdCard,FWorkDate,FWorkLength,LTrim(RTrim(FEmployeeGenere)),FEmployeeGenere,
LTrim(RTrim(FBusiness)),FBusiness,LTrim(RTrim(FLaborage)),FLaborage,LTrim(RTrim(FBranch)),FBranch,
LTrim(RTrim(FDuthcall)),FDuthcall,FPhone,FHandSet,FSchool,FSpeciality,FGraduateDate,FAddress,FPhoto,
LTrim(RTrim(FProName)),FProName,LTrim(RTrim(FCityName)),FCityName,FMonthSalary,FBankNum,FPackStartDate,
FPackEndDate,FPactYear,FStuId from T_StuffBasic where '+@FieldName+' = '''+@FieldVal+''''
exec(@query)


我将上面的存储过程封装为一个方法,返回DataTable.
C#代码为:
/// <summary>
        /// 指定存储过程名,返回的表名,字段名和对应的值查找结果.
        /// </summary>
        /// <param name="proceName">存储过程名.</param>
        /// <param name="tableName">表名.</param>
        /// <param name="fieldName">字段名.</param>
        /// <param name="fieldVal">字段值.</param>
        /// <returns>返回结果集的DataTable.</returns>
        public static DataTable GetStuffBasicDataTableByProcedure(string proceName, string tableName, string fieldName, string fieldVal) {
            _Set = new DataSet();
            using(SqlConnection connect = new SqlConnection(strConn)) {