简体   繁体   中英

TSQL stored proc nonsense

Saw this on a test- My SQL experience is basic CRUD-related and I'm not sure what the intent of this would be, if any?

CREATE PROCEDURE [dbo].[uspGetAccountID]
(
        @AccountID int = 1
)
AS
        SELECT @AccountID 
GO 

What will be the output if the procedure is called as follows, and why?

exec dbo.uspGetAccountID 5

The zero is not the result of the code, it is the return value

SSMS actually generates this

USE [MyDB]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[uspGetAccountID] 
        @AccountID = 5

SELECT  'Return Value' = @return_value

GO

So you would have had 2 result sets. One for code, one for the return code

Add a RETURN 42 to the code in the procedure after the SELECT , you get 42 in the second result...

The result will be 5.

Default values on parameters are only assigned if no value has been provided.

You can create a stored procedure with optional parameters by specifying a default value for optional parameters. When the stored procedure is executed, the default value is used if no other value has been specified.

MSDN Article

Looks like a test stored procedure. It takes a parameter called @AccountID with a default value of 1 .

exec dbo.uspGetAccountID 5

Would result in 5 being selected.

exec [dbo].[uspGetAccountID] 
-- should result in 1
exec [dbo].[uspGetAccountID] 5
-- should result in 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM