Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL存储过程中受限制的用户输入值_Sql_Sql Server_Stored Procedures - Fatal编程技术网

SQL存储过程中受限制的用户输入值

SQL存储过程中受限制的用户输入值,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我希望用户的输入2限制为“是”或“否”。有人能给我提供语法吗 我找到了默认值的synax create procedure db.Test @input1 varchar(max), @input2 {Either 'yes' or 'no'} 但是找不到任何限制输入。您需要使用和if语句进行检查。对于您需要的内容,没有“限制”验证器 @input2 varchar(max) default 'yes' 您需要使用和if语句进行检查。对于您需要的内容,没有“限制”验证器 @input2 va

我希望用户的输入2限制为“是”或“否”。有人能给我提供语法吗

我找到了默认值的synax

create procedure db.Test
@input1 varchar(max), @input2 {Either 'yes' or 'no'}

但是找不到任何限制输入。

您需要使用和
if
语句进行检查。对于您需要的内容,没有“限制”验证器

@input2 varchar(max) default 'yes'

您需要使用和
if
语句进行检查。对于您需要的内容,没有“限制”验证器

@input2 varchar(max) default 'yes'

对于这个问题,您可以随时使用案例,我为您编写了这个示例,以便您可以对其进行一些探索

将服务器输出设置为on 抵销

create procedure db.Test @input1 varchar(max), @input2 varchar(3)
as
begin
    if @input2 not in ('yes', 'no')
    begin
       --raiserror or similar
       return
    end

(...)

end
go
对于“是”或“否”输入,可以使用类似的代码。
希望这对您有所帮助:)

您可以始终使用案例来解决此问题,我为您编写了此示例,以便您可以对其进行一些探索

将服务器输出设置为on 抵销

create procedure db.Test @input1 varchar(max), @input2 varchar(3)
as
begin
    if @input2 not in ('yes', 'no')
    begin
       --raiserror or similar
       return
    end

(...)

end
go
对于“是”或“否”输入,可以使用类似的代码。
希望这有帮助:)

您可以为RAISERROR创建自己的自定义错误消息

declare
Ship_Value Number(1) := &ShipNumber;
ship_method_type varchar(20);
ship_current_date varchar(20);

begin
select to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS')
into ship_current_date
from dual;

case Ship_Value when 1 then
select ship_method_desc
into ship_method_type
from ship_method
where ship_method_id_no = 1;  
dbms_output.put_line('Date: ' || ship_current_date);
dbms_output.put_line('Shipping method 1 is:' || ship_method_type);

when 2 then
select ship_method_desc
into ship_method_type
from ship_method
where ship_method_id_no = 2;
dbms_output.put_line('Date: ' || ship_current_date);
dbms_output.put_line('Shipping method 2 is:' || ship_method_type);

when 3 then 
select ship_method_desc
into ship_method_type
from ship_method
where ship_method_id_no = 3;
dbms_output.put_line('Date: ' || ship_current_date);
dbms_output.put_line('Shipping method 3 is:' || ship_method_type);

else
dbms_output.put_line('Date: ' || ship_current_date);
dbms_output.put_line('only 1-3 exists');
end case;
end;
在此之后,errorid 50010将指向消息Invalid input parameters

exec sp_addmessage @msgnum=50010,@severity=1,@msgtext='Invalid input parameters'

您可以为RAISERROR创建自己的自定义错误消息

declare
Ship_Value Number(1) := &ShipNumber;
ship_method_type varchar(20);
ship_current_date varchar(20);

begin
select to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS')
into ship_current_date
from dual;

case Ship_Value when 1 then
select ship_method_desc
into ship_method_type
from ship_method
where ship_method_id_no = 1;  
dbms_output.put_line('Date: ' || ship_current_date);
dbms_output.put_line('Shipping method 1 is:' || ship_method_type);

when 2 then
select ship_method_desc
into ship_method_type
from ship_method
where ship_method_id_no = 2;
dbms_output.put_line('Date: ' || ship_current_date);
dbms_output.put_line('Shipping method 2 is:' || ship_method_type);

when 3 then 
select ship_method_desc
into ship_method_type
from ship_method
where ship_method_id_no = 3;
dbms_output.put_line('Date: ' || ship_current_date);
dbms_output.put_line('Shipping method 3 is:' || ship_method_type);

else
dbms_output.put_line('Date: ' || ship_current_date);
dbms_output.put_line('only 1-3 exists');
end case;
end;
在此之后,errorid 50010将指向消息Invalid input parameters

exec sp_addmessage @msgnum=50010,@severity=1,@msgtext='Invalid input parameters'

为什么不使用位数据类型呢。该值只能是0或1为什么不使用位数据类型。该值只能为0或1