patternsqlMinor
Is there a way to tell your hostname within a stored procedure?
Viewed 0 times
storedyourwayproceduretellwithinhostnamethere
Problem
Is there a way to determine the hostname of the server you're running in w/ in a stored procedure?
You can run system hostname from a client but you can't make system calls from SP. I don't see it any of the global variables or statuses.
You can run system hostname from a client but you can't make system calls from SP. I don't see it any of the global variables or statuses.
Solution
I can see my hostname on my PC
This works in MySQL 5.x
The query works directly in the mysql client:
Here is quick SP I wrote in MySQL 5.5.12 on my PC
Then run
Here it is as a function
Now, just SELECT it:
Give it a Try !!!
mysql> show variables like 'hostname';
+---------------+--------------+
| Variable_name | Value |
+---------------+--------------+
| hostname | LW-REDWARDS2 |
+---------------+--------------+
1 row in set (0.00 sec)
mysql>This works in MySQL 5.x
SELECT variable_value INTO @local_hostname
FROM information_schema.global_variables
WHERE variable_name = 'hostname';The query works directly in the mysql client:
mysql> SELECT variable_value INTO @local_hostname
-> FROM information_schema.global_variables
-> WHERE variable_name = 'hostname';
Query OK, 1 row affected (0.00 sec)
mysql> select @local_hostname;
+-----------------+
| @local_hostname |
+-----------------+
| LW-REDWARDS2 |
+-----------------+
1 row in set (0.00 sec)
mysql>Here is quick SP I wrote in MySQL 5.5.12 on my PC
DELIMITER $
DROP PROCEDURE IF EXISTS `junk`.`ShowHostName` $
CREATE PROCEDURE `junk`.`ShowHostName` ()
BEGIN
SELECT variable_value INTO @local_hostname
FROM information_schema.global_variables
WHERE variable_name = 'hostname';
SELECT @local_hostname;
END $
DELIMITER ;Then run
CALL junk.ShowHostName();mysql> CALL junk.ShowHostName();
+-----------------+
| @local_hostname |
+-----------------+
| LW-REDWARDS2 |
+-----------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql>Here it is as a function
DELIMITER $
DROP FUNCTION IF EXISTS `junk`.`GetHostName` $
CREATE FUNCTION `junk`.`GetHostName` () RETURNS VARCHAR(64)
DETERMINISTIC
BEGIN
DECLARE local_hostname VARCHAR(64);
SELECT variable_value INTO local_hostname
FROM information_schema.global_variables
WHERE variable_name = 'hostname';
RETURN local_hostname;
END $
DELIMITER ;Now, just SELECT it:
mysql> SELECT junk.GetHostName();
+--------------------+
| junk.GetHostName() |
+--------------------+
| LW-REDWARDS2 |
+--------------------+
1 row in set (0.00 sec)
mysql>Give it a Try !!!
Code Snippets
mysql> show variables like 'hostname';
+---------------+--------------+
| Variable_name | Value |
+---------------+--------------+
| hostname | LW-REDWARDS2 |
+---------------+--------------+
1 row in set (0.00 sec)
mysql>SELECT variable_value INTO @local_hostname
FROM information_schema.global_variables
WHERE variable_name = 'hostname';mysql> SELECT variable_value INTO @local_hostname
-> FROM information_schema.global_variables
-> WHERE variable_name = 'hostname';
Query OK, 1 row affected (0.00 sec)
mysql> select @local_hostname;
+-----------------+
| @local_hostname |
+-----------------+
| LW-REDWARDS2 |
+-----------------+
1 row in set (0.00 sec)
mysql>DELIMITER $$
DROP PROCEDURE IF EXISTS `junk`.`ShowHostName` $$
CREATE PROCEDURE `junk`.`ShowHostName` ()
BEGIN
SELECT variable_value INTO @local_hostname
FROM information_schema.global_variables
WHERE variable_name = 'hostname';
SELECT @local_hostname;
END $$
DELIMITER ;mysql> CALL junk.ShowHostName();
+-----------------+
| @local_hostname |
+-----------------+
| LW-REDWARDS2 |
+-----------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql>Context
StackExchange Database Administrators Q#10841, answer score: 7
Revisions (0)
No revisions yet.