TSQLT Returning Results from a Stored Procedure

爱⌒轻易说出口 提交于 2020-01-05 08:26:50

问题


In TSQLT, I'm trying to return a result from a stored procedure and add it to a variable so that I can assert if it matches my expected result.

I've seen loads of examples of returning results from functions but none where a stored procedure is called.

Does anybody have examples that they could share?

Thanks in advance


回答1:


If you want to get a variable back from a stored procedure one way to do this is to use an out parameter

CREATE PROC MyTest
(@myVar int output)
AS
BEGIN
  SET @myVar = 10
END
GO
DECLARE @x int
EXEC MyTest @myVar=@x output
SELECT @x

If you are getting a result set back from the stored procedure, here is an example from a tSQLt test that I wrote. I haven't bothered with the whole test because this should give you what you need.

    CREATE TABLE #Actual (SortOrder int identity(1,1),LastName varchar(100), FirstName varchar(100), OrderDate datetime, TotalQuantity int) 
    -- Act  
    INSERT #Actual (LastName, FirstName, OrderDate, TotalQuantity)
    EXEC Report_BulkBuyers @CurrentDate=@CurrentDate    

The trick here is that you have to create the #actual table first. It should contain the same columns as what is returned from the stored procedure.

Just as an aside, you may have noticed I have a SortOrder column in the #actual table. This is because I was interested in testing the order of the data returned for this specific report. EXEC tSQLt.AssertEqualsTable will match rows like for like, but does not match the order in which the rows appear in the expected and actual so the way to ensure the order is to add a SortOrder column (which is an identity column) to both the #expected and #actual




回答2:


Have a look here: http://technet.microsoft.com/en-us/library/ms188655.aspx

Lots of examples about returning values from a stored procedure. At the bottom of the page there is also an example about evaluating a return code.




回答3:


its actually really simple.

declare @variable int
exec @variable = _Stored_Procedure


来源:https://stackoverflow.com/questions/19271112/tsqlt-returning-results-from-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!