• 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". 🙂

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/