if exists(Select Name From SySObjects Where Name = 'V_TEST' And Xtype ='V')
Drop View V_TESTdeclare @sql varchar(4000)
set @sql = ' create view V_TEST as select ProjectID'
select @sql = @sql + ',max(case PropertyID when '''+PropertyID+''' then Value end) ['+PropertyID+']'
from (select distinct PropertyID from ProjectProperty) as a
select @sql = @sql+' from ProjectProperty group by ProjectID'
exec(@sql)select * From V_TEST

解决方案 »

  1.   


    drop view if exists V_TEST;
    set @sql = 'create view V_TEST as select ProjectID';
    set @sql = concat(@sql,',max(case PropertyID when PropertyID then Value end) [PropertyID]','from 
    (select distinct PropertyID from ProjectProperty) as a');
    set @sql =concat( @sql,' from ProjectProperty group by ProjectID');prepare s1 from @sql;
    execute s1;
    deallocate prepare s1;select * From V_TEST;
      

  2.   

    这里是不是少了Create View语句,运行没有抱错,但视图没有建立
      

  3.   

    不奥意思。忘掉CREATE VIEW了。
      

  4.   

    执行了以后,报告如下错误,不知道是什么原因:
    Unknown prepared statement handler (s1) given to DEALLOCATE PREPARE