I have the following stored procedure body I am trying to build in SQL Server Express:
When I do a check on it, I get the following errors:
Does what I'm trying to do make sense? Can you tell me what is syntactically wrong with this (might be a lot, admittedly)
Code:
ALTER PROCEDURE [dbo].[sp_GetOrdersDistributed]
-- Add the parameters for the stored procedure here
@FromDate DateTime,
@ToDate DateTime,
@Active int,
@SearchText varchar(3000),
@OrderBy varchar(100),
@OrderByType varchar(4)
AS
Declare @strSQL varchar(4000)
@strSQL = "SELECT dbo.tbl_Items.ItemName AS [Item Name],"
@strSQL = @strSQL + " dbo.tbl_Items.ItemDescription AS [Item Description],"
@strSQL = @strSQL + " COUNT(dbo.tbl_Orders.OrderID) AS [Orders Distributed]"
@strSQL = @strSQL + " FROM dbo.tbl_OrderItems"
@strSQL = @strSQL + " INNER JOIN dbo.tbl_Orders ON dbo.tbl_OrderItems.OrderID = dbo.tbl_Orders.OrderID"
@strSQL = @strSQL + " RIGHT OUTER JOIN dbo.tbl_Items ON dbo.tbl_OrderItems.ItemID = dbo.tbl_Items.ItemID"
@strSQL = @strSQL + " WHERE dbo.tbl_Orders.DateEntered >= " + @FromDate
@strSQL = @strSQL + " AND dbo.tbl_Orders.DateEntered <= " + @ToDate
@strSQL = @strSQL + " AND Active >= " + @Active
@strSQL = @strSQL + @SearchText
@strSQL = @strSQL + " GROUP BY dbo.tbl_Items.ItemName,"
@strSQL = @strSQL + " dbo.tbl_Items.ItemDescription"
@strSQL = @strSQL + " ORDER BY " + @OrderBy + " " + @OrderByType
EXEC(@strSQL)
When I do a check on it, I get the following errors:
Code:
Msg 102, Level 15, State 1, Procedure sp_GetOrdersDistributed, Line 13
Incorrect syntax near '@strSQL'.
Msg 137, Level 15, State 2, Procedure sp_GetOrdersDistributed, Line 27
Must declare the scalar variable "@strSQL".
Does what I'm trying to do make sense? Can you tell me what is syntactically wrong with this (might be a lot, admittedly)