Wednesday, December 28, 2016

SQLLOCALDB.exe

 
SQLLOCALDB is a utility to install the instance of SQL Server express LocalDB, it’s a command line tool which allow the developers to install and manage the instance of SQL Server express LocalDB
SqlLocalDB.exe have multiple arguments to pass i.e Create,Delete,Start, Stop etc.

Example:
sqlLocalDB.exe create "Organization" 
Above command will create the SQL server instance named Organization
 
for more details, please visit link

https://msdn.microsoft.com/en-us/library/hh212961.aspx

Monday, November 28, 2016

Tempdb Sizing

Many DBA’s Prefer to set 100-200 GB size for  database tempdb,  out of this space 90 % for data file(.mdf) and 10% for Log file(.ldf), this sizing help to prevent uncontrolled size growth & reduce contention.

Friday, October 14, 2016

MAX Value From Multiple Columns



CREATE TABLE #MAXCOLUMNVALUES
(ID INT IDENTITY(1,1)
,INFYSalary INT, WIPROSalary INT, SYNESalary INT, TCSSalary INT
)


INSERT INTO #MAXCOLUMNVALUES(InfySalary,WiproSalary,SyneSalary,TCSSalary) VALUES
(10000,20000,33000,25000),
(20000,55000,35000,45000),
(40000,24000,66000,50000),
(50000,51000,53000,77000),
(44000,32000,38000,30000)

SELECT * FROM #MAXCOLUMNVALUES
-- Select Max value from Multiple columns as column name MAXSalary

SELECT
*, (SELECT MAX(MaxSalary) FROM (VALUES (InfySalary),(WiproSalary),(SyneSalary),(TCSSalary)) AS Salary(MaxSalary)) AS MAXSalary
 FROM #MAXCOLUMNVALUES


Thursday, September 8, 2016

QUERY TO GET REVERSE DUPLICATE


Create Table with Reverse Duplicate Data

CREATE TABLE GETREVERSEDUPLICATE
(COL1 VARCHAR(10),COL2 VARCHAR(10) ,COL3 INT)

INSERT INTO GETREVERSEDUPLICATE
SELECT 'A','B',10
UNION
SELECT 'C','D',20
UNION
SELECT 'B','A',10
UNION
SELECT 'D','C',20



SELECT * FROM GETREVERSEDUPLICATE
OUTPUT:
COL1 COL2 COL3
A B 10
B A 10
C D 20
D C 20

GET REVERSE DUPLICATE WITH EXPECTED OUTPUT
EXPECTED RESULT:
COL1 COL2 COL3
B A 10
D C 20



Query:
SELECT * FROM GETREVERSEDUPLICATE T
 WHERE Col1 > Col2
       AND EXISTS (
                   SELECT *
                     FROM GETREVERSEDUPLICATE  AS T1
                    WHERE T1.Col1 = T.Col2
                          AND T1.Col2 = T.Col1
                  );

OUTPUT:
COL1 COL2 COL3
B A 10
D C 20


Please try in Test environment only.



Thursday, August 4, 2016

SQL Server Logins vs Database Users




SQL Server Login is used to Access SQL Server means for Authentication but Database users
are specific to a database to perform database operation based on database role membership and the permission under the tab securables means for Authorization.



After creation of login named SQLLogin as per above figure it will create the user in database Capital with user name SQLLogin with default schema [dbo]
SQL script for SQL Login as per above figure.
CREATE LOGIN [SQLLogin] WITH PASSWORD='random encrypted password', DEFAULT_DATABASE=[master],
DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO



We can see in above image SQLLogin is marked as user in database Capital thus it will
Create database user in database Capital using below SQL Script.

CREATE USER [SQLLogin] FOR LOGIN [SQLLogin] WITH DEFAULT_SCHEMA=[dbo]

Once SQL server login and respective database user is created we cannot delete the SQL Server logins if said SQL Login is used to access the SQL Server or database user is still associated with said SQL Login.
Database Role members.


We can create Database user with different types, shown in below figure



As per above image from securables tab we can give required permissions/authorization to database users for database objects (tables/view/sp’s etc.)
SQL Script to create user without SQL Login:
CREATE USER [sqlwologin] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]