我正在从一本书中学习sql,我正在尝试编写一个存储过程,但我不相信我做得对。下列方法在Microsoft中无效吗?如果没有,什么时候有效?
create procedure dept_count(in dept_name varchar(20), out d_count integer)
begin
select count(*) into d_count
from instructor
where instructor.dept_name=dept_count.dept_name
end
我得到以下错误
Msg 156,第15级,状态1,过程wine_change,第1行在关键字'in‘附近不正确的语法。
发布于 2013-11-12 23:33:58
T-SQL
/*
Stored Procedure GetstudentnameInOutputVariable is modified to collect the
email address of the student with the help of the Alert Keyword
*/
CREATE PROCEDURE GetstudentnameInOutputVariable
(
@studentid INT, --Input parameter , Studentid of the student
@studentname VARCHAR (200) OUT, -- Output parameter to collect the student name
@StudentEmail VARCHAR (200)OUT -- Output Parameter to collect the student email
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname,
@StudentEmail=email FROM tbl_Students WHERE studentid=@studentid
END
发布于 2013-11-13 02:02:41
在T存储过程中,输入参数不需要显式“In”关键字,对于输出参数,需要显式“输出”关键字。有关的查询可以写成:
CREATE PROCEDURE dept_count
(
-- Add input and output parameters for the stored procedure here
@dept_name varchar(20), --Input parameter
@d_count int OUTPUT -- Output parameter declared with the help of OUTPUT/OUT keyword
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Statements for procedure here
SELECT @d_count = count(*)
from instructor
where instructor.dept_name=@dept_name
END
GO
为了执行上述过程,我们可以写成:
Declare @dept_name varchar(20), -- Declaring the variable to collect the dept_name
@d_count int -- Declaring the variable to collect the d_count
SET @dept_name = 'Test'
Execute dept_count @dept_name,@d_count output
SELECT @d_count -- "Select" Statement is used to show the output
发布于 2015-07-23 05:13:48
我觉得它能帮到你:
CREATE PROCEDURE DEPT_COUNT
(
@DEPT_NAME VARCHAR(20), -- Input parameter
@D_COUNT INT OUTPUT -- Output parameter
-- Remember parameters begin with "@"
)
AS -- You miss this word in your example
BEGIN
SELECT COUNT(*)
INTO #D_COUNT -- Into a Temp Table (prefix "#")
FROM INSTRUCTOR
WHERE INSTRUCTOR.DEPT_NAME = DEPT_COUNT.DEPT_NAME
END
然后,您可以这样调用SP,例如:
DECLARE @COUNTER INT
EXEC DEPT_COUNT 'DeptName', @COUNTER OUTPUT
SELECT @COUNTER
https://stackoverflow.com/questions/19947878
复制