CREATE PROCEDURE [dbo].[OLTR_ExamSelectCountThree] 

@StudentID varchar(10), 
@CourseID varchar(10) 

AS DECLARE @tn varchar(100), 
    @sql varchar(4000), 
    @yy nvarchar(30) 
set @yy=getdate() 
set @yy=Year(@yy) 
set @yy=substring(@yy,3,2) 
  set @tn='OLTR_ExamStudentExamRecard'+@yy set @sql= 
'select Count(ScoreNum1) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' 
'select Count(ScoreNum2) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' 'select Count(ScoreNum3) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' 
GO 

解决方案 »

  1.   

    用“+”连接字符串CREATE PROCEDURE [dbo].[OLTR_ExamSelectCountThree] 

    @StudentID varchar(10), 
    @CourseID varchar(10) 

    AS DECLARE @tn varchar(100), 
        @sql varchar(4000), 
        @yy nvarchar(30) 
    set @yy=getdate() 
    set @yy=Year(@yy) 
    set @yy=substring(@yy,3,2) 
      set @tn='OLTR_ExamStudentExamRecard'+@yy set @sql= 
    'select Count(ScoreNum1) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' +
    'select Count(ScoreNum2) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' +
    'select Count(ScoreNum3) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' 
    GO 
      

  2.   

    怎么没看你用EXEC()??
    不然怎么执行...
      

  3.   


    CREATE PROCEDURE [dbo].[OLTR_ExamSelectCountThree] 

    @StudentID varchar(10), 
    @CourseID varchar(10) 

    AS DECLARE @tn varchar(100), 
        @sql varchar(4000), 
        @yy nvarchar(30) 
    set @yy=getdate() 
    set @yy=Year(@yy) 
    set @yy=substring(@yy,3,2) 
      set @tn='OLTR_ExamStudentExamRecard'+@yy set @sql= 
    'select Count(ScoreNum1) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' +' '+
    'select Count(ScoreNum2) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' +' '+
    'select Count(ScoreNum3) from '+@tn+' where StudentID='''+@StudentID+''' and CourseID='''+@CourseID+'''' 
    exec(@sql)
    GO 
      

  4.   

    将@sql print一下看看,然后拷到sql里运行一下