本文介绍了如何在此Sp中使用@Flag创建存储过程以进行选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 HELP PLEAS !!!!! 我为批量插入创建了一个SP。单个批量插入正在工作然后我用标志和选择命令改变了SP。这里我怎么能通过插入标志? 单个批量插入(带标志)我calld sp& ; datatable dt1喜欢这个 public void bulk_insert() { 使用(SqlConnection con = new SqlConnection( @ 数据源=(LocalDB)\ v11.0; AttachDbFilename = D:\ MyPractice \browse_fie \ broowse_fie \ broowse.mdf; Integrated Security = True)) { 使用(SqlCommand cmd = new SqlCommand( SP_PERDRAW T)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = con; cmd.Parameters.AddWithValue( @ tblperdraw,dt1); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } alterd sp is..how我可以调用insert n select吗?必须在批量插入中将表作为参数传递 CREATE PROCEDURE SP_PERDRAWT( @ name varchar ( 20 ),地址 varchar ( 20 ),年龄 int , @ flag varchar ( 20 )) 如果(@ flag = ' insert') BEGIN @ tblperdraw createtype_perdraw READONLY SET NOCOUNT ON INSERT INTO mytable(姓名,地址,年龄) SELECT * FROM @ tblperdraw END if (@ flag = ' select') BEGIN 选择 * 来自 mytable END END 解决方案 我认为您的SP问题请验证它。 CREATE PROCEDURE SP_PERDRAWT( @name varchar(20), @address varchar(20),//此decalration必须以@ @age int开头,//这个decalration必须以@ 开始@flag varchar(20)) AS //在你的SP中没有这样的关键词AS和BEGIN这里 BEGIN // .. IF(@ flag ='insert')// if condition BEGIN @tblperdraw createtype_perdraw READONLY SET NOCOUNT ON INSERT INTO mytable(name ,地址,年龄) SELECT * FROM @tblperdraw END ELSE IF(@ flag ='select')//否则如果条件 BEGIN select * from mytable END END 请尝试 out 此更正 if i ' 错误地理解你的问题然后让我通知 您可能已经为表创建了类型,因此只需将其定义为参数即可。类似 CREATE PROCEDURE SP_PERDRAWT( @ name varchar ( 20 ),地址 varchar ( 20 ),年龄 int , @ flag varchar ( 20 ), @ tblperdraw createtype_perdraw) if (@ flag = ' insert') BEGIN SET NOCOUNT ON INSERT INTO mytable(name,地址,年龄) SELECT * FROM @ tblperdraw END if (@ flag = ' select') BEGIN 选择 * 来自 mytable END END 有关示例,您可以查看示例如何将多条记录传递给存储过程 [ ^ ] HELP PLEAS!!!!!I created a SP for bulk insertion.single bulk insertion is working then am alterd SP with flag and select command.Here how can I pass the insert flag?in single bulk insertion(with out flag)I calld sp &datatable dt1 like thispublic void bulk_insert() { using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\MyPractice\browse_fie\browse_fie\browse.mdf;Integrated Security=True")) { using (SqlCommand cmd = new SqlCommand("SP_PERDRAWT")) { cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = con; cmd.Parameters.AddWithValue("@tblperdraw", dt1); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } }alterd sp is..how can I call insert n select? have to pass table as parameter in bulk insertCREATE PROCEDURE SP_PERDRAWT(@name varchar(20), address varchar(20),age int,@flag varchar(20)) if(@flag='insert') BEGIN @tblperdraw createtype_perdraw READONLY SET NOCOUNT ON INSERT INTO mytable(name,address ,age) SELECT * FROM @tblperdraw END if(@flag='select') BEGIN select * from mytable ENDEND 解决方案 I think you issue with your SP please verify it.CREATE PROCEDURE SP_PERDRAWT( @name varchar(20), @address varchar(20), //this decalration must with start with @ @age int, //this decalration must with start with @ @flag varchar(20))AS // In your SP there no such a key words AS and BEGIN hereBEGIN //.. IF(@flag='insert') //if condition BEGIN @tblperdraw createtype_perdraw READONLY SET NOCOUNT ON INSERT INTO mytable(name,address ,age) SELECT * FROM @tblperdraw END ELSE IF(@flag='select') //else if condition BEGIN select * from mytableENDENDplease try it out with this correctionif i'm going wrong to understand your issue then let me informYou probably have already created the type for table so just define it as a parameter. Something likeCREATE PROCEDURE SP_PERDRAWT(@name varchar(20), address varchar(20),age int,@flag varchar(20), @tblperdraw createtype_perdraw ) if(@flag='insert') BEGIN SET NOCOUNT ON INSERT INTO mytable(name,address ,age) SELECT * FROM @tblperdraw END if(@flag='select') BEGIN select * from mytable ENDENDFor an example you can have a look at for example How to pass multiple records to a Stored Procedure[^] 这篇关于如何在此Sp中使用@Flag创建存储过程以进行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 07:46