Pages

Social Icons

Wednesday 16 May 2012

Get random values in TOP predicate using NEWID()

In SQL-Server we have NEWID() function which will give us a new id every time we can see this by executing the below statement.
       SELECT NEWID()


We can use this function in our order by clause while using the TOP Predicate.


Example : 


 SELECT TOP(3)
        EmployeeName,
        Salary
 FROM 
Employee
 ORDER BY
NEWID()


This statement will give us every time different TOP 3 records.


Thx,
RS
 



Tuesday 15 May 2012

Configure Report Manager URL and TargetServerURL path from SQL-Server for reports

Yesterday I installed Sql-Server 2008 R2 in may machine and I tried to log in on the report manager with my old URL but I was not able to find that URL. So I search on the internet and I found that

Report Manager URL is : http://servername/reports
TargetServerURL is : http://servername/reportserver

This is not absolutely correct because after tried with this url also I was getting the same error.

For correct url we need to go in configuration toold of the sql-server in side the start menu.

Then we need to go in web service URL in this we need to give virtual directory path. This will become our TargetServerURL in my case it is : http://servername/ReportServer_MSSQLSERVERR2




Then we need to go in Report Manager URL and set Virtual Directory Path. This will become our Report Manager URL in my case it is : http://servername/Reports_MSSQLSERVERR2





Thx,
RS

Monday 14 May 2012

10 Easy steps to walk through with SSRS

In this post I am giving the 10 easy steps to create the SSRS report.

Step 1 : Write a SQL function or stored procedure which we will use in SSRS.


Step 2 : Open BIDS(Business Intelligence Development Studio) and Add new project. In side that project open solution explorer and right click on report folder and click on add new item.


Step 3 : In this dialogue select Report and Give it a name like CustomerReport


Step 4 : Now Click on View and select Report Data. In this explorer select Data Source folder and right click and say Add Data Source


Step 5 : In the below dialogue box click on edit and give the datasource connection details.


Step 6 : Now click on Add dataset


Step 7 : Now in the below dialogue box select stored procedure radio button and give the your stored procedure name in the textbox.


Step 8 : Right Click on report and click on insert then you will get the list of control for the report. Select Page Header, Page Footer, table and one text box in header and two text box in footer.


Step 9 : Now go into the table properties and assign dataset to it. Now give the field name to each column. In the header text box write "Customer Details". At the footer in one text box write '= "Page:  " & Globals!PageNumber & "  of  " & Globals!TotalPages' this will give the page count and in second text box write '=FORMAT(Globals!ExecutionTime,"MM/dd/yyyy hh:mm tt")' this will give the report execution time.


Step 10 : Now Click on preview and see the report.


Thx,
RS

Attatch a .MDF file without .Ldf file

When you want to add a mdf file which does ot have its corresponding ldf file then you can use the following command. This command will create the database and restore the mdf file.

sp_attach_single_file_db
           @dbname='AdventureWorks',
          @physname='D:\AdventureWorksLT2008_Data.mdf'

When I was using this command then I got One error i.e. "Access is denied". For solving this problem go to the physician location of the mdf file and right click --> Properties --> Security. Then click on edit and give the full control to this file. then this error will not come.


Thx,
RS

Wednesday 9 May 2012

SQL Server Interview Questions and Answers


1. Which TCP/IP port does SQL Server run on? How can it be changed?
SQL Server runs on port 1433. It can be changed from the Network Utility TCP/IP properties.


2. What are the difference between clustered and a non-clustered index?
A clustered index is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages.
A non clustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a non clustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.

3. What is OLTP (Online Transaction Processing)?
In OLTP - online transaction processing systems relational database design use the discipline of data modeling and generally follow the Codd rules of data normalization in order to ensure absolute data integrity. Using these rules complex information is broken down into its most simple structures (a table) where all of the individual atomic level elements relate to each other and satisfy the normalization rules.

4. What's the difference between a primary key and a unique key?
Both primary key and unique key enforces uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only.

5. What is difference between DELETE and TRUNCATE commands?
Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.

TRUNCATE:
TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.
TRUNCATE removes the data by deallocating the data pages used to store the table's data, and only the page deallocations are recorded in the transaction log.
TRUNCATE removes all rows from a table, but the table structure, its columns, constraints, indexes and so on, remains. The counter used by an identity for new rows is reset to the seed for the column.
You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint. Because TRUNCATE TABLE is not logged, it cannot activate a trigger.
TRUNCATE cannot be rolled back.
TRUNCATE is DDL Command.
TRUNCATE Resets identity of the table

DELETE:
DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.
If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.
DELETE Can be used with or without a WHERE clause
DELETE Activates Triggers.
DELETE can be rolled back.
DELETE is DML Command.
DELETE does not reset identity of the table.

Note: DELETE and TRUNCATE both can be rolled back when surrounded by TRANSACTION if the current session is not closed. If TRUNCATE is written in Query Editor surrounded by TRANSACTION and if session is closed, it can not be rolled back but DELETE can be rolled back.

6. Can a stored procedure call itself or recursive stored procedure? How much level SP nesting is possible?
Yes. Because Transact-SQL supports recursion, you can write stored procedures that call themselves. Recursion can be defined as a method of problem solving wherein the solution is arrived at by repetitively applying it to subsets of the problem. A common application of recursive logic is to perform numeric computations that lend themselves to repetitive evaluation by the same processing steps. Stored procedures are nested when one stored procedure calls another or executes managed code by referencing a CLR routine, type, or aggregate. You can nest stored procedures and managed code references up to 32 levels.

7. What is the difference between a Local and a Global temporary table?
A local temporary table exists only for the duration of a connection or, if defined inside a compound statement, for the duration of the compound statement.
A global temporary table remains in the database permanently, but the rows exist only within a given connection. When connection is closed, the data in the global temporary table disappears. However, the table definition remains with the database for access when database is opened next time.

8. What is the STUFF function and how does it differ from the REPLACE function?
STUFF function is used to overwrite existing characters. Using this syntax, STUFF (string_expression, start, length, replacement_characters), string_expression is the string that will have characters substituted, start is the starting position, length is the number of characters in the string that are substituted, and replacement_characters are the new characters interjected into the string. REPLACE function to replace existing characters of all occurrences. Using the syntax REPLACE (string_expression, search_string, replacement_string), where every incidence of search_string found in the string_expression will be replaced with replacement_string.

9. What is PRIMARY KEY?
A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table. The primary key constraints are used to enforce entity integrity.

10. What is UNIQUE KEY constraint?
A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints.

11. What is FOREIGN KEY?
A FOREIGN KEY constraint prevents any actions that would destroy links between tables with the corresponding data values. A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce referential integrity.

12. What is CHECK Constraint?
A CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity.

13. What is NOT NULL Constraint?
A NOT NULL constraint enforces that the column will not accept null values. The not null constraints are used to enforce domain integrity, as the check constraints.

14. How to get @@ERROR and @@ROWCOUNT at the same time?
If @@Rowcount is checked after Error checking statement then it will have 0 as the value of @@Recordcount as it would have been reset. And if @@Recordcount is checked before the error-checking statement then @@Error would get reset. To get @@error and @@rowcount at the same time do both in same statement and store them in local variable.
SELECT @RC = @@ROWCOUNT, @ER = @@ERROR

15. What are the advantages of using Stored Procedures?
Stored procedure can reduced network traffic and latency, boosting application performance.
Stored procedure execution plans can be reused, staying cached in SQL Server's memory, reducing server overhead.
Stored procedures help promote code reuse.
Stored procedures can encapsulate logic. You can change stored procedure code without affecting clients.
Stored procedures provide better security to your data.

16. What is a table called, if it has neither Cluster nor Non-cluster Index? What is it used for?
Unindexed table or Heap. Microsoft Press Books and Book on Line (BOL) refers it as Heap. A heap is a table that does not have a clustered index and, therefore, the pages are not linked by pointers. The IAM pages are the only structures that link the pages in a table together. Unindexed tables are good for fast storing of data. Many times it is better to drop all indexes from table and then do bulk of inserts and to restore those indexes after that.

17. Can SQL Servers linked to other servers like Oracle?
SQL Server can be linked to any server provided it has OLE-DB provider from Microsoft to allow a link. E.g. Oracle has an OLE-DB provider for oracle that Microsoft provides to add it as linked server to SQL Server group.

18. What is an execution plan? When would you use it? How would you view the execution plan?
An execution plan is basically a road map that graphically or textually shows the data retrieval methods chosen by the SQL Server query optimizer for a stored procedure or ad- hoc query and is a very useful tool for a developer to understand the performance characteristics of a query or stored procedure since the plan is the one that SQL Server will place in its cache and use to execute the stored procedure or query. From within Query Analyzer is an option called "Show Execution Plan" (located on the Query drop-down menu). If this option is turned on it will display query execution plan in separate window when query is ran again.

Thursday 3 May 2012

SSRS Interview Questions


1. What is SQL Server Reporting Services(SSRS)?
SQL Server Reporting Services is a server-based reporting platform that you can use to create and manage tabular, matrix, graphical, and free-form reports that contain data from relational and multidimensional data sources. The reports that you create can be viewed and managed over a World Wide Web-based connection

2. What is the difference between SQL Server Reporting Services and Crystal Reports?
Crystal reports are processed by IIS while SSRS have a report server. Caching in Crystal reports is available through cache server. On the other hand, caching in SSRS s available for Report history snapshots. Crystal reports have standards and user defined field labels. SSRS allows only user defined field labels.

3. Explain the architecture of SQL Reporting service.
Reporting Services runs as a middle-tier server, as part of your existing server architecture.
SQL Server 2000 should be installed for the database server, and Internet Information Services 6.0 as a Web server.
The report server engine takes in report definitions, locates the corresponding data, and produces the reports.
Interaction with the engine can be done through the Web-based Report Manager, which also lets you manage refresh schedules and notifications.
End users view the report in a Web browser, and can export it to PDF, XML, or Excel.

4. Describe report life cycle
The Reporting Lifecycle includes
Report designing – The designing is done in Visual Studio Report Designer. It generates a class which embodies the Report Definition.
Report processing – The processing includes binding the report definition with data from the report data source. It performs on all grouping, sorting and filtering calculations. The expressions are evaluated except the page header, footer and section items. Later it fires the Binding event and Bound event. As a result of the processing, it produces Report Instance. Report instance may be persisted and stored which can be rendered at a later point of time.
Report Rendering-  Report rendering starts by passing the Report Instance to a specific rendering extension (HTML or PDF formats). The instance of reports is paged if paging supported by output format. The expressions of items are evaluated in the page header and footer sections for every page. As a final step, the report is rendered to the specific output document.

5. How to deploy the Report?
Report can be deployed in three ways.
1. Using visual studio: In visual studio you can directly deploy the report through solution explorer by providing the report server URL in project properties at Target Server URL. This will deploy entire project or single report as per you selection.
2. Using report server: Can directly go to the report server and deploy the report by browsing the report from the disk location on server.
3. Creating the utility: SQL server provides the utility using that which can be used to create a customize utility for your report deployment in bulk.

6. What is RS.exe utility?
Rs.exe utility is used for deploying the report on report server. It comes with the report server and can be customize accordingly.

7. What are the three stages of Enterprise Reporting Life Cycle ?
a. Authoring
b. Management
c. Access and Delivery

8. What are the components included in SSRS?
1. A Complete set of Tools that can be used to create, manage and view reports
2. A Report Server component that hosts and processes reports in a variety of formats. Output formats include HTML, PDF, TIFF, Excel, CSV, and more.
3.An API that allows developers to integrate or extend data and report processing in custom applications, or create custom tools to build and manage reports.

9. Important terms used in the reporting services? 
1. Report definition:  The blueprint for a report before the report is processed or rendered. A report definition contains information about the query and layout for the report.
2. Report snapshot: A report that contains data captured at a specific point in time. A report snapshot is actually a report definition that contains a dataset instead of query instructions.
3. Rendered report: A fully processed report that contains both data and layout information, in a format suitable for viewing (such as HTML).
4. Parameterized report: A published report that accepts input values through parameters.
5. Shared data source: A predefined, standalone item that contains data source connection information.
6. Shared schedule: A predefined, standalone item that contains schedule information.
7. Report-specific data source: Data source information that is defined within a report definition.
8. Report model: A semantic description of business data, used for ac hoc reports created in Report Builder.
9. Linked report: A report that derives its definition through a link to another report.
10. Report server administrator: This term is used in the documentation to describe a user with elevated privileges who can access all settings and content of a report server. If you are using the default roles, a report server administrator is typically a user who is assigned to both the Content Manager role and the System Administrator role. Local administrators can have elevated permission even if role assignments are not defined for them.
11. Folder hierarchy: A bounded namespace that uniquely identifies all reports, folders, report models, shared data source items, and resources that are stored in and managed by a report server.
12. Report Server: Describes the Report Server component, which provides data and report processing, and report delivery. The Report Server component includes several subcomponents that perform specific functions. 
13. Report Manager: Describes the Web application tool used to access and manage the contents of a report server database.
14. Report Builder: Report authoring tool used to create ad hoc reports.
15. Report Designer: Report creation tool included with Reporting Services.
16. Model Designer: Report model creation tool used to build models for ad hoc reporting.
17. Report Server Command Prompt Utilities: Command line utilities that you can use to administer a report server.
a) RsConfig.exe, b) RsKeymgmt.exe, c) Rs.exe

10. How to know Report Execution History?
ExecutionLog table in ReportServer database store all the logs from last two months. 
 SELECT * FROM ReportServer.dbo.ExecutionLog

11. what are the Command Line Utilities available In Reporting Services?
• Rsconfig Utility (Rsconfig.exe): encrypts and stores connection and account values in the RSReportServer.config file. Encrypted values include report server database connection information and account values used for unattended report processing
• RsKeymgmt Utility: Extracts, restores, creates, and deletes the symmetric key used to protect sensitive report server data against unauthorized access
• RS Utility: this utility is mainly used to automate report server deployment and administration tasks.Processes script you provide in an input file.

12. What is difference between Tablular and Matrix report?
OR What are the different styles of reports?
Tablular report: A tabular report is the most basic type of report. Each column corresponds to a column selected from the database.
Matrix report: A matrix (cross-product) report is a cross-tabulation of four groups of data:
    a. One group of data is displayed across the page.
    b. One group of data is displayed down the page.
    c. One group of data is the cross-product, which determines all possible locations where the across and down data relate and places a cell in those locations.
    d. One group of data is displayed as the "filler" of the cells.
Martix reports can be considered more of a  Pivot table.