SQL语句是否可以在简单

look_007 2010-08-27 08:45:52


declare @qz int,@wz int,@rq datetime,@bch char(4)

/*@qz 启始座位
@wz 结束座位
@rq 日期
@bch 班次号
*/
if @rq>=convert(char(10),getdate() ,102)
begin
declare @bj int
select @bj=patindex('%1%',substring(zw,@qz,@wz)) from zw where bch=@bch and bcrq=@rq
if @bj!=0
begin
select '存在售票,不予更改' as '提示' ,bch,zw from zw where bch=@bch and bcrq=@rq
return
end
else
begin
select '改之前' as '状态',bch,zw,bcrq,clbh from zw where bch=@bch and bcrq=@rq
select * from spqk where bch=@bch and bcrq=@rq
declare @ws int,@tj char(20)
declare @zs char(10),@sy char(10)
select @ws=len(zw) from zw where bch=@bch and bcrq=@rq
if @qz>@ws
begin
select @tj=@wz-@ws
update zw set zw=zw+replicate('3',@tj) where bch=@bch and bcrq=@rq
while @qz<=@wz
begin
update zw set zw=stuff(zw,@qz,1,'0') where bcrq=@rq and bch=@bch
select @qz=@qz+1
end
end
else
while @qz<=@wz
begin
update zw set zw=stuff(zw,@qz,1,'0') where bcrq=@rq and bch=@bch
select @qz=@qz+1
end
end
select @zs= (len(zw)-len(replace(zw,'0',''))+len(zw)-len(replace(zw,'1',''))) ,@sy=len(zw)-len(replace(zw,'0','')) from zw where bcrq=@rq and bch=@bch
update spqk set syps=@sy ,zsps=@zs where bch=@bch and bcrq=@rq
end
else
begin
print'日期不对,Today is '+convert(char(10),getdate(),102)
return
end

select '改之后' as '状态',bch,zw,bcrq,clbh from zw where bch=@bch and bcrq=@rq
----select '统计' as '统计', (len(zw)-len(replace(zw,'0',''))+len(zw)-len(replace(zw,'1',''))) as '准售',len(zw)-len(replace(zw,'0','')) as '剩余票',len(zw)-len(replace(zw,'1','')) as '已售' from zw where bch=@bch and bcrq=@rq
select * from spqk where bch=@bch and bcrq=@rq


各位高手,请问以上的代码是否可以在简单的。这样写虽然也能得到结果,但看起来很麻烦。
...全文
64 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuifengde 2010-08-27
  • 打赏
  • 举报
回复
我认为还有改进的空间,比如取数据,循环中,具体要看表及要实现的业务
「已注销」 2010-08-27
  • 打赏
  • 举报
回复
贴出你的相关表结构,列出你想要的结果,配合给一些测试数据,这样方便大家帮你解决,而不是要大家先分析你的现有sql逻辑,ok?
duanzhi1984 2010-08-27
  • 打赏
  • 举报
回复
语句并不会因为复杂,而速度减慢。。

只要思路清晰

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧