Bob, maybe it would help to see more of your query. It seems as if you are trying to execute your kentico macro inside of sql server, which won't work, the way you have that written. The macro is going to evaluate before it executes the sql, which is why you are seeing it pass the current document into the sql.
Maybe if I understood what you were trying to achieve, I could provide more assistance.