CREATE TABLE #TestTable(ctrl_no INT, id int, Amount NUMERIC(18,2), OrderDate DATE)
INSERT INTO #TestTable(ctrl_no , id , Amount , OrderDate )
SELECT 1000029,100016,990.0000,'10/25/2012'
UNION ALL
SELECT 1000029,100018,990.0000,'10/25/2012'
UNION ALL
SELECT 1000029,100016,660.0000,'10/26/2012'
UNION ALL
SELECT 1000029,100018,660.0000,'10/26/2012'
UNION ALL
SELECT 1000029,206007,660.0000,'10/26/2012'
UNION ALL
SELECT 1000029,206007,660.0000,'10/27/2012'
DECLARE @MinDate DATE
,@MaxDate DATE
DECLARE @Dates VARCHAR(MAX)
SELECT @Dates = ''
SELECT @MinDate = MIN(OrderDate)
,@MaxDate = MAX(OrderDate)
FROM #TestTable
WHILE @MinDate <= @MaxDate
BEGIN
SELECT @Dates = @Dates + '[' + CONVERT(VARCHAR(10), @MinDate, 101) + '],'
SELECT @MinDate = DATEADD(d, 1, @MinDate)
END
SELECT @Dates = LEFT(@Dates, LEN(@Dates) - 1)
DECLARE @qry NVARCHAR(MAX)
SELECT @qry = N'
SELECT id,
' + @Dates + '
FROM (
SELECT id,
ISNULL(Amount,0) AS Amount,
OrderDate
FROM #TestTable t
) x
PIVOT(
SUM(Amount) FOR OrderDate IN(' + @Dates + ')
) p '
EXEC (@qry)
DROP TABLE #TestTable
Learn SQL and database management at SQLYoga for articles, tutorials, and tips to improve your skills and streamline data operations. Join our community!
December 24, 2012
SQL SERVER: Cross tab Query with Pivot (Dynamic Pivot)
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
November 20, 2012
Configure SQL Reporting Service, to Access Reports using External IP With IIS 7
| Hi All, Recently I come to situation where I need to Configure Reporting services on server having configuration: Window server 2008 and IIS 7. I need to configure reports in manner, so User can access this reports thru External IP Address. I found configuration for IIS 7.0, so user can access reports externally. To able to access the reports externally, do the steps as mentioned below: 1. Create Virtual Directory in IIS 7.0 : 2. Go to Handler Mappings : 3. Double Click on "Handler Mapping". You will get screen like the given below: 4. Now see Top Right Corner: Click On "Add Managed Handler". Here you need to fill up the info like: Fill this info as: Request path: Reserved.ReportViewerWebControl.axd Thats it. Now try with External IP. You can access the reports with External IP too. |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
November 5, 2012
SQL SERVER: Cross tab query with PIVOT
| ctrl_no | id | Amount | OrderDate |
| 1000029 | 100016 | 990 | 10/25/2012 |
| 1000029 | 100018 | 990 | 10/25/2012 |
| 1000029 | 100016 | 660 | 10/26/2012 |
| 1000029 | 100018 | 660 | 10/26/2012 |
| 1000029 | 206007 | 660 | 10/26/2012 |
| 1000029 | 206007 | 660 | 10/27/2012 |
| id | 10/25/2012 | 10/26/2012 | 10/27/2012 |
| 100016 | 990.00 | 660.00 | NULL |
| 100018 | 990.00 | 660.00 | NULL |
| 206007 | NULL | 660.00 | 660.00 |
CREATE TABLE #TestTable ( ctrl_no INT ,id INT ,Amount NUMERIC(18, 2) ,OrderDate DATE ) INSERT INTO #TestTable(ctrl_no , id , Amount , OrderDate ) SELECT 1000029,100016,990.0000,'10/25/2012' UNION ALL SELECT 1000029,100018,990.0000,'10/25/2012' UNION ALL SELECT 1000029,100016,660.0000,'10/26/2012' UNION ALL SELECT 1000029,100018,660.0000,'10/26/2012' UNION ALL SELECT 1000029,206007,660.0000,'10/26/2012' UNION ALL SELECT 1000029,206007,660.0000,'10/27/2012' SELECT id ,[10/25/2012] ,[10/26/2012] ,[10/27/2012] FROM ( SELECT id ,Amount ,OrderDate FROM #TestTable t ) x PIVOT(SUM(Amount) FOR OrderDate IN ( [10/25/2012] ,[10/26/2012] ,[10/27/2012] )) p DROP TABLE #TestTable
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
November 2, 2012
SQL SERVER: Limit memory usage of SQL SERVER
| Recently, we have found that SQL SERVER is using most of the memory on the server, due to heavy queries are running on my different teams and it freezes the whole server. Due to time constraint team doesn’t have time to optimize those queries, so we have found a way to configure SQL SERVER to not to use all available memory. We have an option to configure how much memory can be used by SQL SERVER as below: 1. Connect to the SQL SERVER instance for which we need to limit memory usage and click on properties: By default, SQL SERVER is using maximum server memory, so to resolve the freezing issue, we need to change it as per requirement. So SQL SERVER will use only that much memory and other processes can run in the server. Let me know your comments. Reference: Tejas Shah (http://www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
October 16, 2012
SQL SERVER: T-SQL to find out when database was restored
| Recently, we have found that our testing database has been restored on the server and we lost our recent work. We have to identify that at what time the database has been restored, so we can know that what are the changes need to be restored again on the database. We have found that MSDB database keeps information for each restore we have made on the server and luckily we have found the time and user we had restored the database. Let me share the simple script that I have found: Reference: Tejas Shah (http://www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
October 15, 2012
SQL SERVER: Clear Job History
Recently, we have implemented few jobs and we were testing the jobs. After our test runs successfully, we wanted to clear Job’s history, so we can have accurate job status. I have found following SQL to delete job’s history:
This will clear all history for the specified job. If you wanted to clear job history up to specific date, you can use following:
If you wanted to clear job history for ALL SQL jobs, you should just execute:
|
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
October 11, 2012
SQL SERVER: Configure notification to send an email when JOB fails
| In my previous post, T SQL to display Job history, I have mentioned to get list with the job statuses. One of my developer asked me to share, How to configure Notification to send an email when Job fails. Developers problem is, he is getting empty dropdown while configuring notification as shown in below figure: Here, This dropdown contains list of Operators, so we have to create operators to whom we can send a notification email. Its very simple to configure operator, see below: Now, we have created Operator, so, lets set it up for the job as below: Now, Mail will be send to the email specified in “ErrorEmails” operator. Note: Make sure to Configure Database email to send an email from the SQL SERVER. Reference: Tejas Shah (http://www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
October 10, 2012
SQL SERVER: T-SQL to display Job history
| We have multiple jobs running on the server as per the schedules. We need to have the status of the each job at the end of the day, so we can know that which jobs runs successfully and which gets failed. We have found the table, sysjobhistory, which contains history and get the list as per oyur requirements. Let me share the query:
Here, we have used “h.step_id = 0”, as we just concern about SQL server job status, we are not concerned about each step of the job. This query gives us result as follows: We have also configured, Notification email, which sends an email whenever job is getting failed. This query is used to review all job statuses together. Please let me know your comments Reference: Tejas Shah (http://www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
September 29, 2012
SQL SERVER: SSMS, Allow to create Stored procedure though table doesn’t exists
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
November 21, 2011
SQL SERVER: Develop Reports with RDLC (Asp.Net)
| As, I mentioned in my post, Load Reports without SQL SERVER Reporting Service with Asp.Net, we can develop a report without SQL SERVER Reporting Service. That is very simple to do with Microsoft Visual Studio. Please follow the following steps to generate simple report which can be load in browser without SQL SERVER Reporting Service. We need to follow following steps to generate simple report with RDLC: 1. Create RDLC file. 2. Create Dataset from Report Data Source. 3. Design the Report 4. Runtime pass information to Report Data Source, so report will be generated with Data Let's create one simple web application with RDLC report. 1. Create RDLC file:
2. Create Dataset from Report Data Source:
3. If your "Data Source" is listed in options, select it from there, else click on "New". 4. If your "Connection" is listed in options, select it from there, else click on "New Connection", to make your connection. 5. Once "Connection" is selected, select your Table/View/Stored Procedure/Function, from which you need to build your report and give appropriate name to Dataset: 6. Once you have selected DB Object in Dataset, you can find out all the columns that are available in selected dataset as follows: 7. Once you have this dataset, you will also find that one XSD file is also created in App_code folder, with the same schema, which is used to provide schema definition to RDLC file 3. Design the RDLC Report: In Step #2, we have created Dataset with columns that needs to be displayed on the report. Now its time to use that Dataset and design the report. You should drag, "Table" from "Toolbox" and assign property "Dataset" created Dataset in Step #2.
4. Runtime pass information to Report Data Source, so report will be generated with Data: So far, we have done report designing, now its time to pass actual data to the Report Data Source, so report will be generated with actual Data. To display report on the browser, we have to use "Microsoft Reporting WebForms", which will load the report in the browser with ASPX page. Please follow the steps to pass information to RDLC Report:
using System; using System.Configuration; using System.Data; using System.Data.Common; using System.Web.UI; using Microsoft.Practices.EnterpriseLibrary.Data; using Microsoft.Practices.EnterpriseLibrary.Data.Sql; using Microsoft.Reporting.WebForms; using System.Collections.Generic; using System.Linq; using System.Web.UI.WebControls; using System.Data; using DataAccessLayer; public partial class Reports_ProjectDetail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string connectionString = ConfigurationManager.ConnectionStrings["PISConnectionString"].ConnectionString; Database db = new SqlDatabase(connectionString); DbCommand command = db.GetStoredProcCommand("ReportProjectDetail"); DataSet dataset = db.ExecuteDataSet(command); ReportDataSource datasource = new ReportDataSource("DataSet1", Dataset.Tables[0]); ReportViewer1.LocalReport.DataSources.Clear(); ReportViewer1.LocalReport.DataSources.Add(datasource); ReportViewer1.LocalReport.Refresh(); ReportViewer1.Visible = true; } } } Let me explain how it works: 1. On Page load, we call one stored procedure to get data from database. Here I have used Microsoft Application block to connect to database and get the information 2. Once, we have the data in DataSet, we have created new "Report Data Source" with the SAME name: "Dataset1" and assign result table as per #1 and assign it to ReportViewer 3. And at last we refresh ReportViewer to display the information as per Datasource provided. This report is ready and you can export it in Excel, PDF, Word etc. This example is developed in Microsoft Visual Studio 2010 Let me know your comments and difficulties, if any Reference: Tejas Shah (www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS