SQL Server output parameter issue

后端 未结 8 1758
南旧
南旧 2020-12-24 11:42

I am using SQL Server 2008 Enterprise. I am learning OUTPUT parameter of SQL Server stored procedure. For example, stored procedure sp_add_jobschedule has an OUTPUT paramete

8条回答
  •  孤独总比滥情好
    2020-12-24 12:30

    One additional note regarding part of the original question:

    "Is it allowed not to provide any INPUT values for OUTPUT parameter (..)?"

    In SQLServer it is possible to specify a default value for an OUTPUT parameter (which as others have pointed out actually INOUT). Consider the following example in which you could specify a explicit value or let the function itself generate an ID.

    CREATE PROCEDURE test (@id uniqueidentifier = NULL OUTPUT) AS
    BEGIN
      IF @id IS NULL SET @id = NEWID()
      -- INSERT INTO xyz (...) VALUES (@id, ...)
      PRINT 'Insert with id: ' + CAST (@id as nvarchar(36))
    END
    GO
    
    DECLARE @insertedId uniqueidentifier
    EXECUTE test '00000000-0000-0000-0000-000000000000'
    EXECUTE test @insertedId OUTPUT
    PRINT @insertedId
    
    DROP PROCEDURE test
    

    This prints

    Insert with id: 00000000-0000-0000-0000-000000000000
    Insert with id: 67AE3D27-8EAB-4301-B384-30EEA1488440
    67AE3D27-8EAB-4301-B384-30EEA1488440
    

提交回复
热议问题