当前位置: 代码迷 >> Sql Server >> 必须声明标量变量 "@CourseID"该怎么处理
  详细解决方案

必须声明标量变量 "@CourseID"该怎么处理

热度:55   发布时间:2016-04-27 14:31:36.0
必须声明标量变量 "@CourseID"。
C# code
 int a = 1;        Params1[0] = DB.MakeInParam("@CourseID", SqlDbType.Int, 4, a);               //试卷编号              DataSet ds1 = DB.GetDataSet("Proc_PaperDetail",Params1);        GridView2.DataSource = ds1;        GridView2.DataBind();        SqlParameter[] Params2 = new SqlParameter[1];        int b = 2;        Params2[0] = DB.MakeInParam("@CourseID", SqlDbType.Int, 4, b);               //试卷编号              DataSet ds2 = DB.GetDataSet("Proc_PaperDetail", Params2);        GridView5.DataSource = ds2;


存储过程代码:
SQL code
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[Proc_PaperDetail]        (@CourseID        [int])ASbegin     declare @sql nvarchar(1000)if @CourseID=1 begin        set @sql='select * from MultiProblem where [email protected]'    exec sp_executesql @sql         endelse if @CourseID=2 begin        set @sql='select * from MultiProblem where [email protected]'    exec sp_executesql @sql         endelse if @CourseID=3 begin        set @sql='select * from MultiProblem where [email protected]'    exec sp_executesql @sql         endelse if @CourseID=4 begin        set @sql='select * from MultiProblem where [email protected]'    exec sp_executesql @sql         endelse begin        set @sql='select * from MultiProblem where [email protected]'    exec sp_executesql @sql         endend


------解决方案--------------------
SQL code
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[Proc_PaperDetail]        (@CourseID        [int])ASbegin     declare @sql nvarchar(1000)if @CourseID=1 begin        set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID)    exec sp_executesql @sql         endelse if @CourseID=2 begin        set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID)    exec sp_executesql @sql         endelse if @CourseID=3 begin        set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID)    exec sp_executesql @sql         endelse if @CourseID=4 begin        set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID)    exec sp_executesql @sql         endelse begin        set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID)    exec sp_executesql @sql         endend
------解决方案--------------------
表名或者字段名位变量的时候 需要用动态SQL。
------解决方案--------------------
SQL code
alter procedure [dbo].[proc_paperdetail] ( @courseid int )as     begin         declare @sql nvarchar(4000)        set @sql = 'select * from multiproblem where courseid='            + ltrim(@courseid)           exec (@sql)            end
  相关解决方案