Execute SQL Server queries online

How to execute sql server queries online?

You do not want to install SQL Server On your local system and wanted to execute SQL Server t-sql queries.
In such a situation you can easily execute TSQL Code Online using the below link.

http://sqlzoo.net/howto/source/z.dir/tip106453/sqlserver

START STOP RESTART SQL Server 2008 from command prompt

Following commands can be used to start stop SQL Server service from command prompt.
Note: Run Command prompt as Administrator

Start SQL Server 2008

[sourcecode language=”plain”]net start mssqlserver[/sourcecode]

Start SQL Server 2008 - mssqlserver service from Command Prompt

Stop SQL Server 2008

[sourcecode language=”plain”]net stop mssqlserver[/sourcecode]

STOP SQL Server 2008 - mssqlserver service from Command Prompt

Restart SQL Server 2008

[sourcecode language=”plain”]
net stop mssqlserver
net start mssqlserver
[/sourcecode]

Advanced Topics:
If you want to start/Stop a specific sql server instance replace mssqlserver with the name of the instance specific service[sourcecode language=”plain”]
net stop <<sqlserverservicename>>
net start <<sqlserverservicename>>
[/sourcecode]

For SQL Server Express the name would be MSSQL$SQLEXPRESS
[sourcecode language=”plain”]
net stop MSSQL$SQLEXPRESS
net start MSSQL$SQLEXPRESS
[/sourcecode]

How to find the service name?
1. Open Services.msc (open Run (windowskey+r) then type services.msc and click on ok)
2. In the read through the services(or click S to find all services starting with s)
3. Select a SQL Server service and doubleclick on it(or rightcick-properties)
4. In the popup you will have service name
5. Note down the service name (This is the name of the service – Display name is used for readability)

SQL Server Service Pop up

Following are the few of SQL Server related service names:

Display Name Service Name
SQL Server (MSSQLSERVER) MSSQLSERVER
SQL Server Agent (MSSQLSERVER) SQLSERVERAGENT
SQL Server Analysis Services (MSSQLSERVER) MSSQLServerOLAPService
SQL Server Browser SQLBrowser
SQL Server Integration Services 10.0 MsDtsServer100
SQL Server Reporting Services (MSSQLSERVER) ReportServer
SQL Server VSS Writer SQLWriter SQLWriter

Following are the few of SQL Server instance specific service names:

Display Name Service Name
SQL Server (SQLEXPRESS MSSQL$SQLEXPRESS
SQL Server Agent (SQLEXPRESS) SQLAgent$SQLEXPRESS

Additional Notes:
If you try to start the SQL Server service which is already started you get the below error:

The requested service has already been started.
More help is available by typing NET HELPMSG 2182.

If you try to stop the SQL Server service which is not yet started you get the below error:

The SQL Server (MSSQLSERVER) service is not started.
More help is available by typing NET HELPMSG 3521.

If you try to start or stop SQL Server service when you do not have permissions you get the following error:

System error 5 has occurred.
Access is denied.

Resolutions:
1- Try to open command prompt as Administrator if it is not running in Admin mode.
2- Try to get Admin permissions on the Server.

If you try to start or stop SQL Server service when you do not have service installed you get the following error:

The service name is invalid.
More help is available by typing NET HELPMSG 2185.

Resolutions:
1. Check if SQL Server is installed on the server
2. Try to open command prompt as Administrator if it is not running in Admin mode.
3. Try to get Admin permissions on the Server.
2- Try to get Admin permissions on the Server.

Unable to start/stop any SQL Server sevice:
1. Check if the service name is correct.
You can use services.msc or use the command
[sourcecode language=”plain”]
sc query | findstr /I “MSSQL”
[/sourcecode]
2. Check if the Command prompt is in Admin mode
3. Check SQL Server error logs for additional details

TSQL Code Beautifier

Following link helps you format and beautify SQL Code: http://sqlserverlearner.com/online-tools/tsql-code-formatter

SHRINK Database

Following are the Steps to SHRINK Database in SQL Server.

1) Connect to the SQL Server in SSMS

2) Expand Databases

3) RightClick on the Database to be shrinked, Click on Tasks then on Shrink.

4) Here you will have two options:

Shrink Database
Shrink Files

Using Database option you can shrink the database as a whole
Files option lets you shrink each file associated with the database seperately.

Shrink Database
Shrink Database

5) Select Database

6) Now in the Shrink Database Popup Click OK

Shrink Database Popup
Shrink Database Popup

SQL Server External Tools

SQL Server Management Studio provides a functionality to add external tools(Windows Bases or .Net Based).
This will be in great use when an external executable file has to be run from SSMS.

How to add external tools to SQL Server Management Studio?

The step by step approach to add External tools to SSMS is given below. (Screenshots depict how Notepad has to be added as external tool to SSMS)

Step1:

Open SSSMS

Step2:

Click On Tools Menu Item. Select External Tools… Option

SSMS External Tools Option

Now External Tools Dialog Box pops up. Using this Dialog Box External tools can be added to SSMS.

External Tools Dialog Box
External Tools Dialog Box

Step3:

Now select the command for the external tool by clicking on the browse button.
The below screenshot shows notepad being selected as command from location C:\Windows\System32\notepad.exe

Select Notepad for external tools command
Notepad Selected as External Tools Command

Update the Title with the title you want for this extenal tool(basically the name of the external tool).
If the external tool has arguments then add them in the arguments section.
Also add the intial directory if initial execution directory is needed by the external tool.

For adding Notepad as an external tool there is no mandate to add Arguments and Initial directory.
The tool Title is given as ExternalToolNotepad.

Now click on apply, Then click on OK.

You can find the added external tool in the Tools Menu item.

Using newly added external tool
Using newly added external tool

Now when the external tool is clicked in the tools menu, the tool opens up with the defined options in External tools Dialog.

How to add toolbar Button for External tool?

External tools can be easyly accessed when a toolbar button is added to external tool.
This can be acheived using Customize Option in the tools menu.

Customize External tools
Now Customize dialog box pops up
Select Tools under Catagories in the commands tab.
Drag the External Command1 item in the Commands section to the location on SSMS where you want the button to be added.

Select Tools and External Command1

The cursor initlly has a x mark. As you drag the item the Cursor changes into a + mark indicating it can be added at that location. Leave the cursor at the location where external tool button is needed.

External Tool Added To ToolBar
External Tool Button Added To ToolBar

Using this method External tool can be added to the tool bar, or as a menu item.