Sql Server Interview Questions and Answers (1758) - Page 21

What is "Double Hop" in SQL Server ?

One computer connects to another computer to connect to a third computer, is called a double hop.
How to respond to a Full Transaction Log Error in SQL Server ?

Normally the Transaction Log full Err occurred with 9002 Error code.

1. Backing up and Truncating the log.
2. Freeing the transaction physical disk space so that the log can automatically grow.
3. Moving the log file to a different disk drive with sufficient space.
4. Increasing the size of a log file(Initial and Growth).
5. Completing or killing a long-running transaction.
Maximum How many Row(s) will be there in Sys.Indexes view for Each table in SQL Server 2008/2008 R2 ?

Normally, When we create a new table, One entry will be there in Sys.Indexes view as 'HEAP' the Index_ID is '0', If we create a CLUSTERED Index on that table then, The 'HEAP' will be replaced as 'CLUSTERED' the Index_ID is '1'.

When we create a NONCLUSTERED Index on remaining columns then the Index_ID will be increased as 2,3,4,5....1005. Normally, a table can have maximum 999 NONCLUSTERED INDEXES and 1 CLUSTERED INDEX, Totally a table can have 1000 INDEXES.

But, The Index_ID in Sys.Indexes will be 0 or 1 to 250 and 256 to 1005 (Totally 1000 Indexes/Entries in Sys.Indexes View for a table). Then what about the 251 to 255 (5 Sequence have been reserved for Index Internals).

Finally, An Index_id will be 0 or 1 to 250 and 256 to 1005 (Maximum 1000 Entries will be there in Sys.Indexes View for each table), Minimum 1 entry will be there as 'HEAP' or 'CLUSTERED'
What does the Group By clause mean when used in databases?

Group By clause is Used usually with Aggregate Functions (eg. sum, average) to group rows with same data together.
What do these keywords mean with respect to Sql Server? a) @@Identity b) @@Rowcount c) @@Error

a) @@Identity means newly inserted row of int identity column
b) @@Rowcount is the number of rows affected by last statement
c) @@Error means last error that has occurred during a Transact-SQL operation
What is Stored Procedure? What is the advantage of these?

Stored Procedure means a group of T-SQL statements stored under a name and executed as a single unit of work. A stored procedure can be called from another stored procedure, from a client application.

Advantages are given below:-

Fast Execution
Network Load Reduction.
Security.
What is error handling in stored procedures of SQL Server 2008?

In previous versions of SQL Server you would handle exceptions by checking the @@error global variable immediately after an INSERT, UPDATE or DELETE, and then perform some corrective action if @@error did not equal zero.

SQL Server 2005 provides structured exception handing through TRY CATCH block as other programming language like JAVA, C# etc.

Example:
BEGIN TRY 

RAISERROR ('A problem is raised', 16,1)
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() as ERROR_NUMBER,
ERROR_SEVERITY() as ERROR_SEVERITY,
ERROR_STATE() as ERROR_STATE,
ERROR_MESSAGE() as ERROR_MESSAGE
END CATCH


ERROR_NUMBER() returns the number of the errors.
ERROR_SEVERITY() returns the severity.
ERROR_STATE() returns the error state number.
ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.
ERROR_LINE() returns the line number inside the routine that caused the error.
ERROR_MESSAGE() returns the complete text of the error message. The text includes the values supplied for any substitutable parameters, such as lengths, object names and times etc.
Describe how you can optimize stored procedures in SQL Server?

Below are some points to optimize stored procedure in SQL Server

• Use as much as possible WHERE clause filters. Where Clause is the most important part for optimization.

• Select only those fields which really required.

• Joins are expensive in terms of time. Make sure that use all the keys that relate to the tables together and don't join to the unused tables, always try to join on indexed fields. The join type is important as well in (INNER, OUTER).
Describe what is trigger in SQL Server?

In any database including SQL Server a trigger is a procedure that initiates on INSERT, DELETE or UPDATE actions.

Before SQL Server 2000 Triggers are also used to maintain the referential integrity. We can not execute triggers explicitly. The DBMS automatically fires the trigger when data modification events (INSERT, DELETE or UPDATE) happened in the associated table.

Triggers are same as stored procedures in terms of procedural logic that is stored at the database level. Stored procedures are executed explicitly and triggers are event-drive.
How can you describe RDBMS?

Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. With this the relationships are created and maintained across tables between data. Interdependencies between these tables are defined by the data values.
I could not see the Maintenance Plans when I login into my LoginID, But its showing in some other LoginId, Why ?

Your LoginID should be a member of SysAdmin role.

Object Explorer only displays the Maintenance Plans node for users who are members of the sysadmin fixed server role.
SELECT CASE SysAdmin WHEN 1 THEN 'You are a member of ''Sysadmin'' role' ELSE 'You are not member of ''Sysadmin'' role' END 'Sysadmin Role Permission' FROM 

(
SELECT IS_SRVROLEMEMBER(N'sysadmin') [SysAdmin]
)AS S

How to run Maintenence Plan using script ?

DECLARE @PlanID  VARCHAR(36)


SELECT @PlanID = id FROM msdb.dbo.sysmaintplan_plans
WHERE [NAME] ='Maintenance Plan Name'

EXECUTE msdb..sp_maintplan_start @PlanID,NULL
GO

How to execute a SQL Job using script ?

DECLARE @JobID	VARCHAR(36), 

@retval INT

SELECT @JobID = Job_ID FROM msdb.dbo.sysjobs_view
WHERE [Name] = 'SQL Job Name'

EXEC @retval = msdb.dbo.sp_start_job @job_id = @JobID
GO

How to identify the SQL Server Start/Restart Date & Time ?

1. When was the SQL Server Service Started ?
SELECT sqlserver_start_time 'SQL Server Started at' FROM sys.dm_os_sys_info

GO
2. When was the TempDB database Re-Created ?
SELECT create_date 'SQL Server Started at' FROM sys.databases WHERE [name] ='tempdb'

GO
Both the query have some difference on it's Time, Because, The Tempdb database will be Re-created after the SQL Server service started.
How to identify the Total/Available/Used Physical Memory in database server using script ?

SELECT (total_physical_memory_kb/1024.)/1024. 'Total Physical Memory(GB)', 

(available_physical_memory_kb/1024.)/1024. 'Available Physical Memory(GB)',
(100 / ((total_physical_memory_kb/1024.)/1024.)) * (((total_physical_memory_kb/1024.)/1024.) - ((available_physical_memory_kb/1024.)/1024.)) 'Used Physical Memory(%)' ,
(100 / ((total_physical_memory_kb/1024.)/1024.)) * ((available_physical_memory_kb/1024.)/1024.) 'Available Physical Memory(%)',
system_memory_state_desc 'Memory Status'
FROM sys.dm_os_sys_memory

Which SQL Server Profiler event enables to trace the users involved in Deadlock cycle ?

NOTE: This is objective type question, Please click question title for correct answer.
How do you install SQL Server to take advantage of Microsoft Server Clustering ?

NOTE: This is objective type question, Please click question title for correct answer.
What are the various options to move the data/databases ?

We have lots of options, we have to choose our option depending upon our requirements.

1.BACKUP/RESTORE
2.Dettaching, Moving the files and attaching databases
3.Replication
4.Mirroring
5.Logshipping
6.Implementing Linked Server and accessing the data using four / three part naning convention
7.DTS or DTSX
8.BCP
9.INSERT…SELECT or SELECT…INTO
10. Creating INSERT scripts to generate data.
What are the restrictions apply to compressed backups ?

The compressed backup was introduced in SQL Server 2008 Enterprise.So this technique applicable in SQL Server 2008 Enterprise Edition and Later.

The following are the compressed backup restrictions :
1. Compressed and uncompressed backups cannot co-exist in a media set.
2. Previous versions of SQL Server cannot read compressed backups.
3. NTbackups cannot share a tape with compressed SQL Server backups.
How to enable compressed backup ?

Compressed backup was introduced in SQL Server 2008 Enterprises edition and later only.
EXEC sys.sp_configure N'backup compression default', N'1'

GO
RECONFIGURE WITH OVERRIDE
GO
Once you enable the compressed backup, When you perform the backup next time, The backup will be compressed by default.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories