'And' 'Or' 条件使用 SQL 服务器中的存储过程
'And' 'Or' condition using stored procedure in SQL Server
我对在 SQL Server 2005 中使用存储过程有疑问。
这是我的存储过程:
ALTER PROCEDURE [dbo].[tst_user_sp]
-- Add the parameters for the stored procedure here
@p1 nvarchar = null,
@p2 nvarchar = null AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT state
FROM tst_user
WHERE user_id = @p1 AND user_pwd = @p2;
END
我的预期输出是获取 'state' 的值。它返回了一条空记录。
但是,将 And
更改为 Or
后,我得到了输出。为什么我不能使用 And
获得输出?
我的 sql 查询:
exec tst_user_sp 'lackh', 's'
我不知道这是否与您遇到的问题有关,但您的存储过程存在重大问题。定义是:
ALTER PROCEDURE [dbo].[tst_user_sp]
-- Add the parameters for the stored procedure here
@p1 nvarchar = null,
@p2 nvarchar = null AS
这对 nvarchar
字段使用 默认值 长度,默认值因上下文而异。在 SQL 中使用字符串类型时,您应该 始终 使用长度。类似于:
ALTER PROCEDURE [dbo].[tst_user_sp] (
-- Add the parameters for the stored procedure here
@p1 nvarchar(4000) = null,
@p2 nvarchar(4000) = null
) AS
考虑到您在该过程中所做的事情,由于 where
子句的定义方式,使用默认值将不会返回任何值。
我对在 SQL Server 2005 中使用存储过程有疑问。
这是我的存储过程:
ALTER PROCEDURE [dbo].[tst_user_sp]
-- Add the parameters for the stored procedure here
@p1 nvarchar = null,
@p2 nvarchar = null AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT state
FROM tst_user
WHERE user_id = @p1 AND user_pwd = @p2;
END
我的预期输出是获取 'state' 的值。它返回了一条空记录。
但是,将 And
更改为 Or
后,我得到了输出。为什么我不能使用 And
获得输出?
我的 sql 查询:
exec tst_user_sp 'lackh', 's'
我不知道这是否与您遇到的问题有关,但您的存储过程存在重大问题。定义是:
ALTER PROCEDURE [dbo].[tst_user_sp]
-- Add the parameters for the stored procedure here
@p1 nvarchar = null,
@p2 nvarchar = null AS
这对 nvarchar
字段使用 默认值 长度,默认值因上下文而异。在 SQL 中使用字符串类型时,您应该 始终 使用长度。类似于:
ALTER PROCEDURE [dbo].[tst_user_sp] (
-- Add the parameters for the stored procedure here
@p1 nvarchar(4000) = null,
@p2 nvarchar(4000) = null
) AS
考虑到您在该过程中所做的事情,由于 where
子句的定义方式,使用默认值将不会返回任何值。