日期:2014-05-19  浏览次数:20532 次

帮忙,小问题,解决立即给分!
下面的sql总是说
@sql= '发生时间> = '+@sdate+ '   and   发生时间 < '+@edate       行有错
@sql+= '起点桩号= '+@sposition+ '   and   止点桩号= '+@eposition       行有错
@sql+= '发生时间> = '+@sdate+ '   and   发生时间 < '+@edate+ '   and   起点桩号= '+@sposition+ '   and   止点桩号= '+@eposition   行有错


--CREATE   PROCEDURE   Accident   @state   tinyint,@sdate   datetime,@edate   datetime,@sposition   numeric,@eposition   numeric
--   AS

--set   nocount   on
declare   @state   tinyint(1)
declare   @sdate   datetime
declare   @edate   datetime
declare   @sposition   numeric
declare   @eposition   numeric

declare   @sql   nvarchar(1000)
set   @state= '1 '
set   @sdate= '2006-11-22 '
set   @edate= '2006-11-24 '
set   @sposition= '149.560 '
set   @eposition= '168.360 '


set   @sql= 'select   起点桩号,止点桩号,发生时间,等级编号   from   jtsg   where   '

if   (@state= '1 ')
begin
@sql= '发生时间> = '+@sdate+ '   and   发生时间 < '+@edate
end
print   @sql
if(@state= '2 ')
begin
  @sql+= '起点桩号= '+@sposition+ '   and   止点桩号= '+@eposition
end
if(@state= '0 ')
begin
  @sql+= '发生时间> = '+@sdate+ '   and   发生时间 < '+@edate+ '   and   起点桩号= '+@sposition+ '   and   止点桩号= '+@eposition
end
print   @sql




------解决方案--------------------
--CREATE PROCEDURE Accident @state tinyint,@sdate datetime,@edate datetime,@sposition numeric,@eposition numeric
-- AS

--set nocount on
declare @state tinyint(1)
declare @sdate datetime
declare @edate datetime
declare @sposition numeric
declare @eposition numeric

declare @sql nvarchar(1000)
set @state= '1 '
set @sdate= '2006-11-22 '
set @edate= '2006-11-24 '
set @sposition= '149.560 '
set @eposition= '168.360 '


set @sql= 'select 起点桩号,止点桩号,发生时间,等级编号 from jtsg where '

if (@state= '1 ')
begin
set @sql= '发生时间> = '+@sdate+ ' and 发生时间 < '+@edate --使用set赋值,下同
end
print @sql
if(@state= '2 ')
begin
set @sql=@sql+ '起点桩号= '+@sposition+ ' and 止点桩号= '+@eposition
end
if(@state= '0 ')
begin
set @sql=@sql+ '发生时间> = '+@sdate+ ' and 发生时间 < '+@edate+ ' and 起点桩号= '+@sposition+ ' and 止点桩号= '+@eposition
end
print @sql
------解决方案--------------------

set @sql= @sql + '发生时间> = '+@sdate+ ' and 发生时间 < '+@edate

------解决方案--------------------
or
select @sql= @sql + '发生时间> = '+@sdate+ ' and 发生时间 < '+@edate

.........