SQL statement that can obtain the IP address of the client

Source: Internet
Author: User
Use SQL statements to obtain the Client IP Address Code

Use SQL statements to obtain the Client IP Address Code

-- 1: Obtain the IP address of the client.
/************* IP **************/
Declare @ ip varchar (20), @ hst varchar (20), @ SQL varchar (100)
Declare @ str varchar (100)
Set @ str = 'ping' + Host_Name ()
Create table # tmp (aa varchar (200 ))
Insert # tmp exec master .. xp_mongoshell @ str
Select top 1 @ ip = replace (left (aa, charindex (':', aa)-1), 'reply from ','')
From # tmp where aa like 'reply from %: %'
Drop table # tmp
Select @ ip


-- 2: Obtain the physical address of the NIC.
Create table # tb (re varchar (255 ))
Insert into # tb exec master .. xp_mongoshell 'ipconfig/all'

Select Nic Physical Address = substring (re, charindex (':', re) + 1,255) from # tb where re like '% Physical Address .........: %'

Drop table # tb
Go


-- 3: convert an IP address segment into three segments separated by the DoT number
Create function getIP (@ a varchar (15 ))
Returns varchar (15)
As
Begin
Declare @ s varchar (15)
Set @ s =''
While charindex ('.', @ a)> 0
Begin
Set @ s = @ s + right ('000' + left (@ a, charindex ('.', @ a), 4)
Set @ a = right (@ a, len (@ a)-charindex ('.', @ ))
End
Set @ s = @ s + right ('000' + @ a, 3)
Return @ s
End

/*
Select dbo. getIP ('192. 1.110.2 ')
---------------
202.001.110.002

(The number of affected rows is 1)
*/
-- Drop function getIP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.