Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, i have the following stored procedure which will display the UserName, TargetDate in count, ActualDate in count and the CompletePercentage. Results get are in June. How can i display the records i get into a row June and the rest of the months i will set to 0. E.g.
Output
==========
Jan 0 0 0
Feb 0 0 0
Mar 0 0 0
Apr 0 0 0
May 0 0 0
Jun 1 1 80
.
.
.

i am not sure whether i should do this grouping in c# code or sql. Whereby i will make this into a report in c# using devexpress. Below are my codes:

SQL
ALTER PROCEDURE [dbo].[GetCustomizationDeliveryStatusByDevNameByDate]
	-- Add the parameters for the stored procedure here
	@DEVNAME VARCHAR(MAX),
	@YEAR VARCHAR(MAX)
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	DECLARE @SQL NVARCHAR(MAX)
	DECLARE @SQL2 NVARCHAR(MAX)
	DECLARE @SQL3 NVARCHAR(MAX)
	DECLARE @SQL4 NVARCHAR(MAX)

	SELECT @SQL = ' CREATE TABLE #TargetDateTblx(IssueID VARCHAR(100), TargetDate VARCHAR(100))
        		INSERT [#TargetDateTblx]
        		SELECT issueid, [fielddata] AS TargetDate
        		FROM rndbug.dbo.gemini_customfielddata
        		WHERE customfieldid = 215					
        		
        		
        		CREATE TABLE #ActualDateTblx(IssueID VARCHAR(100), ActualDate VARCHAR(100))
			INSERT [#ActualDateTblx]
			SELECT issueid, [fielddata] AS ActualDate
			FROM rndbug.dbo.gemini_customfielddata
			WHERE customfieldid = 217					
			
			
			CREATE TABLE #CompleteRateTblx(IssueID VARCHAR(100), CompleteRate VARCHAR(100))
			INSERT [#CompleteRateTblx]
			SELECT issueid, [fielddata] AS CompleteRate
			FROM rndbug.dbo.gemini_customfielddata
			WHERE customfieldid = 234					
			
			
			CREATE TABLE #ALLTB(IssueID VARCHAR(100), UserID VARCHAR(100), UserName VARCHAR(100), TargetDate VARCHAR(100), ActualDate VARCHAR(100), CompleteRate VARCHAR(100))
			INSERT [#ALLTB]
			SELECT a.issueid,a.userid,b.username,c.TargetDate,d.ActualDate,e.CompleteRate
			FROM dbo.gemini_issueresources A, dbo.gemini_users B, #TargetDateTblx c, #ActualDateTblx d, #CompleteRateTblx e
			WHERE a.userid = b.userid AND a.issueid = c.IssueID AND  a.issueid = d.IssueID AND  a.issueid = e.IssueID
					'
					
		SET @SQL2 =	'CREATE TABLE #FORMULA(UserID VARCHAR(100), TargetDate VARCHAR(100), ActualDate VARCHAR(100), CompleteRate VARCHAR(100))
				INSERT [#FORMULA]
				SELECT UserID, COUNT(TargetDate) AS TargetDate, COUNT(ActualDate) AS ActualDate, SUM(CONVERT(INT, CompleteRate)) AS CompleteRate FROM #ALLTB					
				GROUP BY UserID
				
				SELECT B.username, F.TargetDate, F.ActualDate, (CONVERT(INT, F.CompleteRate)/F.TargetDate) AS CompletePercentage 
				FROM #FORMULA F, dbo.gemini_users B, #ALLTB A
				WHERE B.username IN ('+@DEVNAME+') AND F.UserID = B.UserID AND F.UserID = A.UserID
					'
					
		SET @SQL3 = 'CREATE TABLE #FORMULA(UserID VARCHAR(100), TargetDate VARCHAR(100), ActualDate VARCHAR(100), CompleteRate VARCHAR(100))
        		     INSERT [#FORMULA]
        		     SELECT UserID, COUNT(TargetDate) AS TargetDate, COUNT(ActualDate) AS ActualDate, SUM(CONVERT(INT, CompleteRate)) AS CompleteRate FROM #ALLTB					
			     GROUP BY UserID
					
		             SELECT B.username, F.TargetDate, F.ActualDate, (CONVERT(INT, F.CompleteRate)/F.TargetDate) AS CompletePercentage 
			     FROM #FORMULA F, dbo.gemini_users B, #ALLTB A
			     WHERE F.UserID = B.UserID AND F.UserID = A.UserID AND SUBSTRING(A.TargetDate, 7, 4) = '+@YEAR+'
					'
				
					
		SET @SQL4 = 'CREATE TABLE #FORMULA(TargetDate VARCHAR(100), ActualDate VARCHAR(100), CompleteRate VARCHAR(100))
			     INSERT [#FORMULA]
		             SELECT COUNT(TargetDate) AS TargetDate, COUNT(ActualDate) AS ActualDate, SUM(CONVERT(INT, CompleteRate)) AS CompleteRate FROM #ALLTB					
					
					
			     SELECT TargetDate, ActualDate, (CONVERT(INT, CompleteRate)/TargetDate) AS CompletePercentage 
			     FROM #FORMULA 
					'
	
	-- AND B.username = '+@DEVNAME+'	
    -- Insert statements for procedure here
	--declare @year varchar(30)
	--set @year = convert(date, GETDATE(), 103)

	--select month(@year)
    IF(@DEVNAME = '' AND @YEAR = '')
		SELECT @SQL = @SQL + @SQL4
				
    ELSE IF(@DEVNAME != '' AND @YEAR != '')
		SELECT @SQL = @SQL + ' AND B.username IN ('+@DEVNAME+')' + @SQL2 + ' AND SUBSTRING(A.TargetDate, 7, 4) = '+@YEAR+''
    
    ELSE IF(@DEVNAME != '' AND @YEAR = '')
		SELECT @SQL = @SQL + ' AND B.username IN ('+@DEVNAME+')' + @SQL2
    
    ELSE
		SELECT @SQL = @SQL + @SQL3
    
	SELECT @SQL = @SQL
	EXEC sp_executesql @SQL
END


sorry it looks messy. Any helps would be appreciated.
Posted
Comments
Prasad Avunoori 25-Jun-14 23:17pm    
Executing scripts like above is not a good practice.
George Jonsson 26-Jun-14 5:29am    
So what is good practice?
Prasad Avunoori 26-Jun-14 5:40am    
Disadvantage is:

SQL injection is possible as soon there is dynamic SQL which is handled carelessly, be that SQL statements sent from the client, dynamic SQL generated in T-SQL stored procedures, or SQL batches executed from CLR stored procedures

This is how I would go about it, if I understood your question correctly.
I hope you can use it.

C#
DataTable dtMonthlyResult = new DataTable();
DataColumn dcMonth = dtMonthlyResult.Columns.Add("Month", typeof(string));
DataColumn dcTarget = dtMonthlyResult.Columns.Add("TargetDate", typeof(int));
DataColumn dcActual = dtMonthlyResult.Columns.Add("ActualDate", typeof(int));
DataColumn dcComplete = dtMonthlyResult.Columns.Add("Completeness", typeof(int));
dtMonthlyResult.PrimaryKey = new DataColumn[] { dcMonth };


Add months and initial values
C#
List<string> monthNames = DateTimeFormatInfo.CurrentInfo.MonthNames.Take(12).ToList();
foreach (string monthName in monthNames)
{
  dtMonthlyResult.Rows.Add(monthName.
    Substring(0, 3), 0, 0, 0);
}


Get data from the stored procedure
This part you should have code for already
C#
string month = "Jun";
int targetDate = 1;
int actualDate = 1;
int completeness = 80;


Find the correct row and update
C#
DataRow drFound = dtMonthlyResult.Rows.Find(month);
if (drFound != null)
{
  drFound[dcTarget] = targetDate;
  drFound[dcActual] = actualDate;
  drFound[dcComplete] = completeness;
  dtMonthlyResult.AcceptChanges();
}


Print out the table
C#
Console.WriteLine("Month\tTarget\tActual\tComplete");
Console.WriteLine("--------------------------------");
foreach (DataRow dr in dtMonthlyResult.Rows)
{
  Console.WriteLine("{0,3}\t{1,3}\t{2,3}\t{3,3}", 
  dr[dcMonth], dr[dcTarget], dr[dcActual], dr[dcComplete]);
}
Console.WriteLine("--------------------------------");
 
Share this answer
 
v2
If you want to get the entire logic in the sql itself, then use case. Something like -

DECLARE @COLVALUE1 INT ,@COLVALUE2 INT,@COLVALUE3 INT
DECLARE @MONTH VARCHAR(50) = 'June'
SET @COLVALUE1 =
CASE
WHEN @MONTH <> 'June' THEN
0
ELSE
1
END
SET @COLVALUE2 =
CASE
WHEN @MONTH <> 'June' THEN
0
ELSE
1
END
SET @COLVALUE3 =
CASE
WHEN @MONTH <> 'June' THEN
0
ELSE
1
END

INSERT INTO YOURTABLE(COL1,COL2,COL3)VALUES(@COLVALUE1,@COLVALUE2,@COLVALUE3)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900