Hello all....I am stumped! I recently upgraded from SQL Ser ver 2000 to SQL Server 2005. I change to some code in one of my Stored Procedures after the upgrade and found that passing a parameter value to a WHERE statement no longer works. I have been researching this for 2 days, and not sure how to get around it. Can someone point me in the right direction for help! Thanks in advance....
Here is my code...
ALTER PROCEDURE [dbo].[USP_SelAccrualInfo] @begCompany varchar(300) = NULL,
@endCompany varchar (300) = NULL,
@begInvDate datetime = NULL,
@endInvDate datetime = NULL,
@begOrderDate datetime = NULL,
@endOrderDate datetime = NULL,
@div varchar(1)
AS
DECLARE @wherestring varchar (3000)
DECLARE @sql varchar (3000)
SET @wherestring = 'DataModesDaily.dbo.viewAccrualInfo.COMPANY >= "'+ @begCompany +'"'+' AND DataModesDaily.dbo.viewAccrualInfo.COMPANY <= "'+@endCompany +'"'
IF @begInvDate IS NOT NULL AND @endInvDate IS NOT NULL
BEGIN
SET @wherestring = @wherestring + ' AND DataModesDaily.dbo.viewAccrualInfo.INV_DATE >= "'+ CONVERT(varchar(10), @begInvDate, 101)+'"'+
' AND DataModesDaily.dbo.viewAccrualInfo.INV_DATE <= "' +CONVERT(varchar(10), @endInvDate, 101)+'"'
END
IF @begOrderDate IS NOT NULL AND @endOrderDate IS NOT NULL
BEGIN
SET @wherestring = @wherestring + ' AND DataModesDaily.dbo.viewAccrualInfRDER_DATE >= "' +CONVERT(varchar(10), @begOrderDate, 101)+'"'
+ ' AND DataModesDaily.dbo.viewAccrualInfRDER_DATE <= "'+CONVERT(varchar(10), @endOrderDate, 101) + '"'
END
SET @wherestring = @wherestring + ' AND DataModesDaily.dbo.viewAccrualInfo.DIV = "' + @div + '"'
SET @sql= ' DECLARE AccrualCursor CURSOR FOR' +
' SELECT DataModesDaily.dbo.viewAccrualInfo.COMPANY, DataModesDaily.dbo.viewAccrualInfo.B_LIST, DataModesDaily.dbo.viewAccrualInfo.INV_DATE,
DataModesDaily.dbo.viewAccrualInfo.INV_NO, DataModesDaily.dbo.viewAccrualInfo.SHIP_DATE,
DataModesDaily.dbo.viewAccrualInfo.CUST_PO_NO, DataModesDaily.dbo.viewAccrualInfRDER_NO, DataModesDaily.dbo.viewAccrualInfRDER_DATE,
DataModesDaily.dbo.viewAccrualInfo.CHARGES, DataModesDaily.dbo.viewAccrualInfo.PRICE_FACTOR, DataModesDaily.dbo.viewAccrualInfo.QTY,
DataModesDaily.dbo.viewAccrualInfo.ITEM_NO, DataModesDaily.dbo.viewAccrualInfo.PRICED, DataModesDaily.dbo.viewAccrualInfo.ACCOUNT,
DataModesDaily.dbo.viewAccrualInfo.STOCK_UNIT ' +
' FROM DataModesDaily.dbo.viewAccrualInfo' + ' WHERE' + ' ' + @wherestring + ' OPEN AccrualCursor'
print @wherestring
EXEC (@sql)
This is the value of @wherestring...
DataModesDaily.dbo.viewAccrualInfo.COMPANY >= "A" AND DataModesDaily.dbo.viewAccrualInfo.COMPANY <= "B" AND DataModesDaily.dbo.viewAccrualInfo.INV_DATE >= "10/01/2009" AND DataModesDaily.dbo.viewAccrualInfo.INV_DATE <= "10/31/2009" AND DataModesDaily.dbo.viewAccrualInfo.DIV = "1"
Thank you!
Here is my code...
ALTER PROCEDURE [dbo].[USP_SelAccrualInfo] @begCompany varchar(300) = NULL,
@endCompany varchar (300) = NULL,
@begInvDate datetime = NULL,
@endInvDate datetime = NULL,
@begOrderDate datetime = NULL,
@endOrderDate datetime = NULL,
@div varchar(1)
AS
DECLARE @wherestring varchar (3000)
DECLARE @sql varchar (3000)
SET @wherestring = 'DataModesDaily.dbo.viewAccrualInfo.COMPANY >= "'+ @begCompany +'"'+' AND DataModesDaily.dbo.viewAccrualInfo.COMPANY <= "'+@endCompany +'"'
IF @begInvDate IS NOT NULL AND @endInvDate IS NOT NULL
BEGIN
SET @wherestring = @wherestring + ' AND DataModesDaily.dbo.viewAccrualInfo.INV_DATE >= "'+ CONVERT(varchar(10), @begInvDate, 101)+'"'+
' AND DataModesDaily.dbo.viewAccrualInfo.INV_DATE <= "' +CONVERT(varchar(10), @endInvDate, 101)+'"'
END
IF @begOrderDate IS NOT NULL AND @endOrderDate IS NOT NULL
BEGIN
SET @wherestring = @wherestring + ' AND DataModesDaily.dbo.viewAccrualInfRDER_DATE >= "' +CONVERT(varchar(10), @begOrderDate, 101)+'"'
+ ' AND DataModesDaily.dbo.viewAccrualInfRDER_DATE <= "'+CONVERT(varchar(10), @endOrderDate, 101) + '"'
END
SET @wherestring = @wherestring + ' AND DataModesDaily.dbo.viewAccrualInfo.DIV = "' + @div + '"'
SET @sql= ' DECLARE AccrualCursor CURSOR FOR' +
' SELECT DataModesDaily.dbo.viewAccrualInfo.COMPANY, DataModesDaily.dbo.viewAccrualInfo.B_LIST, DataModesDaily.dbo.viewAccrualInfo.INV_DATE,
DataModesDaily.dbo.viewAccrualInfo.INV_NO, DataModesDaily.dbo.viewAccrualInfo.SHIP_DATE,
DataModesDaily.dbo.viewAccrualInfo.CUST_PO_NO, DataModesDaily.dbo.viewAccrualInfRDER_NO, DataModesDaily.dbo.viewAccrualInfRDER_DATE,
DataModesDaily.dbo.viewAccrualInfo.CHARGES, DataModesDaily.dbo.viewAccrualInfo.PRICE_FACTOR, DataModesDaily.dbo.viewAccrualInfo.QTY,
DataModesDaily.dbo.viewAccrualInfo.ITEM_NO, DataModesDaily.dbo.viewAccrualInfo.PRICED, DataModesDaily.dbo.viewAccrualInfo.ACCOUNT,
DataModesDaily.dbo.viewAccrualInfo.STOCK_UNIT ' +
' FROM DataModesDaily.dbo.viewAccrualInfo' + ' WHERE' + ' ' + @wherestring + ' OPEN AccrualCursor'
print @wherestring
EXEC (@sql)
This is the value of @wherestring...
DataModesDaily.dbo.viewAccrualInfo.COMPANY >= "A" AND DataModesDaily.dbo.viewAccrualInfo.COMPANY <= "B" AND DataModesDaily.dbo.viewAccrualInfo.INV_DATE >= "10/01/2009" AND DataModesDaily.dbo.viewAccrualInfo.INV_DATE <= "10/31/2009" AND DataModesDaily.dbo.viewAccrualInfo.DIV = "1"
Thank you!