日期:2014-05-18 浏览次数:20581 次
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;
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[Proc_PaperDetail] (@CourseID [int]) AS begin declare @sql nvarchar(1000) if @CourseID=1 begin set @sql='select * from MultiProblem where CourseID=@CourseID' exec sp_executesql @sql end else if @CourseID=2 begin set @sql='select * from MultiProblem where CourseID=@CourseID' exec sp_executesql @sql end else if @CourseID=3 begin set @sql='select * from MultiProblem where CourseID=@CourseID' exec sp_executesql @sql end else if @CourseID=4 begin set @sql='select * from MultiProblem where CourseID=@CourseID' exec sp_executesql @sql end else begin set @sql='select * from MultiProblem where CourseID=@CourseID' exec sp_executesql @sql end end
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[Proc_PaperDetail] (@CourseID [int]) AS begin declare @sql nvarchar(1000) if @CourseID=1 begin set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID) exec sp_executesql @sql end else if @CourseID=2 begin set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID) exec sp_executesql @sql end else if @CourseID=3 begin set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID) exec sp_executesql @sql end else if @CourseID=4 begin set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID) exec sp_executesql @sql end else begin set @sql='select * from MultiProblem where CourseID='+rtrim(@CourseID) exec sp_executesql @sql end end
------解决方案--------------------
表名或者字段名位变量的时候 需要用动态SQL。
------解决方案--------------------
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