I have a Database and i want to execute few queries on it, and the results of the queries i.e. message, has to be comapared in my code if it is expected or not. Please let me know how to capture the output of the SQL queries in any variable that can be used later in code for comparision.
2 Answers
Try the following Method :
- Create a Table with the Same structure as of your Procedure output
- Insert the Result of the SP Execution to the Table
- Compare your Query result with the Table
Like this
CREATE PROCEDURE dbo.uSp_Temp
AS
SELECT
GETDATE() "MyDate"
DECLARE @T TABLE
(
MyDate DATE
)
INSERT INTO @T
EXEC uSp_Temp
SELECT
*
FROM @T
Comments
You can use Temp tables or temp variable to save the result set of a query.
Below is the sample for temp table
create table #temp (id int)
insert into #temp
select 1 as id
select * from #temp
Below is sample for Temp variable
declare @temp table (id int)
insert into @temp
select 1 as id
select * from @temp