单引号截断问题
本帖最后由 loveandkissyou 于 2013-05-02 10:52:32 编辑
DECLARE @man nvarchar(10)
DECLARE @woman nvarchar(10)
DECLARE @nullMarital nvarchar(10)
DECLARE @age_b17 nvarchar(50)
DECLARE @age_18_24 nvarchar(50)
DECLARE @age_25_30 nvarchar(50)
DECLARE @age_31_35 nvarchar(50)
DECLARE @age_36_40 nvarchar(50)
DECLARE @age_u41 nvarchar(50)
SELECT @man=' AND Gender=''男'' ',@woman=' AND Gender=''女'' ',@nullMarital=' MaritalStatus is null ',
@age_b17=' AND (year(getdate())-BirthYear < 18) ',
@age_18_24=' AND (year(getdate())-BirthYear between 18 and 24) ',
@age_25_30=' AND (year(getdate())-BirthYear between 25 and 30) ',
@age_31_35=' AND (year(getdate())-BirthYear between 31 and 35) ',
@age_36_40=' AND (year(getdate())-BirthYear between 36 and 40) ',
@age_u41=' AND (year(getdate())-BirthYear > 40) '
Declare @sql nvarchar(max)
select @sql = N'SELECT top 1 * FROM smith_Character_shanghai WHERE 1=1' + @age_u41+ @man
print @sql
exec sp_executesql @sql
@sql 总是被性别Gender里面的单引号截断,请问怎么解决?谢谢
SQL
------解决方案--------------------DECLARE @man nvarchar(10)
DECLARE @woman nvarchar(10)
明显长度不够。
SELECT @man=' AND Gender=''男'' '
把nvarchar(10)改成nvarchar(30)...