首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL字符串中的有关问题

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

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   group   by   facid   ' exec   (@sql) 

热点排行