Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-07-17 : 13:03:12
How can I make this query part of a stored procedure so that I can prompt for month and year?


SELECT TOP (100) PERCENT dbo.cicmpy.SalesPersonNumber, dbo.arslmfil_SQL.slspsn_name, dbo.oehdrhst_sql.cus_no, SUM(dbo.oehdrhst_sql.tot_sls_amt)
AS TotalSales, SUM(dbo.oehdrhst_sql.tot_cost) AS TotalCost
FROM dbo.arslmfil_SQL INNER JOIN
dbo.cicmpy ON dbo.arslmfil_SQL.humres_id = dbo.cicmpy.SalesPersonNumber RIGHT OUTER JOIN
dbo.oehdrhst_sql ON dbo.cicmpy.debcode = dbo.oehdrhst_sql.cus_no
WHERE (YEAR(dbo.oehdrhst_sql.inv_dt) = 2012) AND (MONTH(dbo.oehdrhst_sql.inv_dt) = 6)
GROUP BY dbo.oehdrhst_sql.slspsn_no, dbo.oehdrhst_sql.cus_no, dbo.arslmfil_SQL.slspsn_name, dbo.cicmpy.SalesPersonNumber
ORDER BY dbo.cicmpy.SalesPersonNumber

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-17 : 13:17:01
create prod myproc
@year int ,
@month int
as

SELECT TOP (100) PERCENT dbo.cicmpy.SalesPersonNumber, dbo.arslmfil_SQL.slspsn_name, dbo.oehdrhst_sql.cus_no, SUM(dbo.oehdrhst_sql.tot_sls_amt)
AS TotalSales, SUM(dbo.oehdrhst_sql.tot_cost) AS TotalCost
FROM dbo.arslmfil_SQL INNER JOIN
dbo.cicmpy ON dbo.arslmfil_SQL.humres_id = dbo.cicmpy.SalesPersonNumber RIGHT OUTER JOIN
dbo.oehdrhst_sql ON dbo.cicmpy.debcode = dbo.oehdrhst_sql.cus_no
WHERE (YEAR(dbo.oehdrhst_sql.inv_dt) = @year) AND (MONTH(dbo.oehdrhst_sql.inv_dt) = @month)
GROUP BY dbo.oehdrhst_sql.slspsn_no, dbo.oehdrhst_sql.cus_no, dbo.arslmfil_SQL.slspsn_name, dbo.cicmpy.SalesPersonNumber
ORDER BY dbo.cicmpy.SalesPersonNumber

go

The prompting will be done by your client before the SP call.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-07-17 : 13:18:24
create proc procName
(@date datetime) --pass in the first day of the month/year here
as
SELECT TOP (100) PERCENT dbo.cicmpy.SalesPersonNumber, dbo.arslmfil_SQL.slspsn_name, dbo.oehdrhst_sql.cus_no, SUM(dbo.oehdrhst_sql.tot_sls_amt)
AS TotalSales, SUM(dbo.oehdrhst_sql.tot_cost) AS TotalCost
FROM dbo.arslmfil_SQL INNER JOIN
dbo.cicmpy ON dbo.arslmfil_SQL.humres_id = dbo.cicmpy.SalesPersonNumber RIGHT OUTER JOIN
dbo.oehdrhst_sql ON dbo.cicmpy.debcode = dbo.oehdrhst_sql.cus_no
WHERE oehdrhst_sql.inv_dt >= @date AND oehdrhst_sql.inv_dt < DATEADD(mm, 1, @date)
--WHERE (YEAR(dbo.oehdrhst_sql.inv_dt) = 2012) AND (MONTH(dbo.oehdrhst_sql.inv_dt) = 6)
GROUP BY dbo.oehdrhst_sql.slspsn_no, dbo.oehdrhst_sql.cus_no, dbo.arslmfil_SQL.slspsn_name, dbo.cicmpy.SalesPersonNumber
ORDER BY dbo.cicmpy.SalesPersonNumber

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-07-17 : 13:19:27
quote:

WHERE (YEAR(dbo.oehdrhst_sql.inv_dt) = 2012) AND (MONTH(dbo.oehdrhst_sql.inv_dt) = 6)



Never use code like that in the WHERE clause as you won't be able to use a good execution plan on it. Isolate the columns away from functions and do the math on the other side of the equation if needed.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-17 : 14:53:26
even if @month and @year are parameters you can do like this

.....
WHERE dbo.oehdrhst_sql.inv_dt >= DATEADD(mm,@month-1,DATEADD(yy,@year-1900,0))
AND dbo.oehdrhst_sql.inv_dt < DATEADD(mm,@month,DATEADD(yy,@year-1900,0))
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-07-17 : 14:56:20
Yes and that's what I meant by "do the math on the other side of the equation".

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-17 : 14:58:04
quote:
Originally posted by tkizer

Yes and that's what I meant by "do the math on the other side of the equation".

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog


yep... I was just elaborating on that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -