you can rename a database in SQL Server 2005,2008 and 2012 by using SQL Server Management Studio or Transact-SQL.
Here i am describe all the ways to rename a Database .
Rename Database using Transact-SQL.:
Using SQL Server Management Studio To rename a Database:
1-In Object Explorer, right-click the database you want to rename and choose Design from the shortcut menu.
2-From the View menu, choose Properties.
3-In the field for the Name value in the Properties window, type a new name for the database.
4-To cancel this action, press the ESC key before leaving this field.
5-From the File menu choose Save name.
Here i am describe all the ways to rename a Database .
Rename Database using Transact-SQL.:
1-sp_renameDB 'oldDB','newDB'
2-ALTER DATABASE Test
MODIFY NAME =
NewTest
Using SQL Server Management Studio To rename a Database:
1-In Object Explorer, right-click the database you want to rename and choose Design from the shortcut menu.
2-From the View menu, choose Properties.
3-In the field for the Name value in the Properties window, type a new name for the database.
4-To cancel this action, press the ESC key before leaving this field.
5-From the File menu choose Save name.
In this article I am going to explain how you can find nth highest salary of the employee in SQL Server.
It is very common question asked by interviewer that how to find 2nd highest salary of employee or how to find 3rd highest salary of employee or how to find nth highest salary of employee in SQL Server. Here in this Sql Server Query Example i will Show you how to find 2nd,3rd,4th,.......,nth highest salary of employee.For this sql server query example i have created a Temporary Sql server Table in Sql Server Database named #EmpDetails .And enterd some values to this sql server table.The script is given below.
CREATE TABLE #EmpDetails(EMPID int,EMPNAME NVARCHAR(10),EMPSAL int)INSERT INTO #EmpDetails VALUES (1,'RAJ',2000)INSERT INTO #EmpDetails VALUES(2,'SURESH',12000)INSERT INTO #EmpDetails VALUES(3,'SURAJ',15000)INSERT INTO #EmpDetails VALUES(4,'SUNEEL',14000)INSERT INTO #EmpDetails VALUES(5,'SANJEEV',13000)INSERT INTO #EmpDetails VALUES(6,'ATUL',12070)INSERT INTO #EmpDetails VALUES(7,'VINEET',12600)INSERT INTO #EmpDetails VALUES(8,'VIVEK',12700)
Now Check the table for values.
select * from #EmpDetails
It will display The following Rows.
| EMPID | EMPNAME | EMPSAL |
|---|---|---|
| 1 | RAJ | 2000 |
| 2 | SURESH | 12000 |
| 3 | SURAJ | 15000 |
| 4 | SUNEEL | 14000 |
| 5 | SANJEEV | 13000 |
| 6 | ATUL | 12070 |
| 7 | VINEET | 12600 |
| 8 | VIVEK | 12700 |
Once table creation is completed now we will see different ways to get 2nd, 3rd, etc or nth highest salary of employee. The below sql query can be used to get 1st, 2nd, 3rd, 4th, 5th ….etc highest salary of employee.
SELECT TOP 1 EMPSAL FROM ( SELECT DISTINCT TOP n EMPSAL FROM #EmpDetails ORDER BY EMPSAL DESC) a ORDER BY EMPSAL
Replace n with 2 to find the second (2nd) highest salary of employee query will look like this .
SELECT TOP 1 EMPSAL FROM ( SELECT DISTINCT TOP 2 EMPSAL FROM #EmpDetails ORDER BY EMPSAL DESC) a ORDER BY EMPSAL
Result will be
EMPSAL
----------
14000
Replace n with 3 to find the third (3rd) highest salary of employee query will look like this .
SELECT TOP 1 EMPSAL FROM ( SELECT DISTINCT TOP 3 EMPSAL FROM #EmpDetails ORDER BY EMPSAL DESC) a ORDER BY EMPSAL
Result will be
EMPSAL
-------------
13000
This topic is about sql server Comma Separated Values(csv) from Table Column.
There are Many times where we need to convert a set of rows in a single column to a comma separated (CSV) string , in this article i am going to tell you how you can get csv values from sql server table column.
For this sql server query example i have created a Temporary Table in Sql Server Database.
CREATE TABLE #MyTable(Column1 NVARCHAR(10))
INSERT INTO #MyTable VALUES('sdf')INSERT INTO #MyTable VALUES('dfg')INSERT INTO #MyTable VALUES('ghj')INSERT INTO #MyTable VALUES('rty')INSERT INTO #MyTable VALUES('hjk')
Here i am showing Two methods to create Comma Separated Values(csv) from Table Column.
1-Create Comma Seperated Values(CSV) Using COALESCE Method
DECLARE @MakeStr VARCHAR(200)
SELECT @MakeStr = COALESCE(@MakeStr+',' ,'') + Column1
FROM #MyTable
SELECT @MakeStr as MyCSV
2-Create Comma Seperated String Using XML Method
DECLARE @MakeStr VARCHAR(200)
SELECT SUBSTRING(
(SELECT ',' + Column1
FROM #MyTable
ORDER BY Column1
FOR XML PATH('')),2,200) AS MyCSV
And the result for the above Sql Query Will be
MyCSV
——————–
sdf,dfg,ghj,rty,hjk
MyCSV
——————–
dfg,ghj,hjk,rty,sdf
And the result for the above Sql Query Will be
MyCSV
——————–
sdf,dfg,ghj,rty,hjk
MyCSV
——————–
dfg,ghj,hjk,rty,sdf
Here is a SQL Query to find the list of Functions in SQL Server database.When you run this query it will show the list of all functions in SQL Server DataBase.
SELECT name FROM sys.objects
WHERE type
IN ('AF ','FN', 'IF', 'TF', 'FS', 'FT')
The COUNT() function returns the number of rows that matches
a specified criteria.
Syntax for SQL COUNT(column)
The COUNT(columnname) function returns the total number
of Record of the specified column.In this case the NULL values will not be
counted.
SELECT COUNT(columnname) FROM tablename
Syntax
for SQL SQL COUNT(*)
The COUNT(*) function returns the total number of records in the
specified table:
SELECT COUNT(*) FROM tablename
Syntax for
SQL COUNT(DISTINCT
columnname)
The COUNT(DISTINCT
columnname) function returns the number of distinct values of the specified
column in a table:
SELECT COUNT(DISTINCT columnname) FROM tablename
Example: We have the following
"Orders" table:
| O_Id | Date | Price | Customer_Name |
|---|---|---|---|
| 1 | 2012/11/12 | 10000 | Rajesh |
| 2 | 2012/10/23 | 16000 | Shiv |
| 3 | 2012/09/02 | 7000 | rupa |
| 4 | 2012/09/03 | 3000 | Hansen |
| 5 | 2012/08/30 | 20000 | Jensen |
| 6 | 2012/10/04 | 1000 | Shiv |
Here we want to count the number of orders from
'Customer Shiv' the sql statement we use looks like this .
SELECT COUNT(Customer_name) FROM Orders
WHERE Customer='Shiv'
Result :2
if we use this statement:
SELECT COUNT(*) FROM Orders
Result:6
When SQL Server is installed
by default, the guest user is disabled for security reasons.
There are many ways to get
guest user status:
Using SQL Server Management
Studio
You can expand the database
node >> Security >> Users. If you see the RED arrow pointing
downward, it means that the guest user is disabled.
Using sys.sysusers
If you notice
column dbaccess as
1, it means that the guest user is enabled and has access to the
database.
SELECT name, hasdbaccess
FROM sys.sysusers
WHERE name = 'guest'
Using sys.database_principals and
sys.server_permissions
SELECT name, permission_name, state_desc
FROM sys.database_principals dp
INNER JOIN sys.server_permissions
sp
ON dp.principal_id = sp.grantee_principal_id
WHERE name = 'guest' AND
permission_name = 'CONNECT'
Using sp_helprotect
the following stored procedure will give you all
the permissions associated with the user.
sp_helprotect @username = 'guest'
Disable Guest
Account
REVOKE CONNECT FROM guest