【问题标题】:SQL Server: how to create a stored procedureSQL Server:如何创建存储过程
【发布时间】:2020-09-15 00:23:17
【问题描述】:

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

我收到以下错误

消息 156,级别 15,状态 1,过程 wine_change,第 1 行 关键字“in”附近的语法不正确。

【问题讨论】:

  • 你遇到了什么错误?
  • 你有什么问题?
  • Msg 156,级别 15,状态 1,过程 wine_change,第 1 行关键字“in”附近的语法不正确。
  • 去掉in关键字
  • 那个语法是Oracle PL/SQL

标签: sql-server tsql


【解决方案1】:

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】:

    在 T-SQL 存储过程中,输入参数不需要显式“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 
    

    【讨论】:

      【解决方案3】:

      我认为它可以帮助你:

      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
      

      【讨论】:

        【解决方案4】:

        试试这个:

         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 还会返回吗?
        • @eric:您想在查询中返回 d_count 作为讲师的数量吗?
        • @a_horse_with_no_name:我说的是 sql server 老兄。
        • @a_horse_with_no_name : 问题标签是 Tsql 和 Sql server。
        【解决方案5】:
        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)
        

        【讨论】:

          【解决方案6】:

          以这种方式创建。

          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 上运行?
          • 原来的参数有IN关键字。这将在 SQL Server 中工作。
          • 我不认为所示的into 在 T-SQL 中有效,无法将查询结果存储到变量中
          【解决方案7】:

          试试这个:

          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 语法混合在一起。绝对不会起作用。
          【解决方案8】:

          在 SQL Server Management Studio 中创建 SQL Server Store 过程

          • 扩展您的数据库
          • 以编程方式扩展
          • 右键单击存储过程并选择“新存储过程”

          现在,编写你的 Store 过程,例如,它可以如下所示

          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 = 数据库名称
          程序名称 = SP 的名称
          InputValue = 您的输入参数值(@LastName 和 @FirstName)和 type = 参数类型示例 nvarchar(50) 等。

          来源:Stored procedure in sql server (With Example)

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

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

          【讨论】:

            猜你喜欢
            • 2016-09-24
            • 2013-05-02
            • 2012-05-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多