Sean Lange (8/29/2013)
ScottPletcher (8/29/2013)
Here's an idea: let the proc get the schema name for you :-), then you don't even have to type it in at all!
create PROC [dbo].[sp_getrecords]
(
@TableName sysname
)
AS
declare @SchemaName sysname
select top (1) @SchemaName = SCHEMA_NAME(schema_id)
from sys.objects
where
name = @TableName and
type in ('u', 'v')
exec('select * from ['+@SchemaName+'].['+@TableName+']')
That works fine until you have a table with the same name in more than 1 schema and you want "the other one". 🙂
Yes, but nothing deals easily with that for-most-people unusual situation :-).
One could extend the code to check for the current user's default schema, check it first, etc., but is it really worth the effort?
SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.