本文介绍了为什么存储过程的条件语句不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨。 我设计了一个带有asp.net4的webform,用于Employees.in我的一个页面有Checkbox和Textbox.so如果用户选中了复选框,则表示他决定(显示我已经在组织内部学习),并且在数据库中我有mada它boolian(0,1),否则如果他没有检查Checkbox文本框可用于他并且可以写在里面,还有一些Nvarchar(他已经研究了一些其他的地方)将被插入数据库。 a部分代码如下: 开关(IsAlmostafaCheckBox.Checked) { case true : command.Parameters.AddWithValue( @ IsInOrganization, this .IsOrganizationCheckBox.Checked); command.Parameters.AddWithValue( @ InstituteName,DBNull.Value); break ; case false : command.Parameters.AddWithValue( @ InstituteName, this .InstituteNameTextBox.Text); command.Parameters.AddWithValue( @ IsInOrganization,此 .IsOrganizationCheckBox.Checked); break ; } 但问题是当我按下按钮时发生错误并说你已经省略了您的存储过程中的IsInOrganization参数,并且不能接受数据库中的空值 所以因为我不想让这个字段Nullable,我试图更改存储过程,如下所示: ALTER PROCEDURE [dbo]。[Insert_GeneralPassedTerms_SP]( @ CityId int , @ IsInOrganization bit , @ TermTitle nvarchar ( 1000 ), @ InstituteName nvarchar ( 500 ), @ Date 日期时间, @ Duration decimal ( 4 , 1 ), @ Score decimal ( 4 , 2 ), @ Description nvarchar ( 1000 )) AS begin if @InstituteName = null begin insert 进入 GeneralPassedTerms( CityId, IsInOrganization, TermTitle, InstituteName, 日期,持续时间,分数,说明) 值( @ CityId , @ IsInOrganization , @ TermTitle , ' Null', @ Date , @ Duration , @ Score , @ Description ) End else Insert into GeneralPassedTerms( CityId, IsInOrganization, TermTitle,学院名称, 日期,持续时间,分数,描述) 值( @ CityId , @ IsInOrganization , @ TermTitle , @ InstituteName , @ Date , @ Duration , @ Score , @ Description ) 选择 @@ IDENTITY end 我不知道这个程序是否正确?你能帮帮我吗?解决方案 这个错误意味着它的意思。如果您的proc提供参数的默认值,则不需要设置这些参数。如果希望默认值为null,则在proc中的参数中输入put = null,或将其添加到集合中并将其设置为null。它没有神奇地显示为null,如果你没有指定它 试试这段代码希望你的查询得到解决。你不需要if else条件。可以使用下面的代码实现... ALTER PROCEDURE [dbo]。[Insert_GeneralPassedTerms_SP]( @CityId int, @IsInOrganization位, @TermTitle nvarchar(1000), @InstituteName nvarchar(500), @Date日期时间, @Duration decimal(4,1​​) , @Score decimal(4,2), @Description nvarchar(1000)) AS begin insert into GeneralPassedTerms ( CityId, IsInOrganization, TermTitle, InstituteName,日期,持续时间,分数,描述)价值( @CityId, @IsInOrganization, @TermTitle, isnull(@InstituteName,''Null'',@ InstituteName), @Date, @Duration, @Score, @Description ) select @@ IDENTITY end Hi.I have designed a webform wiht asp.net4 which is for Employees.in one of my page there is Checkbox and also a Textbox.so if the user checked that checkbox ,it means he has decided (to show i have studied inside the organization), and also in the Database i have mada it boolian (0,1), in otherwise if he didn''t check the Checkbox the Textbox to be available for him and can write inside , and also some Nvarchar(he has studied some where else) will be inserted in the database.a part of the code is in below:switch (IsAlmostafaCheckBox.Checked) { case true: command.Parameters.AddWithValue("@IsInOrganization", this.IsOrganizationCheckBox.Checked); command.Parameters.AddWithValue("@InstituteName", DBNull.Value); break; case false: command.Parameters.AddWithValue("@InstituteName", this.InstituteNameTextBox.Text); command.Parameters.AddWithValue("@IsInOrganization", this.IsOrganizationCheckBox.Checked); break; }but the problem is that when i press the button an error occures and says that you have omitted the IsInOrganization parameter in your Stored Procedure , and can not accept the null value in the databaseso because i didn''t want to make the field Nullable, i tried to change the stored procedure , like this :ALTER PROCEDURE [dbo].[Insert_GeneralPassedTerms_SP](@CityId int,@IsInOrganization bit,@TermTitle nvarchar(1000),@InstituteName nvarchar(500),@Date Datetime ,@Duration decimal(4,1),@Score decimal(4,2),@Description nvarchar(1000))ASbegin if @InstituteName=nullbegininsert into GeneralPassedTerms(CityId,IsInOrganization,TermTitle,InstituteName,Date,Duration,Score,Description)Values(@CityId,@IsInOrganization,@TermTitle,'Null',@Date,@Duration,@Score,@Description)End else Insert into GeneralPassedTerms(CityId,IsInOrganization,TermTitle,InstituteName,Date,Duration,Score,Description) Values(@CityId,@IsInOrganization,@TermTitle,@InstituteName,@Date,@Duration,@Score,@Description) select @@IDENTITYendI don''t whether this procedure is correct or not ? would you please help me? 解决方案 The error means what it says. If your proc provides default values for parameters, those parameters do not need to be set. If you want the default to be null, put = null in the parameter in the proc, or add it to the collection and set it to null. It doesn''t magically appear as null, if you did not specify itTry this code hope your query will get resolved. You do not required if else condition. It can be achieve using below code...ALTER PROCEDURE [dbo].[Insert_GeneralPassedTerms_SP](@CityId int,@IsInOrganization bit,@TermTitle nvarchar(1000),@InstituteName nvarchar(500),@Date Datetime ,@Duration decimal(4,1),@Score decimal(4,2),@Description nvarchar(1000))ASbegin insert into GeneralPassedTerms(CityId,IsInOrganization,TermTitle,InstituteName,Date,Duration,Score,Description)Values(@CityId,@IsInOrganization,@TermTitle,isnull(@InstituteName,''Null'',@InstituteName),@Date,@Duration,@Score,@Description) select @@IDENTITYend 这篇关于为什么存储过程的条件语句不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-18 14:47