Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

I Have Problem when execute Stored Procedure


when i execute the following code I get the right result :

SQL
ALTER PROCEDURE dbo.Selectplandate
	(
	@plandatefrom datetime,
	@plandateto datetime
	
	)
AS
SELECT Distinct CONVERT(datetime,plandate,103) as [Date]
FROM      plannedgangs
WHERE (plandate between @plandatefrom AND @plandateto )


but when I use this code i got wrong result : the result is null

SQL
ALTER PROCEDURE dbo.Selectplandate
	(
	@plandatefrom datetime,
	@plandateto datetime
	
	)
AS

DECLARE @plandate datetime

SET @plandate =
(
SELECT Distinct CONVERT(datetime,plandate,103) as [Date]
FROM      plannedgangs
WHERE (plandate between @plandatefrom AND @plandateto )
)
SELECT @plandate as [Date]
Posted

My guess is that the SQL statement returns more than one row. A scalar variable can contain only one value. Use a Table variable instead.

SQL
DECLARE @t1 TABLE([date] DATETIME);
INSERT INTO @t1 ([date]) SELECT DISTINCT plandate AS [Date]
  FROM plannedgangs
  WHERE (plandate BETWEEN @plandatefrom AND @plandateto);
SELECT [Date] FROM @t1;


Assuming that plandate in plannedgangs is a DateTime Data Type, you do not need the CONVERT function. If it is not a DateTime Data Type, then the between clause will not work correctly.
 
Share this answer
 
v8
Comments
shms_rony 24-Aug-13 7:36am    
yes that is right :)
first code get all rows but the second code get first row only

but how can i get all rows in variable to use it in other statement ??
Mike Meinz 24-Aug-13 7:48am    
See revised Solution 1.
I removed CONVERT function because it is not needed.
In solution of this question: scalar variable and any type of variable always store must be a single value at time and when you working with store procedure select statement it's returns more then one value because of that you have to declare and use table variable instead of scalar variable.

SQL
SELECT Distinct CONVERT(datetime,plandate,103) as [Date]
into #tmp
FROM      plannedgangs
WHERE (plandate between @plandatefrom AND @plandateto )

select * from #tmp

drop table #tmp



Thank you......:-)
 
Share this answer
 

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