SQL Server:如何创建存储过程

26

我正在从一本书学习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

我遇到了以下错误

Msg 156, Level 15, State 1, Procedure wine_change, Line 1 Incorrect syntax near the keyword 'in'.


2
你收到了什么错误? - Amir Keshavarz
你对这个有什么问题? - MusicLovingIndianGirl
Msg 156,级别 15,状态 1,过程 wine_change,行 1 关键字 'in' 附近语法不正确。 - Spark323
1
删除 in 关键字 - LuigiEdlCarno
1
那个语法是Oracle PL/SQL。 - user330315
显示剩余2条评论
8个回答

36

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

11

在 T-SQL 存储过程中,输入参数不需要显式使用 'in' 关键字,而输出参数需要显式使用 'Output' 关键字。有关所讨论查询的编写方式如下:

在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

执行上述过程的方法如下:

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 

4
我认为这可以帮助你:
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

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)

1
尝试这个:

 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

或者

create procedure dept_count(@dept_name varchar(20))
           begin
            select count(*)
                           from instructor
                            where instructor.dept_name=dept_count.dept_name
           end

d_count仍然会返回吗? - Spark323
@eric:你想在查询中将d_count作为教师数量返回吗? - Amir Keshavarz
@a_horse_with_no_name:我在说SQL Server啊。 - Amir Keshavarz
@a_horse_with_no_name:该问题标签为Tsql和Sql Server。 - Amir Keshavarz

0

以这种方式创建。

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

这与原始版本有何不同?这在 MS SQL Server 上如何运作? - user330315
原始版本的参数使用了 IN 关键字。这在 SQL Server 中是有效的。 - MusicLovingIndianGirl
2
我认为在 T-SQL 中所示的 into 无法有效地将查询结果存储到变量中。 - user330315

0

试试这个:

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

您正在混合使用 T-SQL 参数 (@dept_name) 和 Oracle 语法。这肯定行不通。 - user330315

0

在SQL Server Management Studio中创建SQL Server存储过程

  • 展开您的数据库
  • 展开可编程性
  • 右键单击存储过程,选择“新建存储过程”

现在,编写您的存储过程,例如,可以像下面这样:

USE DatabaseName;  
GO  
CREATE PROCEDURE ProcedureName 
 @LastName nvarchar(50),   
 @FirstName nvarchar(50)   
AS   

SET NOCOUNT ON;  
 
//Your SQL query here, like
Select  FirstName, LastName, Department  
FROM HumanResources.vEmployeeDepartmentHistory  
WHERE FirstName = @FirstName AND LastName = @LastName  
GO  

其中,DatabaseName = 您的数据库名称
ProcedureName = 存储过程名称
InputValue = 输入参数值(@LastName 和 @FirstName)和类型 = 参数类型示例 nvarchar(50) 等。

来源:SQL Server 中的存储过程(带示例)

要执行上述存储过程,您可以使用以下示例查询

EXECUTE ProcedureName @FirstName = N'Pilar', @LastName = N'Ackerman';  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接