Microsoft SQL Server FAQ's Part - IVQ1. Topic - CASTingFirst question of day: what is the len of @c?
declare @c varchar(8000)
set @c = N'hello' + replicate('-',8000)print len(@c)print @cA. 8000
B. 4000
C. 2000
Ans: B
Explanation: The CAST to NVARCHAR(4000) means that the maximum len is 4000, then the cast to varchar(8000) allows more characters, but the string is already truncated.
Ref: CAST and CONVERT - http://msdn2.microsoft.com/en-us/library/ms187928.aspxQ2. Topic - Database Background
What type of database is used on this site?
A. Oracle
B. MS SQL Server 2000
C. MySQL
D. MS SQL Server 2005
E. MS Access
Ans: D
Explanation: Of course the background of this site is MS SQL Server 2005! After running SQL Server 2000 for 6 years, the database was upgraded to SQL Server 2005 in September 2007.
Q3. Topic - Datetime conversion
You have a default SQL2005 SP2 install. What is the output from the following
set language us_english
set dateformat dmy
go
declare @date datetime
set @date = '11 apr 2008 17:10'
select left(@date,1)
A. 1
B. 4
C. A
D. An error 'The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.'
Ans: C
Explanation: The default conversion of a date type to character data is style 0, 'mon dd yyyy hh:miAM (or PM)', which gives us 'Apr 11 2008 5:10PM' and the left function returns the leftmost n characters, in this case 1 character, namely 'A'. Setting the DATEFORMAT to DMY has no impact on the outcome, as this setting is used only in the interpretation of character strings as they are converted to date values.
Ref:SET DATEFORMAT - http://msdn2.microsoft.com/en-us/library/ms189491(SQL.100).aspx Q4. Topic - SQL Server 2008 FILESTREAM
What is the purpose of FILESTREAM storage in SQL Server 2008?
A. To enable user direct access to database files
B. To store BLOB data on file system
C. To allow network storage of data and log files
Ans: B
Explanation: FILESTREAM storage allows user to create a table with a varbinary(max) column (BLOB) which is actually stored on the filesystem, rather than as a field in the row.
Ref: Designing and Implementing FILESTREAM - http://msdn2.microsoft.com/en-us/library/bb895234(SQL.100).aspxQ5. Topic - Installed Instances
Which registry key houses the values for the name of SQL Server 2005's installed instances?
A. HKLM\SOFTWARE\Microsoft\Microsoft SQL Native Client
B. HKLM\SOFTWARE\Microsoft\Microsoft SQL Server
C. HKLM\SOFTWARE\Microsoft\Microsoft SQL Server 2005 Redist
Ans: B
Explanation: The "InstalledInstance" Value is located in HKLM\SOFTWARE\Microsoft\Microsoft SQL Server.
Q6. Topic - Function execution
With the user defined function in SQL Server 2005:
CREATE FUNCTION [dbo].[fn_DoSomething](@Bin VARCHAR(10))
RETURNS VARCHAR(12)
AS
BEGIN
DECLARE @L AS INT
SET @L = LEN(LTRIM(RTRIM(@Bin)))
RETURN (@Bin)
END
GOWhat value would you expect to be returned when executing this code in SQL Server 2005?
SELECT dbo.fn_DoSomething('1234567890ABC')
A. An error message
B. 1234567890AB
C. 1234567890
D. 1234567890ABC
Ans: C
Explanation: Try it to see for yourself. It will return "1234567890" and this appears to be a glitch in SQL 2005 at compatibility level 90 and in SQL 2000.
Q7. Topic - Returning Data from Stored Proc
In SQL Server 2005, a strored procedure can return the data using: (select all that apply)
A. Output parameter
B. Return Code
C. Cursor data type in an output parameter
Ans: A, B, C
Explanation: The stored procedure can return the data using output parameters, a return code and as well as a cursor data type.
Ref: Designing Stored Procedures - http://msdn2.microsoft.com/en-us/library/ms191132.aspxQ8. Topic - Blocking
You have a default SQL2000 SP4 install.
select * from sysprocesses
reports SPID 55 as blocking SPID 55. You also notice that the waitime value is low and the waittype is a latch waittype
What is the most likely reason?
A. The code has entered into an infinite loop, blocking itself
B. As of SP4, sysprocesses reports latch waits and the behaviour is expected
C. Parallelism is enabled and the SPID is waiting for other threads of the SPID to finish
D. SPID 55 is reserved for CHECKPOINTS and is waiting for the next CHECKPOINT interval
Ans: B
Explanation: After you install Microsoft SQL Server 2000 Service Pack 4 (SP4), you may notice that the blocked column in the sysprocesses system table is populated for latch waits in addition to lock waits. Sometimes, you may notice brief periods of time when a single server process ID (SPID) is reported as blocking itself. This behavior is expected.
Ref: KB 906344 http://support.microsoft.com/default.aspx/kb/906344
Q9. Topic - Impersonation in an Execute As statement
The database user or SQL Server login is impersonated when the EXECUTE AS statement is executed or specified in a module. Which of the following statements are true about the impersonation? (Select all that apply)
A. Another EXECUTE AS statement or the REVERT statement must be used before the impersonation will end.
B. The database user or SQL Server login impersonation ends when the session is dropped or when the module finishes its execution.
C. If the statement is called by a member of sysadmin, server-level impersonation is used. If the statement is called by an account that is dbo, database-level impersonation is used.
D. The scope of the impersonation is explicitly defined
Ans: B, D
Explanation: Although using another Execute As statement or the revert statement will modify the impersonation it is not necessary to execute either statement to end the impersonation. The impersonation will end automatically when the session is dropped or the module completes its execution.
Ref: Understanding Context Switching -
http://msdn2.microsoft.com/en-us/library/ms191296.aspx The scope of the impersonation is explicitly defined in the Execute AS statement.
The SETUSER statement is implicit. http://msdn2.microsoft.com/en-us/library/ms188315.aspx
Q10. Topic - Query
What will happen if following query get executed:
select CategoryID, Quantity, Pricefrom Customer c, SalesOrderDetail sodwhere sod.clientid in (select customerid where pricingplan='X')
A. Query will return error because the From clause is missing in query used in where clause
B. Query will succeed if the customerid column is available in any table used in the first From Clause
C. Query is wrong
Ans: B
Explanation: If any attribute is available in the outer From Clause then it will validate column in that before returning any error. You can view this with the following code:
create table customer (ClientID int, customername varchar(10), pricingplan char(1))--create table customer (ClientID int, customername varchar(10), pricingplan char(1), CustomerID int) gocreate table SalesOrderDetail (ClientID int, CustomerName varchar(10), categoryid int, quantity int, price money, customerid int)goinsert customer select 1, 'Steve', 'X'insert customer select 2, 'Andy', 'Y'-- insert customer select 1, 'Steve', 'X', 1-- insert customer select 2, 'Andy', 'Y', 2insert SalesOrderDetail select 1, 'Steve', 1, 2, 10, 1insert SalesOrderDetail select 2, 'Andy', 1, 5, 10, 2select CategoryID, Quantity, Pricefrom Customer c, SalesOrderDetail sodwhere sod.clientid in (select customerid where pricingplan='X') drop table Customerdrop table SalesOrderDetail You can also remove CustomerID from both tables and see that it causes an error.
Q11. Topic - Delete Duplicates
Which of the three lines will correctly remove duplicated items in the following table:
Create table #new(ID int null, Keyvalue varchar(2))
insert into #new(id,keyvalue) values (1,'aa')
insert into #new(id,keyvalue) values (2,'bb')
insert into #new(id,keyvalue) values (1,'aa')
insert into #new(id,keyvalue) values (1,'aa')
A. with numbered;(SELECT rowno=row_number() over (partition by ID order by ID),ID,keyvalue from #new)delete from numbered where rowno>1
B. ;with numbered as(SELECT rowno=row_number() over (partition by ID order by ID),ID,keyvalue from #new)delete from numbered where rowno>1
C. ;with numbered as(SELECT rowno=row_number() over (partition by ID order by ID),ID,keyvalue from #new)delete from numbered where rowno=1
Ans: B
Explanation: Of these statements, the first one produces a syntax error. The last one produces a table with 2 duplicates. The second one is correct. A semi-colon at the beginning of the statement is valid and we want to delete all the matching row numbers greater than 1.
Q12. Topic - SQL Server 2008 FILESTREAM
How do you know whether FILESTREAM storage is available and enabled in SQL Server 2008?
A. Query Sys,Configurations view for 'FileStreamAccessLevel'
B. Query the Sys.assembly_files for 'FileStream'
C. Query the server property 'FilestreamEffectiveLevel'
Ans: C
Explanation: For knowing the current effective level of FileStream access you need to run the following query –
SELECT SERVERPROPERTY ('FilestreamShareName') ,SERVERPROPERTY ('FilestreamEffectiveLevel'); which will give you the instance name and the filestream access level. If the level is set to 3 the filestream storage is enabled and available.
Ref: sp_filestream_configure - http://msdn2.microsoft.com/en-us/library/bb934198(SQL.100).aspx Q13. Topic - Statistics
What would you use to update a single table's statistics in the shortest time?
A. sp_updatestats
B. UPDATE STATISTICS
C. All of the above
Ans: B
Explanation: Sp_updatestats does not have an option to update a single table's statistic. It will update all tables in the database. This answer is incorrect. UPDATE STATISTICS can be used with single table and therefore it will run in most cases faster.
Ref: UPDATE STATISTICS - http://msdn2.microsoft.com/en-us/library/aa260645(SQL.80).aspx Q14. Topic - SQL Server 2005
Which of the following are system databases in SQL Server 2005? (select all that apply)
A. Master
B. Model
C. Msdb
D. Tempdb
E. Resource
Ans: A, B, C, D, E
Explanation: All of these are system databases in SQL Server 2005.
Ref: System Databases - http://msdn2.microsoft.com/en-us/library/ms178028.aspx
Q15. Topic - Bit data
The AdventureWorks database has the HumanResources.Employee table with a column SalariedFlag defined as follows:
[SalariedFlag] [dbo].[Flag] NOT NULL
The Flag user-defined data type is defined by:
CREATE TYPE [dbo].[Flag] FROM [bit] NOT NULL
What will be the result when the following query is executed on SQL Server 2005?
USE AdventureWorksSELECT COUNT(*) as [Number of Salaried Employees]FROM HumanResources.EmployeeWHERE SalariedFlag = 'true'
A. A numeric result will be returned
B. An error message will appear: Syntax error converting the varchar value 'true' to a column of data type bit.
C. A result set with no value (a blank result) will be returned.
Ans: A
Explanation: In SQL Server 2005, the string values TRUE and FALSE can be converted to bit values.
See the SQL Server Books Online topic "bit (Transact-SQL)" at
http://msdn2.microsoft.com/en-us/library/ms177603.aspx.
In SQL Server 2000, a query such as: USE Northwind; SELECT * FROM Products WHERE discontinued = 'true'; would give the error message.
Q16. Topic - ANSI_NULLS
Assume the following table
CREATE TABLE testnull(ggg INT NULL)
INSERT testnull(ggg)SELECT NULL UNION ALLSELECT 1 UNION ALLSELECT 0
What counts do you get from the followingSELECT Count(*)
FROM testnull GOSET ANSI_NULLS OFFGOSELECT Count(*)
FROM testnull WHERE ggg=1
OR ggg!=1;
SELECT Count(*)
FROM testnull WHERE ggg=1
OR ggg!=1
OR ggg=NULL;GOSET ANSI_NULLS ONGOSELECT Count(*)
FROM testnull WHERE ggg=1
OR ggg!=1;
SELECT Count(*)
FROM testnull WHERE ggg=1
OR ggg!=1
OR ggg=NULLGODROP TABLE testnull
A. 3,3,3,2,2
B. 3,3,2,2,2
C. 3,2,3,2,2
D. 3,2,3,2,3
Ans: C
Explanation: Although Setting the ANSI_NULLS setting to OFF allows for using the =NULL notation, it does NOT affect how NULL operates with regards to comparisons with non-null values. the SET ANSI_NULLS OFF has been marked as deprecated in SQL 2005 and will be removed in the future. For more info on ANSI_NULLS, read the following Books Online document:
Ref: SET ANSI_NULLS-
http://msdn2.microsoft.com/en-us/library/ms188048.aspxQ17. Topic - Implicit Conversions
What happens with this code?select 'B'union select 4
A. The int is converted to varchar
B. The varchar is converted in int (error results)
C. The query does not compile.
Ans: B
Explanation: This is an example of an implicit conversion. In this case, an error is returned as the varchar is converted to an int, or an attempt is made, which returns an error. The int is of higher precedence than a varchar, so that is the order of conversions.
Ref: Connect and Implicit Casts - http://blogs.msdn.com/isaac/archive/2008/04/10/connect-and-implicit-casts.aspxData Type Precedence - http://msdn2.microsoft.com/en-us/library/ms190309(SQL.100).aspx Q18. Topic - T-SQL query
Is there a condition for the WHERE clause to display the details about the students born in the month numbers of 1[Jan], 3,4,5,7,8? The condition should not use both IN and OR operators
A. No
B. Yes
C. Not Supported in SQL Server
Ans: B
Explanation: We can give the condition as follows:where datename(m,dob) not like '%e%'
because these month(s) specified do not have the letter 'e', but all the other months have at least one 'e' in month name.
Q19. Topic - Rebuild Master
If you rebuild the master database in SQL Server 2005, what happens to your version level?
A. It reverts to the RTM version, losing service packs and additonal patches
B. It reverts to the latest Service Pack version, losing any additional patches.
C. It remains the same.
Ans: A
Explanation: When you rebuild the master database, all system databases, including the systemresource database, are rebuilt from original media, so all Service Pack, Hotfix, QFE/GDR information is lost and you revert to the original RTM version.
Ref: How to: Install SQL Server 2005 from the Command Prompt (scroll down to rebuild database section) - http://msdn2.microsoft.com/en-us/library/ms144259.aspx Q20. Topic - DAC Port
On which port can you make a Dedicated Administrator Connection (DAC)?
A. 1435
B. 3389
C. 1433
D. Dynamically assigned
Ans: D
Explanation: The DAC port is dynamically assigned when the instance starts and listed in the SQL Server error log. By default it tries for 1434, but it is possible that this port is already in use. The message will be something like:Dedicated admin connection support was established for listening locally on port 1977.
Ref: Using a Dedicated Administrator Connection - http://msdn2.microsoft.com/en-us/library/ms189595.aspx Q21. Topic - Set trigger firing order
I have 3 UPDATE triggers on a individual table, TRA, TRB and TRC. Can I fire them in the sequence TRA, TRB and TRC?
A. No
B. Yes
Ans: B
Explanation: http://msdn2.microsoft.com:80/en-us/library/ms186762.aspx Using sp_settriggerorder Set TRA as first, TRC as LAST and TRB will fire between TRA and TRC firing which is what is desired. Q22. Topic - Identity Columns
In SQL Server 2005, how can you easily determine which columns have the identity property set? (select all that apply).
A. Query sys.identity_columns for the rows.
B. Query sys.tables.identity_column for the name of the column in each table
C. Query sys.columns.is_identity for a value of 1
D. There is no way to do this in T-SQL.
Ans: A, C
Explanation: There is a table, sys.identity_columns that contains a row for each column in your database that has the identity property set. There is also a column called is_identity in the sys.columns view that contains a 1 if the column has the identity property set. The other answer was made up.
Ref: sys.identity_columns - http://msdn2.microsoft.com/en-us/library/ms187334.aspx
Q23. Topic - Floor Function
What is the output of this query?select floor(13.890)
A. 14
B. 13.9
C. 13
Ans: C
Explanation: Floor returns the largest integer less than or equal to the specified numeric expression. In this case, that is 13.
Q24.Topic - SQL Server Express DAC
Does SQL Server 2005 Express allow Dedicated Administrator Connections (DAC)?
A. Yes, always
B. Yes, if remote connections are allowed.
C. Yes, if started with trace flag 7806
D. No
Ans: C
Explanation: SQL Server 2005 Express Edition does not listen on the DAC port by default. If it is started with trace flag 7806, then DAC connections can be made.
Ref: Using a Dedicated Administrator Connection -
http://msdn2.microsoft.com/en-us/library/ms189595.aspxQ25. Topic - Named Pipes
In SQL Server 2005, how do you perform a backup using Named Pipes?
A. Normal backups to local disks all use named pipes
B. In the TO part of the backup command, specify the pipe as "TO PIPE = '\sql\pipe'"
C. SQL Server 2005 does not support backup to named pipes.
Ans: C
Explanation: In SQL Server 2005, the ability to backup to a named pipe (disk or tape) was removed.
Ref: Discontinued Database Engine Functionality in SQL Server 2005 - http://msdn2.microsoft.com/en-us/library/ms144262.aspx