简体   繁体   English

如何从表(SQL Server数据库)中将值选择为参数(存储过程)?

[英]How to select values from a table (SQL Server database) into a parameter (Stored Procedure)?

I am trying to select few values from a table (SQL Server database) into few parameters (Stored Procedure) and here's what I did: 我正在尝试从表(SQL Server数据库)中将几个值选择为几个参数(存储过程),这是我所做的:

CREATE PROCEDURE [dbo].ValidateStudent
(
    @UserName varchar(50),
    @Password varchar(50),
    @Yes_No int OUTPUT,
    @StudentId int OUTPUT
)
AS
    SET NOCOUNT ON;
SELECT        @Yes_No = COUNT(*)
FROM            Student
WHERE        (UserName = @UserName) AND (Password = @Password)
SELECT        @StudentId = StudentId
FROM            Student
WHERE        (UserName = @UserName) AND (Password = @Password)

Since I am not sure whether it is the right way to achieve what I have mentioned, I need to know few things: 由于我不确定实现上述内容的正确方法,因此我需要了解的几件事:

  1. Is it syntactically valid to select 2 values in 2 select statements as in the query above inside 1 stored procedure? 如上面在1个存储过程中的查询中那样,在2条select语句中选择2个值在语法上是否有效?
  2. Is the way I have written the query to select values from table into parameter is valid or will work? 我编写查询以从表中选择值作为参数的方式有效还是可以使用?

Please tell me if the above query is valid and if not then suggest me with the valid query for the same. 请告诉我以上查询是否有效,如果无效,则建议我使用相同的有效查询。

Thanks, 谢谢,

You can populate both parameter values with one SELECT 您可以使用一个SELECT填充两个参数值

CREATE PROCEDURE [dbo].ValidateStudent
(
    @UserName varchar(50),
    @Password varchar(50),
    @Yes_No int OUTPUT,
    @StudentId int OUTPUT
)
AS
    SET NOCOUNT ON;
SELECT        @StudentId = StudentId, @Yes_No = COUNT(1)
FROM            Student
WHERE        (UserName = @UserName) AND (Password = @Password)
GROUP BY StudentId

That said, wouldn't the value of @Yes_No always be 1, unless you have more than one student with the same UserName and Password ? 就是说,@ Yes_No的值是否不会始终为1,除非您有多个具有相同UserNamePassword学生?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server 2000-使用SELECT语句的存储过程参数值 - SQL Server 2000 - Stored procedure parameter values with a SELECT statement 将表中的列值作为参数传递给存储过程sql - Passing column values from a table as parameter to a stored procedure sql 如何将SELECT语句作为参数传递给SQL Server中的存储过程? - How to pass the SELECT statement as a parameter to stored procedure in SQL Server? Excel中的表从SQL Server存储过程与工作簿中的参数字段 - Table in Excel from SQL Server stored procedure with parameter field in workbook SQL Server:如何将数据库名称用作存储过程中的参数? - SQL Server: How to use a database name as a parameter in a stored procedure? SQL Server:如何在存储过程中获取数据库名称作为参数 - SQL Server: how to get a database name as a parameter in a stored procedure 如何使用存储过程将datetime插入SQL Server数据库表中? - How to insert datetime into a SQL Server database table with a stored procedure? 表值作为SQL Server存储过程的参数 - Table values as parameters for SQL Server stored procedure 如何使用SQL Server从具有多个select语句的存储过程中获取一个数据表 - How to get one data table from Stored procedure that has multiple select statements using sql server 如何将值从存储过程中的表参数传递到另一个存储过程? - How to pass values from a table parameter in a Stored Procedure to another Stored Procedure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM