Tuesday, July 22, 2014

How to get SQL Server IP Address in SQL Query?

Get IP Address of SQL Server using SQL Query:

I have a situation to get the SQL Server IP Address. My SQL Server was in different IP Address and my web application was in different address. Some time we need to get both SQL Client and SQL Server Local IP Address. The below Example will help me to get the both client and sql server IP’s.
@@SPID – This is the session ID of particular connection. Each connection have different session ID.

SELECT   client_net_address,local_net_address, @@SPID as SPID
FROM sys.dm_exec_connections
WHERE Session_id = @@SPID;

I hope the above SQL Query will help you to get the SQL Server IP Address.

If you want to learn more ablut @@SPID Please check the below link


Wednesday, March 26, 2014

How to Select/Display/List tables from MS SQL Server 2008 R2 ?

Some time we need to select list of user table in the Database to identify the table list. For that we have different SQL querys to get display the Table list. Here I am give some small code to display the user table in different SQL query language.

List out the Data Base Tables detail:

SELECT * FROM sys.Tables


SELECT sc.name +'.'+ ta.name TableName ,SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa  ON pa.OBJECT_ID = ta.OBJECT_ID
INNER JOIN sys.schemas sc ON ta.schema_id = sc.schema_id
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
GROUP BY sc.name,ta.name ORDER BY SUM(pa.rows) DESC

How to Select / Display / List Views in the MS SQL Server 2008 R2:


How to Select / Display / List Table Columns in the MS SQL Server 2008 R2:


Thursday, February 13, 2014

How to check if stored procedure exists or Not in SQL server 2008R2 ?

Drop and Create Stored Procedure in SQL before Creating any SP:

In my project I need to give some Stored Procedure to client system to run some bug fixing. The client environment already have the same SP name. So we can give directly ALTER SP query to execute in the SQL server. But that not a good practices. As a good developer need to think future also. What happen if new client dos not have the Stored Procedure in that system? It will give error message. So avoiding the Error Message we need to add little bit code before the SP.

Before creating SP or deleting any Stored Procedure we need to check if that SP already exist or not in the Database. If Exist DROP the procedure and recreate again. If not exist that SP means don’t Drop the SP just create new Stored Procedure. This is the Good way to Creating Script in the SQL server. Below example first I have checked that SP name already Exist in the DB or Not. If exist I will DROP the Procedure and after that I will create New SP. If Not exist that SP name means it will not run the Drop function.

Drop Stored Procedure if exist in SQL Server and Create New one:

--My_SP_Name - Stored Procedure Name

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[My_SP_Name]') AND type in (N'P', N'PC'))



I hope the above code will help you to make good SQL script wit out error free script running. This is not only for SPs, You can use same method for Create Table also.

Wednesday, January 1, 2014

How to Restart IIS in command line

          Some time we need to restart the IIS to certain configuration changes take effect in the IIS server. There are different methods available to reset or restart the IIS. The below example will help you to restart IIS in command line.

How to restart IIS in command line:
          From the Start menu, click Run and type iisreset . This is the simplest method to restart the IIS. The below list will help you to do other works in IIS.

To restart IIS using the IIS Restart command-line utility
1. From the Start menu, click Run.
2. In the Open box, type cmd, and click OK.
3. At the command prompt, type iisreset , and press ENTER.
The below examples for other way of managing IIS
iisreset /stop – Stop the IIS services
iisreset /start – Start the IIS services
IISreset /restart - Stop and then restart all Internet services.
iisreset /status - will show the current status of IIS
iisreset /noforce - will prevent the server from forcing close applications and process. This can cause IIS to reset slower but is more graceful.
iisreset /disable - this command disables IIS and prevents all iisreset calls from executing
iisreset /enable - to re-enable the IIS Admin service
iisreset /REBOOT - Reboot the computer.
iisreset /REBOOTONERROR - Reboot the computer if an error occurs when starting, stopping, or restarting Internet services.
iisreset /NOFORCE - Do not forcefully terminate Internet services if attempting to stop them gracefully fails.