Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to records from my Sales table with two parameters based on the month. Instead of the getting the exact months chosen, it fires me all the records. How can I go about it?

When I include the column Monat in a WHERE Clause, then I get an empty results

My sp:
SQL
CREATE PROC [dbo].[spMonthInfo]
(   
@Periode1 NVARCHAR (255),
@Periode2 NVARCHAR (255) 
)
AS
BEGIN
DECLARE @Dynamictbl nvarchar(MAX) =
N'SELECT
[t].[Monat], [t].[Project],[t].[Description],
[t].[Finance], --[t].[Project] AS [Entrance],

SUM(CASE WHEN [t].Monat = ' + @Periode1 + ' THEN [t].[Amount1] END) AS [Amount1Feb],
SUM(CASE WHEN [t].Monat = ' + @Periode2 + ' THEN [t].[Amount1] END) AS [Amount1March],
SUM(CASE WHEN [t].Monat = ' + @Periode1 + ' THEN [t].[Amount2] END) AS [Amount2Feb],
SUM(CASE WHEN [t].Monat = ' + @Periode2 + ' THEN [t].[ Amount2] END) AS [Amount2March],

FROM [dbo].[tblSales]


GROUP BY [t].[Monat],[t].[Project],[t].[Description],
[t].[Finance], --[t].[Project] AS [Entrance],

EXECUTE sp_executesql @Dynamictbl

END
GO
-- 
EXEC spMonthInfo @Periode1 = '02.2011', @Periode2 = '03.2011'


The Problem: [^]
Posted
Comments
CHill60 20-May-15 9:27am    
Are you absolutely sure that the format of the input parameters @Periode1 and @Periode2 match the data in the column Monat?
I also think you might need an ELSE 0 for those case statements
mikybrain1 20-May-15 9:49am    
Yeah. They do match match exactly. I have added a where clause to it and it seems to be functioning WHERE [t].[Monat] = ' + @Periode1 +' OR [t].[Monat] = '+ @Periode2 + ' AND [t].[Region] = ''AB'' but then the next problem. This line isn't applying. AND [t].[Region] = ''AB''. Do u please have a clue?
_Asif_ 20-May-15 9:33am    
Monat type is DateTime?
mikybrain1 20-May-15 9:50am    
No it's a NVARCHAR

1 solution

A perfect example of why stored procedures don't make you immune to SQL Injection[^] vulnerabilities.

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Within SQL, use sp_executesql[^] to execute a dynamic query with parameters.

You also have extra trailing commas within your dynamic query which need to be removed.
SQL
CREATE PROC [dbo].[spMonthInfo]
(   
    @Periode1 NVARCHAR(255),
    @Periode2 NVARCHAR(255) 
)
AS
BEGIN
    DECLARE @Dynamictbl nvarchar(MAX) = N'SELECT
        [t].[Monat], [t].[Project],[t].[Description],
        [t].[Finance], --[t].[Project] AS [Entrance],
        SUM(CASE WHEN [t].Monat = @Periode1 THEN [t].[Amount1] END) AS [Amount1Feb],
        SUM(CASE WHEN [t].Monat = @Periode2 THEN [t].[Amount1] END) AS [Amount1March],
        SUM(CASE WHEN [t].Monat = @Periode1 THEN [t].[Amount2] END) AS [Amount2Feb],
        SUM(CASE WHEN [t].Monat = @Periode2 THEN [t].[Amount2] END) AS [Amount2March]
    FROM 
        [dbo].[tblSales]
    GROUP BY 
        [t].[Monat],
        [t].[Project],
        [t].[Description],
        [t].[Finance]
    ;';
 
    EXECUTE sp_executesql @Dynamictbl, 
        N'@Periode1 NVARCHAR(255), @Periode2 NVARCHAR(255)',
        @Periode1 = @Periode1,
        @Periode2 = @Periode2
    ;
END
GO

In this particular example, you're not gaining anything by using a dynamic query. You could replace the entire body of the stored procedure with the contents of your @Dynamictbl string.
 
Share this answer
 
v2
Comments
mikybrain1 20-May-15 10:34am    
@Richard. Thnx for the tip. But I do have one problem. I am using a where clause to differentiate the records: WHERE [t].[Monat] = @Periode1 OR [t].[Monat] = @Periode2 AND [t].[Finance] = ''AB'' but This line isn't applying: AND [t].[Region] = ''AB''. It still fires me the whole records Do u please have a clue?
Richard Deeming 20-May-15 10:38am    
It's probably an issue with operator precedence - you're asking for (records in period 1) OR (records in period 2 with finance = 'AB').

Try adding parentheses to the filter:
WHERE ([t].[Monat] = @Periode1 OR [t].[Monat] = @Periode2) AND [t].[Finance] = ''AB''

Alternatively, you could use the IN operator:
WHERE [t].[Monat] IN (@Periode1, @Periode2) AND [t].[Finance] = ''AB''
mikybrain1 21-May-15 3:04am    
Thnx.

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