I'm learning sql from a book and I'm trying to write a stored procedure but I don't believe that I'm doing it correctly. Is the following way not valid in Microsoft SQL? If not, when is it valid, if ever?
我正在从书中学习SQL,我正在尝试编写存储过程,但我不相信我正确地做了。以下方式在Microsoft SQL中无效吗?如果没有,什么时候有效,如果有的话?
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
I get the following error
我收到以下错误
Msg 156, Level 15, State 1, Procedure wine_change, Line 1 Incorrect syntax near the keyword 'in'.
消息156,级别15,状态1,过程wine_change,第1行关键字'in'附近的语法不正确。
7 个解决方案
#1
31
T-SQL
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
#2
9
In T-SQL stored procedures for input parameters explicit 'in' keyword is not required and for output parameters an explicit 'Output' keyword is required. The query in question can be written as:
在输入参数的T-SQL存储过程中,不需要显式'in'关键字,对于输出参数,需要显式的'Output'关键字。有问题的查询可以写成:
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
and to execute above procedure we can write as:
并执行上述程序我们可以写为:
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
#3
3
I think it can help you:
我认为它可以帮助你:
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
Then, you can call the SP like this way, for example:
然后,您可以像这样调用SP,例如:
DECLARE @COUNTER INT
EXEC DEPT_COUNT 'DeptName', @COUNTER OUTPUT
SELECT @COUNTER
#4
1
CREATE PROCEDURE [dbo].[USP_StudentInformation]
@S_Name VARCHAR(50)
,@S_Address VARCHAR(500)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Date VARCHAR(50)
SET @Date = GETDATE()
IF EXISTS (
SELECT *
FROM TB_StdFunction
WHERE S_Name = @S_Name
AND S_Address = @S_Address
)
BEGIN
UPDATE TB_StdFunction
SET S_Name = @S_Name
,S_Address = @S_Address
,ModifiedDate = @Date
WHERE S_Name = @S_Name
AND S_Address = @S_Address
SELECT *
FROM TB_StdFunction
END
ELSE
BEGIN
INSERT INTO TB_StdFunction (
S_Name
,S_Address
,CreatedDate
)
VALUES (
@S_Name
,@S_Address
,@date
)
SELECT *
FROM TB_StdFunction
END
END
Table Name : TB_StdFunction
S_No INT PRIMARY KEY AUTO_INCREMENT
S_Name nvarchar(50)
S_Address nvarchar(500)
CreatedDate nvarchar(50)
ModifiedDate nvarchar(50)
#5
0
Create this way.
创造这种方式。
Create procedure dept_count(dept_name varchar(20),d_count integer)
begin
select count(*) into d_count
from instructor
where instructor.dept_name=dept_count.dept_name
end
#6
0
try this:
尝试这个:
create procedure dept_count( @dept_name varchar(20), @d_count INTEGER out)
AS
begin
select count(*) into d_count
from instructor
where instructor.dept_name=dept_count.dept_name
end
#7
0
Try this:
尝试这个:
create procedure dept_count(@dept_name varchar(20),@d_count int)
begin
set @d_count=(select count(*)
from instructor
where instructor.dept_name=dept_count.dept_name)
Select @d_count as count
end
Or
要么
create procedure dept_count(@dept_name varchar(20))
begin
select count(*)
from instructor
where instructor.dept_name=dept_count.dept_name
end
#1
31
T-SQL
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
#2
9
In T-SQL stored procedures for input parameters explicit 'in' keyword is not required and for output parameters an explicit 'Output' keyword is required. The query in question can be written as:
在输入参数的T-SQL存储过程中,不需要显式'in'关键字,对于输出参数,需要显式的'Output'关键字。有问题的查询可以写成:
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
and to execute above procedure we can write as:
并执行上述程序我们可以写为:
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
#3
3
I think it can help you:
我认为它可以帮助你:
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
Then, you can call the SP like this way, for example:
然后,您可以像这样调用SP,例如:
DECLARE @COUNTER INT
EXEC DEPT_COUNT 'DeptName', @COUNTER OUTPUT
SELECT @COUNTER
#4
1
CREATE PROCEDURE [dbo].[USP_StudentInformation]
@S_Name VARCHAR(50)
,@S_Address VARCHAR(500)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Date VARCHAR(50)
SET @Date = GETDATE()
IF EXISTS (
SELECT *
FROM TB_StdFunction
WHERE S_Name = @S_Name
AND S_Address = @S_Address
)
BEGIN
UPDATE TB_StdFunction
SET S_Name = @S_Name
,S_Address = @S_Address
,ModifiedDate = @Date
WHERE S_Name = @S_Name
AND S_Address = @S_Address
SELECT *
FROM TB_StdFunction
END
ELSE
BEGIN
INSERT INTO TB_StdFunction (
S_Name
,S_Address
,CreatedDate
)
VALUES (
@S_Name
,@S_Address
,@date
)
SELECT *
FROM TB_StdFunction
END
END
Table Name : TB_StdFunction
S_No INT PRIMARY KEY AUTO_INCREMENT
S_Name nvarchar(50)
S_Address nvarchar(500)
CreatedDate nvarchar(50)
ModifiedDate nvarchar(50)
#5
0
Create this way.
创造这种方式。
Create procedure dept_count(dept_name varchar(20),d_count integer)
begin
select count(*) into d_count
from instructor
where instructor.dept_name=dept_count.dept_name
end
#6
0
try this:
尝试这个:
create procedure dept_count( @dept_name varchar(20), @d_count INTEGER out)
AS
begin
select count(*) into d_count
from instructor
where instructor.dept_name=dept_count.dept_name
end
#7
0
Try this:
尝试这个:
create procedure dept_count(@dept_name varchar(20),@d_count int)
begin
set @d_count=(select count(*)
from instructor
where instructor.dept_name=dept_count.dept_name)
Select @d_count as count
end
Or
要么
create procedure dept_count(@dept_name varchar(20))
begin
select count(*)
from instructor
where instructor.dept_name=dept_count.dept_name
end