Escolar Documentos
Profissional Documentos
Cultura Documentos
createtable Mytrantest
(
OrderId intPrimarykeyIdentity
Begintran Transtart
Insertinto Mytrantest
defaultvalues
Savetran firstpoint
insertinto Mytrantest
defaultvalues
rollbacktran firstpoint
Committran transtart
Select*from Mytrantest
Answer - 1
Question2. You are creating a stored procedure that will be called by an application and
will access data stored in one of the databases. The stored procedures must be
configured to return the current value of a parameter to the calling application. What
must you do?
A. Specify an output parameter in the stored procedure using the OUTPUT keyword.
B. Include a select statement in the stored procedure to return the necessary value
C. Specify a return code for the stored procedure using the RETURN statement.
D. Declare a variable using the DECLARE statement
Answer - A
Question3. You have created a view that reference columns from multiple base tables.
You want to modify data in the underlying tables through the view using an UPDATE
statement. What must be done to allow this modification to occur?
Answer - C
A. Read Uncommited
B. Read Commited
C. Repeatable Read
D. Serializable
Answer - B
Scenario Based Question: -There are two tables (User&UserHistory) as shown below:-
User
user_id
name
phone_num
UserHistory
user_id
date
action
Question5.Given the tables above, write a SQL query to determine which user_ids in the User
table are not contained in the UserHistory table (assume the UserHistory table has a subset of
the user_ids in User table).
Answer.select u.user_id
from User as u
left join UserHistory as uh on u.user_id=uh.user_id
where uh.user_id is null
createtable Mytrantest
(
OrderId intPrimarykeyIdentity
)
Begintran Transtart
Insertinto Mytrantest
defaultvalues
Savetran firstpoint
insertinto Mytrantest
defaultvalues
rollback
Select*from Mytrantest
Salesperson Customer
ID Name Age Salary ID Name City Industry Type
1 Abe 61 140000 4 Samsonic pleasant J
2 Bob 34 44000 6 Panasung oaktown J
5 Chris 34 40000 7 Samony jackson B
7 Dan 41 52000 9 Orange Jackson B
8 Ken 57 115000
11 Joe 38 38000
Orders
Number order_date cust_id salesperson_id Amount
10 8/2/96 4 2 2400
20 1/30/99 4 8 1800
30 7/14/95 9 1 460
40 1/29/98 7 2 540
50 2/3/98 6 7 600
60 3/2/98 6 7 720
70 5/6/98 9 7 150
Question7. Find the largest order amount for each salesperson and the associated order number,
along with the customer to whom that order belongs to.
Answer.
select ord.sales_id, sp.name,number as OrderNumber, cust_id ,amount from
orders ord
JOIN
salesperson sp
on sp.sales_id= ord.sales_id
JOIN
(select sales_id ,MAX(amount)mx from orders groupby sales_id)maxord
on ord.amount= maxord.mx
Question8. Write syntax to create a View name CustomerOrders_vw to display amount of each
salesperson and the associated order number, along with the salesperson name.
Answer.
CREATEVIEW Customerorders_vw
As
Select name ,number, order_date,amount
FROM dbo.Salesperson sp
Join dbo. orders ord
ON sp.sales_id=ord.sales_id
Answer.
ALTERVIEW Customerorders_vw
WITHENCRYPTION
As
Select name ,number, order_date,amount
FROM dbo.salesperson sp
Join dbo.orders ord
ON sp.sales_id=ord.sales_id
Answer.
DROPVIEW Customerorders_vw
Question11. Write syntax to alter the above View name CustomerOrders_vw in order to bind with
underlying tables
Answer.
ALTERVIEW Customerorders_vw
WITHSCHEMABINDING
AS
SELECT name ,number, order_date,amount
FROM dbo.salesperson sp
Join dbo.orders ord
ON sp.sales_id=ord.sales_id.
Question12. You have created the stored procedure, shown below, to report the year-to-date sales
for a specific book title.
Createprocedure get_sales_for_title
@title varchar(80), @ytd_sales intoutput
AS
SELECT @ytd_sales= ytd_sales from titles where title=@title
IF@@rowcount=0
Return (-1)
Else
Return 0
You are now creating a script that will perform this stored
procedure. The stored procedure should report the year-to-date
sales for the book title if it executes successfully. If,
however, the stored procedures fails to execute, it should
report following message:
‘ No Sales Found’
Which of the following illustrates how the script required to
achive this should be created?
A.Declare @retval int
Decalre @ytd int
Exec get_sales_for_title‘Net Etiquette’,@ytd
IF @retval <0
Print ‘No Sales found’
ELSE
Print ‘Yeartodatesales: ‘+STR(@ytd)
GO
Answer. D
createtable UnionTest1
(
idcol intIDENTITY,
col2 char(3)
)
createtable UnionTest2
(
idcol intIDENTITY,
col4 char(3)
)
Insertinto UnionTest1
VALUES ('AAA')
Insertinto UnionTest1
VALUES ('BBB')
Insertinto UnionTest1
VALUES ('CCC')
Insertinto UnionTest2
VALUES ('CCC')
Insertinto UnionTest2
VALUES ('DDD')
Insertinto UnionTest2
VALUES ('EEE')
Question14. What would happen when you execute the code given below in query
Analyzer?
createPROCEDUREsp_who
AS
PRINT'SURPRISE'
GO
EXECUTEsp_who
Answer. Information about current SQL Server users and processes is displayed.
The explanation could be found in the Books Online under Creating a Stored
Procedure. One of the sections, named System Stored Procedures which describes
how SQL Server looks up the system stored procedure has this note: Important If any
user-created stored procedure has the same name as a system stored procedure, the
user-created stored procedure will never be executed.
Question15. On Friday, you issued several INSERT statements using Query Analyzer.
You then verified the data had been correctly entered with a SELECT statement. On
Monday, your users report that data is not there. What happened?
In this mode, after inserting or updating the data, you must issue the COMMIT
TRANSACTION statement to save the data permanently. Otherwise, once the
connection is broken all your inserts are rolled back.
Employee_Test Employee_Test_Audit
Emp_ID Emp_Name Emp_Sal Emp_ID Emp_Name Emp_Sal Audit_Action DateTime
1 Anees 1000.00
2 Rick 1200.00
3 John 1100.00
4 Stephen 1300.00
Question16. Now write syntax to create AFTER INSERT Trigger which will insert the
rows inserted in Employee_Test table into Employee_Test_Audit table.
Answer.
END
Answer.
END
Answer.
Alter table Employee_Test disable trigger trgAfterDelete
Answer.
Answer.
BEGIN
if(@emp_sal>1200)
begin
PRINT 'Cannot delete where salary > 1200'
ROLLBACK;
end
END
END
EmployeeInfo
EmpId EmpName EmpLogin Emppassword EmploymentDate
EmpProjInfo
EmpId Projectname
1 IBS
2 Tata Steel
3 Godrej
4 OITDS
5 Godrej
Question21. Write syntax to create a view which gives the empid, empname,
employmentdate as the output.
Answer.
Question22. Write syntax to create a view which gives the information about the
employees and its projects as the output.
Answer.
Answer.
Create view Vw_EmployeeProj
WITH ENCRYPTION
As
Select EmployeeInfo.EmpId, EmployeeInfo.EmpName,
EmpProjInfo.Projectname from EmployeeInfo inner join
EmpProjInfo on EmployeeInfo.EmpId=EmpProjInfo.EmpId
Question24. Write syntax to ALTER a view which gives the information about the
employees and its projects who are working on Godrej Project.
Answer.
Question25. Write syntax to alter the above View name in order to bind with underlying
tables
Answer.
Scenario Based: - there is a table called tbl_Students whose structure is given below:
(
[Studentid] [int] IDENTITY(1,1) NOT NULL,
[Firstname] [nvarchar](200) NOT NULL,
[Lastname] [nvarchar](200) NULL,
[Email] [nvarchar](100) NULL
)
Studentid Firstname Lastname Email
1 Vivek Johari vivek@abc.com
2 Pankaj Kumar pankaj@abc.com
3 Amit Singh amit@abc.com
4 Manish Kumar manish@abc.comm
5 Abhishek Singh abhishek@abc.com
Question27. Write syntax to create a stored procedure which will returns student name
whose Studentid is given as input parameter.
Answer
@studentid INT
)
AS
BEGIN
SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid
END
Question29. Write syntax to create a stored procedure which will returns student name
in the output parameter.
Answer
Create PROCEDURE GetstudentnameInOutputVariable
(
@studentid INT,
@studentname VARCHAR(200) OUT
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE
studentid=@studentid
END
Answer.
Question31. Write a syntax to alter a stored procedure which will returns student email
address along with student name in the output parameter.
Answer
@studentid INT,
@studentname VARCHAR (200) OUT,
@StudentEmail VARCHAR (200)OUT
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname,
@StudentEmail=email FROM tbl_Students WHERE studentid=@studentid
END
Answer.
Scenario based Question:- You are working as a database developer at CMC. CMC has
a database named HumanResources that contain information about all employees and
office locations. The database also includes information about potential employees and
office locations, which are stored in the tables as shown in the exhibit below:
Existing employees are assigned to a location and existing location have one or more
employees assigned to them. Potential employees are not assigned to a location, and
potential office locations do not have any employees. You have received instruction
from CEO to create a report that displays all existing and potential employees and office
locations. The report has to list every existing and potential location, followed by any
employees who have been assigned to that location. Also, potential employees have to
be listed together.
Question34. Of the following script, which is the one you should employee to achieve
this objective?
A.
SELECT l.LocationName, e.FirstName, e.LastName
FROM Employee AS e LEFT OUTER JOIN Location as l
ON e. LocationId = l. LocationId
ORDER BY l.LocationName, e.FirstName, e.LastName
B.
SELECT l.LocationName, e.FirstName, e.LastName
FROM Location AS l LEFT OUTER JOIN Employee as l
ON e. LocationId = l. LocationId
ORDER BY l.LocationName, e.FirstName, e.LastName
C.
SELECT l.LocationName, e.FirstName, e.LastName
FROM Employee AS e FULL OUTER JOIN Location as l
ON e. LocationId = l. LocationId
ORDER BY l.LocationName, e.FirstName, e.LastName
D.
SELECT l.LocationName, e.FirstName, e.LastName
FROM Employee AS e FULL OUTER JOIN Location as l
ORDER BY l.LocationName, e.FirstName, e.LastName
Answer – c
Question35. Which of the above script you should use to create a list of all possible
location and employee combinations?
Answer – D
You must populate the Countries table by moving the country information from the
Instructors table to the Countries Table. You must accomplish this task by using cursor.
Write Transact-SQL query to accomplish the job.
ANSWER.
DECLARE @Country char(20)
DECLARE cursor_country CURSOR
FOR SELECT Country FROM Instructors
OPEN cursor_country
FETCH NEXT FROM Instructors into @Country
WHILE @@FETCH_STATUS=0
BEGIN
IF NOT EXISTS (SELECT CountryID FROM Countries WHERE
CountryName=@Country)
INSERT INTO Countries(CountryName)VALUES (@Country)
FETCH NEXT FROM Instructors into @Country
END
CLOSE cursor_country
DEALLOCATE cursor_country
Question37. All customer companies that purchase significant amount of products are
offered discounts. Prices of the products with a category ID of 15 have been increased
by 10%. To compensate for customer companies that placed there orders on the day of
the price increase the discounts for those products must also be increased by 10%.
Both UPDATE statements must be implemented together. In the event of either update
failing, then the other update must not be implemented.
BEGIN TRAN
UPDATE Products SET Unitprice = Unitprice * 1.1 WHERE CategoryID =15
UPDATE OrderDetails SET Discount=Discount*1.1
FROM OrderDetails od JOIN Products p ON od.PRODUCTID= p.PRODUCTID
JOIN orders o ON o.OrderID= od. OrderID
WHERE CategoryID =15 AND OrderDate= '18 NOV 2013'
COMMIT
BEGIN TRAN
UPDATE Products SET Unitprice = Unitprice * 1.1 WHERE CategoryID =15
COMMIT
BEGIN TRAN
UPDATE OrderDetails SET Discount=Discount*1.1
FROM OrderDetails od JOIN Products p ON od.PRODUCTID= p.PRODUCTID
JOIN orders o ON o.OrderID= od. OrderID
WHERE CategoryID =15 AND OrderDate= '18 NOV 2013'
COMMIT
ANSWER. A
Write a Transact SQL Script to produce the result as shown below. You must use the
CURSOR.
ANSWER.
exec test1
go
Answer.
1
2
A. UPDATE customer
set c.name =c.name
FROM customer c, customerdetail cd
WHERE c.custid=cd.custid
B. UPDATE c
set c.name =u.name
FROM customer c, customerdetail cd
WHERE c.custid=cd.custid
C.UPDATE customer
set name =c.name
FROM customer c, customerdetail cd
WHERE c.custid=cd.custid
D.None of the Above
Answer. B & C