0

I have the following code:

  SELECT c.name AS ColName, t.name AS TableName
  FROM sys.columns c
      JOIN sys.tables t ON c.object_id = t.object_id
  WHERE c.name LIKE '%MyName%'

I would like the code above to be put the TVF in a tools database, and call it from where-ever on the server and search the database of my choosing.

Is this even possible, would I have to use SP with the exec statement ? My concern is that I have other frequently used queries that I would also like to store in this but they are much longer...

0

1 Answer 1

1

The only way to use a variable for a database is to use dynamic sql, which is not possible in a TVF. You could only do it with a stored procedure.

Sign up to request clarification or add additional context in comments.

3 Comments

Or use OpenQuery. :)
MSDN says that OPENQUERY does not accept variables for its arguments, which jives with my experience with it. Am I missing something?
No I meant you could use OpenQuery to execute your created string. Still can't execute it in a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.