当前位置: 代码迷 >> Sql Server >> 帮忙,小疑点,解决立即给分
  详细解决方案

帮忙,小疑点,解决立即给分

热度:55   发布时间:2016-04-27 20:51:46.0
帮忙,小问题,解决立即给分!
下面的sql总是说
@sql= '发生时间> = '[email protected]+ '   and   发生时间 < '[email protected]       行有错
@sql+= '起点桩号= '[email protected]+ '   and   止点桩号= '[email protected]       行有错
@sql+= '发生时间> = '[email protected]+ '   and   发生时间 < '[email protected]+ '   and   起点桩号= '[email protected]+ '   and   止点桩号= '[email protected]   行有错


--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= '发生时间> = '[email protected]+ '   and   发生时间 < '[email protected]
end
print   @sql
if(@state= '2 ')
begin
  @sql+= '起点桩号= '[email protected]+ '   and   止点桩号= '[email protected]
end
if(@state= '0 ')
begin
  @sql+= '发生时间> = '[email protected]+ '   and   发生时间 < '[email protected]+ '   and   起点桩号= '[email protected]+ '   and   止点桩号= '[email protected]
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= '发生时间> = '[email protected]+ ' and 发生时间 < '[email protected] --使用set赋值,下同
end
print @sql
if(@state= '2 ')
begin
set @[email protected]+ '起点桩号= '[email protected]+ ' and 止点桩号= '[email protected]
end
if(@state= '0 ')
begin
set @[email protected]+ '发生时间> = '[email protected]+ ' and 发生时间 < '[email protected]+ ' and 起点桩号= '[email protected]+ ' and 止点桩号= '[email protected]
end
print @sql
------解决方案--------------------

set @sql= @sql + '发生时间> = '[email protected]+ ' and 发生时间 < '[email protected]

------解决方案--------------------
or
select @sql= @sql + '发生时间> = '[email protected]+ ' and 发生时间 < '[email protected]

.........

  相关解决方案