mr.sverrir
mr.sverrir

Reputation: 414

Is it possible to query data from tables by object_id?

I was wondering whether it is possible to query tables by specifying their object_id instead of table names in SELECT statements.

The reason for this is that some tables are created dynamically, and their structure (and names) are not known before, and yet I would like to be able to write sprocs that are capable of querying these tables and working on their content.

I know I can create dynamic statements and execute it, but maybe there are some better ways, and I would be grateful if someone could share how to approach it.

Thanks.

Upvotes: 0

Views: 57

Answers (1)

gbn
gbn

Reputation: 432271

You have to query sys.columns and build a dynamic query based on that.

There are no better ways: SQL isn't designed for adhoc or unknown sturctures.

I've never worked on an application in 20 years where I don't know what my data looks like. Either your data is persisted or it should be in XML or JSON or such if it's transient-

Upvotes: 2

Related Questions