SQL Server – How to Set HOST_NAME() Value in Stored Procedure

sql serversql-server-2008

We have existing triggers which read the workstation id property of the connection string using host_name(). There are existing PowerBuilder client applications connecting to the database directly and this setting was used to pass the identity of the current user to the application (not my design).

We have newer web applications updating the database, and the triggers require the user identity information to be present in host_name. However, we don't want to set the user id in the connection string due to the affect on connection pooling (connections are not pooled across all users).

We pass the identity of the user via stored procedure parameters and would like to somehow set the host_name in the stored procedure such that the trigger requirements are satisfied.

How might this be accomplished, if at all?

Best Answer

As far as I'm aware, you can't manipulate HOST_NAME() inside of SQL Server. And I think that's a good thing. The fact that you can spoof this in the connection string is something I've long considered a security problem waiting to happen.

Instead, you will need to change the logic in your triggers. For example you could store HOST_NAME() in a variable, then check PROGRAM_NAME() - if it's your PowerBuilder app, overwrite the variable with whatever, and then the trigger logic can check the variable instead of HOST_NAME().

(You can do that in fewer steps, obviously; just illustrating.)