0

Want to know if it is at all possible to capture the calling query... example

SELECT SITE,NAME,COUNT(ITEMS) FROM SQLVIEW1 WHERE NAME = 'JIM'

in the SQL view it has something like this

DECLARE VAR1

where VAR1 would equal the above query that triggered the procedure.

"SELECT SITE,NAME,COUNT(ITEMS) FROM SQLVIEW1 WHERE NAME = 'JIM'"
JRhino
  • 99
  • 2
  • 14

1 Answers1

1

No, there is no such variable.

You could find out the currently executing statement through clever joins of system tables:

SELECT STATEMENT_STRING FROM SYS.M_TRANSACTIONS AS TR
INNER JOIN SYS.M_PREPARED_STATEMENTS AS ST
ON TR.CURRENT_STATEMENT_ID = ST.STATEMENT_ID
WHERE TR.CONNECTION_ID = CURRENT_CONNECTION

However, it seems conceptually wrong to me, to have a view depend on its calling statement. Maybe explain what you want to achieve and someone can suggest a better solution?

djk
  • 943
  • 2
  • 9
  • 27
  • Really more of an experiment... if certain columns are used in the select string it triggers another query... – JRhino Apr 25 '17 at 14:19