declare @st2 varchar(20)
set @st2='35,3530'
exec('select Id号=''型号'''+@f1
    +' union all select ''图像'''+@f2
    +' union all select ''批发价'''+@f3
    +' union all select ''零售价'''+@f4
+'  union all 
select * from 
(select ipv.Name ipvn, ipv.ItemId, ip.Name ipn 
from [5oujkruasdsdjuejhfk].[dbo].Item_Parameter_Value IPV, [5oujkruasdsdjuejhfk].[dbo].Item_Parameter IP,[5oujkruasdsdjuejhfk].[dbo].Item_ParameterCa c where ipv.itemId in (' + @st + ') and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb 
pivot (max(ipvn) for itemid in (' + @st2 + ')) ba') 

解决方案 »

  1.   

    itemid in ([35],[3530])
     ==〉
    CHARINDEX(itemid , @st2)>0
    这样试试看!
      

  2.   

    --TRY
    declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000), @f4 varchar(8000)
    declare @st2 varchar(8000)
    set @st2='(35,3530)'
    select @f1='',@f2='',@f3='', @f4=''select @f1=@f1+',['+ltrim(itemId)+']='''+[型号]+''''
         ,@f2=@f2+','''+[图像]+''''
        ,@f3=@f3+','''+ltrim([UnitCost])+''''
        ,@f4=@f4+','''+ltrim([ListPrice])+''''
        from (select * from
     (select  i.itemid, 型号=(b.Name ++ i.xh), 图像=i.ImageIndex, c.UnitCost, c.ListPrice
     from [5oujkruasdsdjuejhfk].[dbo].[CO] c, [5oujkruasdsdjuejhfk].[dbo].[brand] b, [5oujkruasdsdjuejhfk].[dbo].[item] i where c.ItemId=i.itemid and i.itemid in (35,3530) and i.brandid=b.brandid and c.UnitCost>0 and c.ListPrice>0) a) aabb
    exec('select Id号=''型号'''+@f1
        +' union all select ''图像'''+@f2
        +' union all select ''UnitCost'''+@f3
        +' union all select ''ListPrice'''+@f4
    +'  union all 
    select * from 
    (select ipv.Name ipvn, ipv.ItemId, ip.Name ipn 
    from [5oujkruasdsdjuejhfk].[dbo].Item_Parameter_Value IPV, [5oujkruasdsdjuejhfk].[dbo].Item_Parameter IP,[5oujkruasdsdjuejhfk].[dbo].Item_ParameterCa c where ipv.itemId in (35,3530) and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb 
    pivot (max(ipvn) for itemid in ' +@st2+') ba') 
      

  3.   

    不知哪有错,就用print 把exec中的内容给打印出来
      

  4.   


    declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000), @f4 varchar(8000)
    select @f1='',@f2='',@f3='', @f4=''select @f1=@f1+',['+ltrim(itemId)+']='''+[型号]+''''
      ,@f2=@f2+','''+[图像]+''''
        ,@f3=@f3+','''+ltrim([批发价])+''''
    ,@f4=@f4+','''+ltrim([零售价])+''''
        from (select * from
     (select i.itemid, 型号=(b.Name ++ i.xh), 图像=i.ImageIndex, 批发价=c.UnitCost, 零售价=c.ListPrice
     from [5oujkruasdsdjuejhfk].[dbo].[CO] c, [5oujkruasdsdjuejhfk].[dbo].[brand] b, [5oujkruasdsdjuejhfk].[dbo].[item] i
     where c.ItemId=i.itemid and i.brandid=b.brandid and c.UnitCost>0 and c.ListPrice>0 and i.itemid in (@st)) a) aabb  --这里的@st运行不了,提示在将 nvarchar 值 '3098,3097,3094' 转换成数据类型 int 时失败。 exec('select Id号=''型号'''+@f1
    +' union all select ''图像'''+@f2
        +' union all select ''批发价'''+@f3
    +' union all select ''零售价'''+@f4
    +'  union all 
    select * from 
    (select ipv.Name ipvn, ipv.ItemId, ip.Name ipn 
    from [5oujkruasdsdjuejhfk].[dbo].Item_Parameter_Value IPV, [5oujkruasdsdjuejhfk].[dbo].Item_Parameter IP,[5oujkruasdsdjuejhfk].[dbo].Item_ParameterCa c where ipv.itemId in (' + @st + ') and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb 
    pivot (max(ipvn) for itemid in (' + @st2 + ')) ba') 
    这样子虽然没有错,但我应用到程序当中时,我传过来的@st是如3098,3097,3094这样的值,程序提示:
    在将 nvarchar 值 '3098,3097,3094' 转换成数据类型 int 时失败。 
     
      

  5.   

    declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000), @f4 varchar(8000)
    select @f1='',@f2='',@f3='', @f4=''select @f1=@f1+',['+ltrim(itemId)+']='''+[型号]+''''
         ,@f2=@f2+','''+[图像]+''''
        ,@f3=@f3+','''+ltrim([批发价])+''''
        ,@f4=@f4+','''+ltrim([零售价])+''''
        from (select * from
     (select i.itemid, 型号=(b.Name ++ i.xh), 图像=i.ImageIndex, 批发价=c.UnitCost, 零售价=c.ListPrice
     from [5oujkruasdsdjuejhfk].[dbo].[CO] c, [5oujkruasdsdjuejhfk].[dbo].[brand] b, [5oujkruasdsdjuejhfk].[dbo].[item] i
     where c.ItemId=i.itemid and i.brandid=b.brandid and c.UnitCost>0 and c.ListPrice>0 and cast(i.itemid as varchar(100)) in (@st)) a) aabb  --这里的@st运行不了,提示在将 nvarchar 值 '3098,3097,3094' 转换成数据类型 int 时失败。 exec('select Id号=''型号'''+@f1
        +' union all select ''图像'''+@f2
        +' union all select ''批发价'''+@f3
        +' union all select ''零售价'''+@f4
    +'  union all 
    select * from 
    (select ipv.Name ipvn, ipv.ItemId, ip.Name ipn 
    from [5oujkruasdsdjuejhfk].[dbo].Item_Parameter_Value IPV, [5oujkruasdsdjuejhfk].[dbo].Item_Parameter IP,[5oujkruasdsdjuejhfk].[dbo].Item_ParameterCa c where ipv.itemId in (' + @st + ') and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb 
    pivot (max(ipvn) for itemid in (' + @st2 + ')) ba') 
      

  6.   

    declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000), @f4 varchar(8000)
    select @f1='',@f2='',@f3='', @f4=''select @f1=@f1+',['+ltrim(itemId)+']='''+[型号]+''''
         ,@f2=@f2+','''+[图像]+''''
        ,@f3=@f3+','''+ltrim([批发价])+''''
        ,@f4=@f4+','''+ltrim([零售价])+''''
        from (select * from
     (select i.itemid, 型号=(b.Name ++ i.xh), 图像=i.ImageIndex, 批发价=c.UnitCost, 零售价=c.ListPrice
     from [5oujkruasdsdjuejhfk].[dbo].[CO] c, [5oujkruasdsdjuejhfk].[dbo].[brand] b, [5oujkruasdsdjuejhfk].[dbo].[item] i
     where c.ItemId=i.itemid and i.brandid=b.brandid and c.UnitCost>0 and c.ListPrice>0 and cast(i.itemid as varchar(100)) in (@st)) a) aabb  --这里的@st运行不了,提示在将 nvarchar 值 '3098,3097,3094' 转换成数据类型 int 时失败。 exec('select Id号=''型号'''+@f1
        +' union all select ''图像'''+@f2
        +' union all select ''批发价'''+@f3
        +' union all select ''零售价'''+@f4
    +'  union all 
    select * from 
    (select ipv.Name ipvn, ipv.ItemId, ip.Name ipn 
    from [5oujkruasdsdjuejhfk].[dbo].Item_Parameter_Value IPV, [5oujkruasdsdjuejhfk].[dbo].Item_Parameter IP,[5oujkruasdsdjuejhfk].[dbo].Item_ParameterCa c where ipv.itemId in (' + @st + ') and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb 
    pivot (max(ipvn) for itemid in (' + @st2 + ')) ba') 
      

  7.   

    改成楼上了之后,就变成:消息 205,级别 16,状态 1,第 1 行
    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。真是郁闷死了。