读书人

SQL字符串中的有关问题

发布时间: 2012-01-21 21:31:43 作者: rapoo

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) 

读书人网 >SQL Server

热点推荐