Showing posts with label SQL server Basic command. Show all posts
Showing posts with label SQL server Basic command. Show all posts

Monday, November 17, 2014

SQL Server : Important Commands for SQL Server DBA



--To check for Blocked Processes:
Sp_who2   
--To find all the log files and the percentage space used of those log files in an instance:
dbcc sqlperf (logspace)  
--To list all the files and their details in a particular database:
exec sp_helpfile
--To list all the databases and their details in a instance:
exec sp_helpdb
--To shrink a database file without specifying target size(preferred for log files):
dbcc shrinkfile(fileid)
--To shrink a database file with specifying target size(preferred for datafiles):
dbcc shrinkfile(fileid,target_size)
--To find the details about locks currently held by the processes:
exec sp_lock
--To find all the drive spaces through MS SQL:
EXEC master..xp_fixeddrives
--To put the database in single user mode at command prompt:
sqlservr.exe –m
--To put the database in Minimal mode at command prompt:
sqlservr.exe –f         -->  -c  for console applications
--To find out the Database ID?
 Select db_id ('db_name')
--To find out logins:
select * from sys.syslogins
--To find out current user:
Select current_user
--Short cut for MS SQL Server Management studio:
sqlwb.exe
--To check the service pack at product level:
select serverproperty ('machinename')
SELECT  SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')
--To check the open transactions :
dbcc opentran('dbname')
--To find MS SQL Server product level details:
exec master..xp_msver
--To find the details of current processes running like percent completed:
select * from sys.dm_exec_requests
--To find the longest running queries
select * from sys.dm_exec_query_stats
--Command to find the free space and used space :
select * from dbo.sysfiles
--To check load in server and database and files:
select * from sys.dm_io_virtual_file_stats(database id,file id)
--To find the longest running queries
select * from sys.dm_exec_sql_text
--To find Orphan login:
sp_validatelogins
--To find orphaned users
EXEC sp_change_users_login 'Report'
--To map users to logins
exec sp_change_users_login @Action='update_one', @UserNamePattern='<database_user>', @LoginName='<login_name>'
--If you already have a login id and password for this user, fix it by doing:
exec sp_change_users_login 'Auto_Fix', 'user'
--To delete login:
exec sp_revokelogin 'USERX'
--To list all the objects owned by user id:
Select name from sysobjects where uid=user_id('user')
--To enable ‘show advanced options’ for server configuration options:
exec sp_configure 'show advanced options', 1
--To enable particlar server configuration option:
exec sp_configure 'option name', 1
--To find performnce related stastictics:
select * from sys.dm_os_performance_counters
--To find the information about indexes on tables and views.
select * from sys.dm_db_index_usage_stats
--To find out all of the users who are connected to the database server:
select * from sys.dm_exec_sessions
--To display the error log using the query:
xp_readerrorlog
--To display error log archive numbers and their dates:
exec sp_enumerrorlogs
--To view no. of traces running.
SELECT count(*) FROM :: fn_trace_getinfo(default) WHERE property = 5 and value = 1
--To find details about the traces which are running.
SELECT * FROM :: fn_trace_getinfo(default)
--To terminate a trace
EXEC sp_trace_setstatus 1, @status = 0 / @status=2
--To know the Space used bye Data,index in particular Database:
Sp_spaceused
--To know  the DB status of particular Database:
SELECT DATABASEPROPERTYEX('AdventureWorks', 'Status')
--To view jobs which failed at last run:
 sysjobactivity, sysjobschedules, msdb.dbo.sysjobservers, msdb.dbo.sysjobs
--To check pages of table :
Dbcc ind('dbname','tablename',-1)
--To check pages contents
dbcc trace on(3604)  dbcc page('dbname',fid,pid,1)              -- F:File  P:page
--To set maximum connections.
exec SP_Configure
--To find no.of connections.
SELECT COUNT(dbid) as TotalConnections FROM sys.sysprocesses WHERE dbid > 0
--If log file is full,To find out reason.
Select name,log_reuse_wait_desc  from sys.databases



Like and Share to SQL Integrity Blog

Saturday, December 29, 2012

SQL Server – Delete the duplicate record (data) form table


 It is very easy to delete duplicate record from table in sql server. SQL Server always stores each tupple (Row) as unique into the table.

To see duplicate record, we can use the count function with group by clause with having in the condition.

 To delete the record to Max function with NOT IN keyword.

Just execute and see how it work to delete duplicate record  into table.

USE tempdb
GO
CREATE TABLE Jainendra_TestTable (My_ID INT, Rank_Col VARCHAR(50))
Go
INSERT INTO Jainendra_TestTable (My_ID, Rank_Col)
SELECT 1, 'First'
UNION ALL
SELECT 2, 'No Rank'
UNION ALL
SELECT 3, 'Second'
UNION ALL
SELECT 4, 'Second'
UNION ALL
SELECT 5, 'Second'
UNION ALL
SELECT 6, 'Third'
UNION ALL
SELECT 7, 'Five'
UNION ALL
SELECT 8, 'Second'
UNION ALL
SELECT 9, 'Five'
UNION ALL
SELECT 10, 'Nine'
UNION ALL
SELECT 11, 'Third'
GO

-- See the inserted data in create table
SELECT *
FROM Jainendra_TestTable
GO

-- Now below query is detecting duplicate records into table

SELECT Rank_Col, COUNT(*) TotalCount

FROM Jainendra_TestTable GROUP BY Rank_Col

HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC

GO

-- Now below query is deleting the duplicate record into table

DELETE FROM Jainendra_TestTable
WHERE My_ID NOT IN
( SELECT MAX(My_ID) FROM Jainendra_TestTable GROUP BY Rank_Col)

GO

-- Selecting Data
SELECT *
FROM Jainendra_TestTable
GO
DROP TABLE Jainendra_TestTable

GO


If it is useful than please like and share it to other SQL Server learners

Like and Share to SQL Integrity Blog

Tuesday, August 28, 2012

SQL SERVER - What is DBMS


·        In DBMS no relationship concept
·        It supports Single User only
·        It treats Data as Files internally
·        It supports 3 rules of E.F.CODD out off 12 rules
·        It requires low Software and Hardware Requirements.

Like and Share to SQL Integrity Blog

SQL SERVER - What is basically RDBMS



RDBM is stand for Relational Database Management System, it is the basic of SQL and it provide the relation between table object which contain in the database object.

 RDBMS has provided referential integrity facility to SQL server to maintain the database.

 RDBMS follows 12 rules which were defined by E.F. Codd.

Like and Share to SQL Integrity Blog

Friday, August 17, 2012

SQL SERVER- What is Similarity and Difference between Truncate and Delete in SQL Server

Most Common question  in Interview purpose -

Similarity

      1. These both command will only used for delete data of the specified table.

Difference

1.      TRUNCATE is a DDL (data definition language) command whereas DELETE is a DML (data manipulation language) command. 

2.      We can’t execute a trigger in case of TRUNCATE whereas with DELETE command, we can execute a trigger.

3.      We can apply condition on DELETE whereas can’t apply on TRUNCATE.

4.      TRUNCATE remove the reference like Identity whereas DELETE isn’t.

5. DELETE Stmt delete record one by one whereas TRUNCATE remove all recods in one attempt. 

Like and Share to SQL Integrity Blog

Wednesday, January 25, 2012

Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server 2000/05/08

Hi ....
 It is very very simple any body can do it..... follow below post .... and enjoy with sql server
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values.
Create TestTable
USE TestData
GO
CREATE TABLE CSVTest
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO

Create CSV file in drive C: with name csvtest.txt with following content. The location of the file is C:\csvtest.txt
1,James,Smith,19750101
2,Meggie,Smith,19790122
3,Robert,Smith,20071101
4,Alex,Smith,20040202

Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
BULK
INSERT
CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM CSVTest
GO
--Drop the table to clean up database.
SELECT *
FROM CSVTest
GO


Like and Share to SQL Integrity Blog

Tuesday, January 24, 2012

How to Select Command use in sql server 2008


The Base of Relational Database Management System is a database object called table. Each database consists of one or more tables, which store the database’s data/information. Each table has its own unique name and consists of columns and rows.
The database table columns (called also table fields) have their own unique names and have a pre-defined data types. Table columns can have many attributes defining the column functionality (the column is a primary key, there is an index defined on the column, the column has certain default value, etc.).
While table columns describe the data types, the table rows contain the actual data for the columns.
Here is an example of a simple database table, containing Address data. The first row, listed in bold, contains the names of the table columns:
Database: Adventure Works
Schema: Person
Table: Address
Query : select * from Person.Address
Output:

So Friends Now that we’ve learned what is a database table, we can continue with our MSSQL  tutorial and learn how to manipulate the data within the database tables.

Like and Share to SQL Integrity Blog