关键字 'union' 附近有语法错误。

时间:2021-02-26 19:57:51
我的代码如下:

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([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,36) and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb
pivot (max(ipvn) for itemid in ([35],[3530])) ba

运行时提示:关键字 'union' 附近有语法错误。

4 个解决方案

#1


我需要把两个结果合并在一起,应怎么修改这样的语句?

#2



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,36) and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb
pivot (max(ipvn) for itemid in ([35],[3530])) ba')

#3


上面和下面字段名不一致,字段数量好像也不一样,资料类型不知道一不一样。
好像很乱!

#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([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,36) and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb
pivot (max(ipvn) for itemid in ([35],[3530])) ba')

#1


我需要把两个结果合并在一起,应怎么修改这样的语句?

#2



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,36) and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb
pivot (max(ipvn) for itemid in ([35],[3530])) ba')

#3


上面和下面字段名不一致,字段数量好像也不一样,资料类型不知道一不一样。
好像很乱!

#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([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,36) and ipv.ParameterId=ip.Parameterid and ip.ParameterCaId=c.ParameterCaId) bb
pivot (max(ipvn) for itemid in ([35],[3530])) ba')