I have a stored procedure that takes two parameters as varchar(50). The stored procedure does a few simple queries into a temp table and the returns the result set from the temp table (I removed the actual queries)
ALTER PROCEDURE [dbo].[cv_GetBooks]
@bookNumber as varchar(50),
@bookDate as varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--a few select statements
if @ismultiple = '0'
begin
select * from books where bookNumber = @bookNumber
and bookDate = @bookDate
and Bookname is not null
end
ELSE
Begin
select * into #temp from books
where bookNumber = @bookNumber
and bookDate = @bookDate
and Bookname is null
select * from books
where bookauthor not in (select bookauthor from #temp)
and bookNumber= @bookNumber
and bookDate= @bookDate
drop table #temp
end
END
I have this query installed on my local development machine on SQL Server 2008. I have also installed it on a two test machines running Windows Server 2003 and SQL Server 2008. The stored procedure has been working as expected.
cv_GetBooks '012345678', '06062012' --returns result set as expected
I recently moved it to a test server in another remote environment that is running Windows server 2008 and SQL Server 2008 R2. The stored procedure no longer works as expected. After running SQL Profiler I have confirmed that the same code is being executed:
cv_GetBooks '012345678', '06062012' --run on SQL server 2008 r2 returns nothing
When I removed the quotes from the query I got the expected result:
cv_GetBooks 012345678, 06062012 --run with out quotes server returns expected result set
I have since installed the same stored procedure on local version of SQL Server 2008 R2 and everything is running as expected, with the literal string quotes in place, as in the first example.
At first I thought it was an escape issue with the passed parameters but that doesn’t seem correct because the passed values are do not contain any single quotes.
Having installed and had it working on so many environments, I am under the impression that this is maybe a setting in SQL Server that I am unaware of.
What could be causing the stored procedure to not return the result set with the string literals in place on the SQL Server 2008 r2 instance, but work correctly with out them there?
You did not post the table definition of table Books, but
could be caused if BookNumber and BookDate were numeric rather than varchar:
Leading zero in ints is dropped, and when converted to varchar the resulting string does not contain the leading zero.
It’s also not clear how the data in your table affects the execution (IF statement!) in your code.