European Windows 2012 Hosting BLOG

BLOG about Windows 2012 Hosting and SQL 2012 Hosting - Dedicated to European Windows Hosting Customer

SQL Server Hosting - HostForLIFE.eu :: How to Use Visual Studio 2010 as a SQL Server?

clock March 29, 2016 23:29 by author Anthony

In this article I will discuss about how to use Visual Studio 2010 as a SQL Server database management studio. If you want to create applications using the .NET Framework you certainly will need Microsoft Visual Studio as IDE to build your application. If you have installed SQL Server course you will need a SQL Server Management Studio as your medium to process and manipulate data.


What if you've already installed Microsoft Visual Studio but when installing Microsoft SQL Server does not install the SQL Server Management Studio?

Of course you can not query your data right, and you can only pass the command prompt. But with Microsoft Visual SQL Management Studio as the media, you can process the data in the Microsoft Visual Studio IDE

  1. Run Microsoft Visual Studio, and then open Server Explorer listed on the menu view. After that will appear Data Connection and Server.
  1. Add a data connection by clicking the right mouse button and select Add Connection. Then see the database connection will appear.
  2. Enter your name server as the instance name on your SQL Server, log into SQL by using the username and password to get access, or by using Windows authentication. Then select the database you want to use and then press the Test Connection button, and make sure that the connection to the database successfully.

  3. If the Data Connection has been filled with database structure view you have, and then query the data from the database table and by right-clicking on the Data Connection that you created and select New Query.

  4. It will display the Query Window. Then select the table you want to create the query. But not only the table that can be in the query, Views, Function and Synonims you can also query the right to suit your needs.

  5. Once you select a table, the query functioned in accordance with the display data or you can write a query that you want on the bottom. To be able to execute the query, you can click the Execute button or by pressing the F5 key

HostForLIFE.eu SQL Server Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.



SQL Server 2014 Hosting - HostForLIFE.eu :: How to Remove Duplicate Data From String on SQL Server?

clock March 15, 2016 21:10 by author Peter

In this tutorial, let me show you how to remove data from string in which data are separated by delimiter. An identifier that complies with all the rules for the format of identifiers can be used with or without delimiters. An identifier that does not comply with the rules for the format of regular identifiers must always be delimited.

Delimited identifiers are used in these situations:

  • When reserved words are used for object names or portions of object names.

    It is recommended that reserved keywords not be used as object names. Databases upgraded from earlier versions of Microsoft® SQL Server™ may contain identifiers that include words not reserved in the earlier version, but are reserved words for SQL Server 2000. You can refer to the object using delimited identifiers until the name can be changed.

  • When using characters not listed as qualified identifiers.

    SQL Server allows any character in the current code page to be used in a delimited identifier; however, indiscriminate use of special characters in an object name may make SQL statements and scripts difficult to read and maintain.

You can use the following code:   

CREATE FUNCTION [dbo].[DistinctList] 
    ( 
    @List VARCHAR(MAX), 
    @Delim CHAR 
    ) 
    RETURNS 
    VARCHAR(MAX) 
    AS 
    BEGIN 
    DECLARE @Return_List [varchar](max); 
    DECLARE @Temp_Str [varchar](max); 
    DECLARE @Char_index int; 
    SET @List=@List+@Delim; 
    SET @Return_List=''; 
    SET @Char_index=CHARINDEX(@Delim,@List,1); 
    WHILE @Char_index>0 
    BEGIN 
    SET @Temp_Str=SUBSTRING(@List,1,@Char_index-1); 
    SET @Return_List=@Return_List+@Temp_Str+@Delim; 
    SET @List=REPLACE(@List,@Temp_Str+@Delim,''); 
    SET @Char_index=CHARINDEX(@Delim,@List,1); 
    END 
    Return SUBSTRING(@Return_List,1 ,LEN(@Return_List)-1); 
    END  


In this function first parameter take the string  and second parameter the delimiter ,on the behalf of this delimiter we split the string and remove the duplicate data.
    DECLARE @String [varchar](max); 
    SET @String='10,11,12,10,11'; 
    SELECT dbo.DistinctList(@String,',') AS List; 


And here is the output:
10,11,12

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



SQL Server 2014 Hosting - HostForLIFE.eu :: Make an Index on #temp tables on SQL Server

clock March 8, 2016 21:12 by author Peter

Today let me explain you about how to make an Index on #temp tables. #Temp tables are much like SQL tables that are defined and stored in TempDB. Difference between them and a permanent table is they are not allowed to have foreign keys.


One of the feature of temp table (#temp) is that we can add a clustered or non clustered index. Also, #temp tables allow for the auto-generated columns (fields) to be created. This can help the optimizer when determining the number of records. Below is an example of creating both a clustered and non-clustered index on a temp table.
-- creating temp table - #employees
CREATE TABLE #Employees 

ID INT IDENTITY(1,1), 
EmployeeID INT, 
EmployeeName VARCHAR(50) 


INSERT INTO #Employees 

EmployeeID, 
EmployeeName 

SELECT 
EmployeeID = e.EmployeeID 
,EmployeeName = e.EmployeeName 
FROM dbo.Employees e 
 
-- creating clustered index 
CREATE CLUSTERED INDEX IDX_C_Employees_EmployeeID ON #Employees(EmployeeID)
 
-- creating non-clustured index 
CREATE INDEX IDX_Users_UserName ON #Employees(EmployeeName) 

 

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



How To Collect Performance Metrics From SQL Server Query Analyzer?

clock March 1, 2016 20:37 by author Peter

In this topic, I will show you how to collect value of performance counters from query analyzer. Microsoft® SQL Server SQL Query Analyzer is a graphical tool that allows you to:

  • Create queries and other SQL scripts and execute them against SQL Server databases. (Query window)
  • Quickly create commonly used database objects from predefined scripts. (Templates)
  • Quickly copy existing database objects. (Object Browser scripting feature)
  • Execute stored procedures without knowing the parameters. (Object Browser procedure execution feature)
  • Debug stored procedures. (T-SQL Debugger)
  • Debug query performance problems. (Show Execution Plan, Show Server Trace, Show Client Statistics, Index Tuning Wizard)
  • Locate objects within databases (object search feature), or view and work with objects. (Object Browser)
  • Quickly insert, update, or delete rows in a table. (Open Table window)
  • Create keyboard shortcuts for frequently used queries. (custom query shortcuts feature)
  • Add frequently used commands to the Tools menu. (customized Tools menu feature)

The following code is demo query to do same. You can create collect any counter vale for instant. Now write the code:

/*************************************************/
USE Master
GO
Create Table Perfmon (Object_name Varchar (200),
Counter_name varchar (300) , Instance_name varchar (100),
cntr_value bigint, cntr_type bigint , date varchar (20))
GO
Insert into Perfmon
SELECT object_name , counter_name , instance_name , cntr_value, cntr_type
, convert (varchar (20),getdate () , 120) as date
FROM sys.dm_os_performance_counters
WHERE OBJECT_NAME in
(
'MSSQL$Servername:Databases' ,
'MSSQL$Servername:General Statistics',
'MSSQL$Servername:Buffer Manager' ,
'MSSQL$Servername:Locks'
)
AND counter_name in
(
'Transactions/sec' ,'User Connections','Page life expectancy',
'Buffer cache hit ratio', 'Buffer cache hit ratio base' ,
'Free pages','Total pages','Target pages','Lock Wait Time (ms)'
)
Select @@servername
GO
Select *, case
when cntr_type  = 65792  Then 'instant_value'
when cntr_type = 272696576 Then 'Incremental'
When cntr_type = 1073939712 Then 'Instant fraction'
When cntr_type = 537003264 Then 'Use this with Base value'
else 'null' end 
from Master.dbo.perfmon
/*************************************************/

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



SQL Server 2014 Hosting - HostForLIFE.eu :: String comparison and Collation in SQL Server

clock February 23, 2016 19:39 by author Peter

You probably already know that string comparison is dependent on the collation. but sometimes this dependency is slightly strange. I have installed an SQL Server with LATIN1_GENERAL_CI_AS as default collation (like is also the default setting during installation). Some weeks ago I came across a specific behavior in combination of this collation and the german language.

German is a stunning language! really. You may know that we have a tendency to Germans have some special vowels (the “Umlaut”: ä, ö, and ü) and also that extra ß which is often (but not always) simply interchangeable with “ss”. look at this query that uses the default collation for comparison:
select case
 when 'ss' collate latin1_general_ci_as
     = 'ß' collate latin1_general_ci_as
      then 'Yes'
 else 'No'
end [ss=ß?]

Considering that LATIN1_GENERAL_CI_AS is the default collation, the query above is identical to the following query:
select case
 when 'ss' = 'ß' then 'Yes'
 else 'No'
end [ss=ß?]

In both cases the result is Yes, so “ß” is considered as to be identical to “ss” when using the LATIN1_GENERAL_CI_AS collation. Now it's time to look at this query:

select case
 when 'ä' = 'ae' then 'Yes'
 else 'No'
end [ä=ae?]

This time the answer is no which is somewhat surprising, since one could consider the overall behavior inconsistent. In German, “ä” and “ae” (and also “ö” and “oe” in addition as “ü” and “ue”) are just as interchangeable as “ß” and “ss”. When this happened to me, I started browsing books online. There’s a hint, where you're advised setting the default collation to LATIN1_GENERAL_BIN for new installations. I didn’t know this so far, but ok: let’s retry our experiment:

select case
 when 'ss' collate latin1_general_bin
     = 'ß' collate latin1_general_bin
      then 'Yes'
 else 'No'
end [ss=ß?]

Now the answer is no. but considering the fact that most newer installations use LATIN1_GENERAL_CI_AS, simply because that’s the default during installation, I can’t just change the collation to LATIN1_GENERAL_BIN, since this will certainly produce other problems with queries spanning multiple databases with totally different collations. Not taking into account that changing the collation for an existing server and all of its databases/columns is a cumbersome and also very risky task on its own.

 

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



SQL Server 2014 Hosting - HostForLIFE.eu :: How to Return Value in SQL Server from EXEC Function ?

clock February 16, 2016 20:15 by author Peter

In this tutorial, I will show you how to Return Value in SQL Server  from EXEC Function. If you specify the OUTPUT keyword for a parameter in the procedure definition, the procedure can return the current value of the parameter to the calling program when the procedure exits. The following Stored Procedure is used which returns an Integer value 1 if the StudentId exists and 0 if the StudentId does not exists.

CREATE PROCEDURE CheckStudentId 
@StudentId INT 
AS 
BEGIN 
SET NOCOUNT ON; 
DECLARE @Exists INT 
IF EXISTS(SELECT StudentId FROM Students WHERE StudentId = @StudentId) 
BEGIN 
SET @Exists = 1 
END 
ELSE 
BEGIN 
SET @Exists = 0 
END 
RETURN @Exists 
END 

Returned value from EXEC function:
The returned integer value from the Stored Procedure, you need to make use of an Integer variable and use along with the EXEC command while executing the Stored Procedure.

    DECLARE @ReturnValue INT 
    EXEC @ReturnValue = < Store Procedure Name > < Parameters > Select @ReturnValue 
    Example: DECLARE @ReturnValue INT 
    EXEC @ReturnValue = CheckStudentId 34 
    SELECT @ReturnValue 

If There are valid StudentId then Output will be : 1

 

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



SQL Server 2014 Hosting - HostForLIFE.eu :: How To Check Available Column in A Table

clock January 5, 2016 21:50 by author Rebecca

In this tutorial, we will share to you how to check available colum in a table. If you want to check if the column is already available in the table, you can use system views like sys.columns or INFORMATION_SCHEMA.COLUMNS.

Step 1

First, let us create the dataset:

USE TEMPDB;
CREATE TABLE TESTING(ID INT, NAME VARCHAR(100))

Step 2

Suppose you want to find out the existence of the column named NAME and print a message. You can do it by using any of the following methods:

IF EXISTS
(
SELECT * FROM SYS.COLUMNS
WHERE NAME='NAME' AND OBJECT_ID=OBJECT_ID('TESTING')
)
PRINT 'COLUMN EXISTS'
--
IF EXISTS
(
SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME='NAME' AND TABLE_NAME='TESTING'
)
PRINT 'COLUMN EXISTS'

There is also a shorter way to do this. You can use COL_LENGTH system function:

IF (SELECT COL_LENGTH('TESTING','NAME')) IS NOT NULL
PRINT 'COLUMN EXISTS'

What it does is that it finds the length of the column. If it is null, the column does not exist in the table otherwise it exists.

Simple and fast, right?

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



SQL Server 2014 Hosting - HostForLIFE.eu :: How to Add Primary Key to Existing Table

clock December 8, 2015 23:28 by author Peter

I have an existing table referred to as Persion. in this table I actually have five columns:
persionId
Pname
PMid
Pdescription
Pamt

When I created this table, I set PersionId and Pname because the primary key. I now wish to include an extra column within the primary key - PMID. however can i write an ALTER statement to try and do this? (I already have 1000 records in the table). First, you can drop constraint and recreate it with the following code:
alter table Persion drop CONSTRAINT <constraint_name>
alter table Persion add primary key (persionId,Pname,PMID)


You can find the constraint name by using the code below:
select OBJECT_NAME(OBJECT_ID) AS NameofConstraint
FROM sys.objects
where OBJECT_NAME(parent_object_id)='Persion'
and type_desc LIKE '%CONSTRAINT'


I think something like this should work
-- drop current primary key constraint
ALTER TABLE dbo.persion
DROP CONSTRAINT PK_persionId;
GO

-- add new auto incremented field
ALTER TABLE dbo.persion
ADD pmid BIGINT IDENTITY;
GO

-- create new primary key constraint
ALTER TABLE dbo.persion
ADD CONSTRAINT PK_persionId PRIMARY KEY NONCLUSTERED (pmid, persionId);
GO

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



SQL Server 2014 Hosting - HostForLIFE.eu :: How to Check Last Accessed And Modified Table Data in SQL Server?

clock October 29, 2015 23:38 by author Peter

Now, I will guide you how to check last accessed and modified table data in SQL Data. And here is the script that I use:

/*************************************************/
SELECT OBJECT_NAME(OBJECT_ID) AS [Object_Name], last_user_update,last_user_scan  ,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'Access') AND OBJECT_ID=OBJECT_ID('test')
/*************************************************/

--Note: This will return null if sql server restarted and after that data is not accessed

         Demo:
/**** Create New Database ****/
Create Database Access
GO
use Access
GO
/**** Create Table ****/
Create Table test (id int , name varchar (100))
GO
/**** Insert New Record ****/
Insert into test values (1,'saurabh')
Insert into test values (2,'Sumit')
GO
/**** After insertion Check Stats ****/
SELECT OBJECT_NAME(OBJECT_ID) AS [Object_Name], last_user_update,last_user_scan  ,*
FROM sys.dm_db_index_usage_stats WHERE database_id = DB_ID( 'Access') AND OBJECT_ID=OBJECT_ID('test')

Object_Name   last_user_update                  last_user_scan
test               2015-10-29 08:39:54.100           NULL

Here last user scan is null because we are just inserted data, If we read data then we'll get read time also
So now we are going to read data and see if we get last user scan. Now, write the following code:

Select * from access.dbo.test
GO
/**** Checking Stats ****/
SELECT OBJECT_NAME(OBJECT_ID) AS [Object_Name], last_user_update,last_user_scan  ,*
FROM sys.dm_db_index_usage_stats WHERE database_id = DB_ID( 'Access') AND OBJECT_ID=OBJECT_ID('test')


So here we just updated table , didnt read. You can see the result on the following picture:

Similarly if we want to see how current indexes are performing on table we can see user scan , user seek or look ups.

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



SQL Server 2014 Hosting Tutorial - HostForLIFE.eu :: How to Detect Empty Row in A Table

clock October 28, 2015 03:14 by author Rebecca

Maybe you were recently doing a clean up of your website database, then you create some tables on your database but never adding any new rows to it. In this tutorial, I will tell you how to detect empty row in a table on SQL Server database.

Here's a simple query to list all empty rows in tables in your SQL Server database using a Dynamic Management View called dm_db_partition_stats. This will return page and row-count information for every partition in the current database:

;WITH EmptyRows AS
(
   SELECT SUM(row_count) AS [TotalRows],
          OBJECT_NAME(OBJECT_ID) AS TableName
   FROM sys.dm_db_partition_stats
   WHERE index_id = 0 OR index_id = 1
   GROUP BY OBJECT_ID
)
SELECT * FROM EmptyRows
WHERE [TotalRows] = 0

And here's the output:

Easy right?

HostForLIFE.eu SQL Server 2014 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in