日期:2014-05-18  浏览次数:20549 次

SQL字符串中的问题?
create table facmachine 
(
  facid varchar(100),
  machineid varchar(100),
  num int
)

insert into facmachine select '1000','100',10 union all
  select '1000','200',10 union all
  select '2000','100',100 union all
  select '3000', '400',100

select * from facmachine 

declare @sql nvarchar(1000)
set @sql = 'select facid [机型]'
set @sql = @sql+ ','''+machineid+'''= sum(case machineid when '''+[machineid]+''' then num else 0 end)' from (select distinct machineid from facmachine) dd
 
set @sql = @sql+', sum(num) as [总计] from facmachine group by facid '
exec (@sql)

drop table facmachine

  请问专家这个SQL语句错在什么地方,请教...

------解决方案--------------------
SQL code

第二个SET 也不能用,用select 
create   table   facmachine   
( 
    facid   varchar(100), 
    machineid   varchar(100), 
    num   int 
) 

insert   into   facmachine   select   '1000 ', '100 ',10   union   all 
    select   '1000 ', '200 ',10   union   all 
    select   '2000 ', '100 ',100   union   all 
    select   '3000 ',   '400 ',100 

select   *   from   facmachine   

declare   @sql   nvarchar(1000) 
set   @sql     =   'select   facid  as  [机型] ' 
select   @sql   =   @sql+   ', '''+machineid+ '''=   sum(case machineid when '''+[machineid]+ 
'''   then   num   else   0   end) '  
from   (select   distinct   machineid   from   facmachine)   dd 
  
set   @sql   =   @sql+ ',   sum(num)   as   [总计]   from   facmachine   group   by   facid   ' 
exec   (@sql) 

drop   table   facmachine

------解决方案--------------------
declare @sql nvarchar(1000) 
set @sql = 'select facid [机型] ' 
set @sql = @sql+ ', ' ' '+machineid+ ' ' '= sum(case machineid when ' ' '+[machineid]+ ' ' ' then num else 0 end) ' from (select distinct machineid from facmachine) dd 
  
set @sql = @sql+ ', sum(num) as [总计] from facmachine group by facid ' 
exec (@sql) 

drop table facmachine 

用 select 

SQL code

create   table   facmachine   
( 
    facid   varchar(100), 
    machineid   varchar(100), 
    num   int 
) 

insert   into   facmachine   select   '1000 ', '100 ',10   union   all 
    select   '1000 ', '200 ',10   union   all 
    select   '2000 ', '100 ',100   union   all 
    select   '3000 ',   '400 ',100 

select   *   from   facmachine   

declare   @sql   nvarchar(1000) 
set   @sql     =   'select   facid       [机型] ' 
 select   @sql   =  @sql+   ','''+machineid+ '''= sum(case   machineid   when   '''+[machineid]+ '''   then   num   else   0   end) ' from  (select   distinct   machineid   from   facmachine)   dd 
set   @sql   =   @sql+ ',   sum(num)   as   [总计]   from   facmachine   group   by   facid   ' 
exec   (@sql) 

drop   table   facmachine

------解决方案--------------------
SQL code
declare   @sql   nvarchar(1000) 
set @sql = 'select facid [机型]' 
select @sql = @sql+ ', sum(case machineid when ''' + machineid + ''' then num else 0 end) ['+machineid+']'
from (select distinct machineid from facmachine) dd 
set @sql = @sql+ ', sum(num) as [总计] from facmachine group by facid ' 
exec   (@sql)

------解决方案--------------------
SQL code

declare   @sql   nvarchar(1000) 
set   @sql     =  'select   facid [机型] ' 
select  @sql   =   @sql+  ', '''+machineid+ '''= sum(case   machineid   when   '''+[machineid]+ '''   then   num   else   0   end) '   from   (select   distinct   machineid   from   facmachine)   dd 
set   @sql   =   @sql+ ',   sum(num)   as   [总计]   from   facmachine   gro