Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return an empty result set from a procedure using T-SQL?

I'm interested in returning an empty result set from SQL Server stored procedures in certain events.

The intended behaviour is that a L2SQL DataContext.SPName().SingleOrDefault() will result in CLR null value.

I'm presently using the following solution, but I'm unsure whether it would be considered bad practice, a performance hazard (I could not find one by reading the execution plan), or if there is simply a better way:

SELECT * FROM [dbo].[TableName] WHERE 0 = 1; 

The execution plan is a constant scan with a trivial cost associated with it.

The reason I am asking this instead of simply not running any SELECTs is because I'm concerned previous SELECT @scalar or SELECT INTO statements could cause unintended result sets to be served back to L2SQL. Am I worrying over nothing?

like image 618
Jesse Hallam Avatar asked May 09 '10 00:05

Jesse Hallam


People also ask

Can we return table variable from stored procedure?

The RETURN exits the stored procedure, and nothing that follows it will be executed, including the SELECT statement on the following line. Otherwise, if you want the data for the entire table, as your question shows, add a SELECT after the INSERT . But don't put RETURN in front of it!


1 Answers

If you need column names in the response then proceed with the select TOP 0 * from that table, otherwise just use SELECT TOP 0 NULL. It should work pretty fast :)

like image 133
Alexander Avatar answered Oct 03 '22 07:10

Alexander