Monday, March 12, 2012

Is there such a thing as table name wildcards?

Hey all,

I have a datagrid with populated by this query: SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_TYPE = 'BASE TABLE')

I have paging, sorting and selection enabled.

Now I am looking for a way to use a wild card as a placeholder for the table name in my select statements so I can use the valued selected from the datagrid.

Example : SELECT * FROM %TABLENAME%


TIA

WOOHOO! my first post.

You probably need to use dynamic SQL for this.

EXEC('SELECT * FROM ' + @.tablename)

|||

Thanks for pointing me in the right direction. I looked into dynamic SQL and ran with this stored procedure:

ALTER Procedure GenericTableSelect
@.TABLE_NAMEVarChar(100)
AS

Declare @.SQLVarChar(1000)

SELECT @.SQL ='SELECT * FROM '
SELECT @.SQL = @.SQL + @.TABLE_NAME

Exec ( @.SQL)


Some more info on dynamic sql : http://www.sqlteam.com/item.asp?ItemID=4599

No comments:

Post a Comment