开发者

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

开发者 https://www.devze.com 2022-12-29 10:33 出处:网络
I\'m interested in returning an empty result set from SQL Server stored procedures in certain events.

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 L2S开发者_如何学JAVAQL. Am I worrying over nothing?


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 :)


That is a reasonable approach. Another alternative is:

SELECT TOP 0 * FROM [dbo].[TableName]


If you want to simply retrieve the metadata of a result set w/o any actual row, use SET FMTONLY ON.


I think the best solution is top 0 but not using a dummy table. This does it for me

select top 0 null as column1, null as column2.

Using e.g. a system table may be fine for performance but looks unclean.


It's an entirely reasonable approach.

To alleviate any worries about performance (whoch you shouldn't have any in the first place - the server's smart enough to avoid table scanning for 1=0), pick a table that's very small and not heavily used - I'm sure your DB schema has one.

0

精彩评论

暂无评论...
验证码 换一张
取 消