高手快来,求一条sql.

luwenshuo 2007-03-16 05:12:10
如果有一表A
Table name:A
Column :userID,countryCode,SMS

其中userID和countryCode为联合主键.现在这个表中有这样的数据:

userID countryCode SMS
aa@a.com HK 259991
aa@a.com MY 445588
ss@ww.com MY 445588
............

我想用一条sql语句search出来
userID=aa@a.com and countryCode=MY这条记录之后的所有记录出来

应该怎样写?

注:database为sql2000
...全文
256 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
luwenshuo 2007-03-19
  • 打赏
  • 举报
回复
我试过这样,把所有的sql statement都存储在一个变量中再去执行,就像mengmou说的一样,但是因为我的"select id=identity(int,1,1),* into tmp from OtherTable" 这个OtherTable也是一条sql Statement产生的虚表,而且生成这个OtherTable的sql Statement中有用到getDate()函数,所以就运行不了,经过测试如果生成OtherTable的sql Statement中如果没有使用函数的情况下可以执行成功.

但是生成这个OtherTable的sql Statement是从另一张table中select出来的.存在一个变量中了.也不能修改这个sqlStatement.应该怎样解决?
mengmou 2007-03-19
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into tmp from exec(select sqlStatement from OtherTable)
但是这样的语法在sql2000中不允许.改怎么办?
--------------
--试试
exec('select id=identity(int,1,1),sqlStatement into tmp from OtherTable')
luwenshuo 2007-03-19
  • 打赏
  • 举报
回复
up up
luwenshuo 2007-03-19
  • 打赏
  • 举报
回复
我又把创建了一个临时表,存放select出来的data.
这样一来这个SP中就使用了2个临时表.看上去速度也不慢.hehe
问题算是解决了.


给分HOHOHO.
luwenshuo 2007-03-16
  • 打赏
  • 举报
回复
事实上表A也是虚拟的,
表A的数据是从另一个table中的一个column中取出来的sql执行之后产生的.
例如:
exec(select sqlStatement from OtherTable)

如果使用临时表,我将使用类似这样的sql

select id=identity(int,1,1),* into # from exec(select sqlStatement from OtherTable)
但是这样的语法在sql2000中不允许.改怎么办?
mengmou 2007-03-16
  • 打赏
  • 举报
回复
对啊,他要的是在数据库中的原始顺序
---------
这个原始顺序是指?
luwenshuo 2007-03-16
  • 打赏
  • 举报
回复
我只是怕用临时表效率不高.hehe
请问用临时表效率高还是用表变量效率高?
jacobsan 2007-03-16
  • 打赏
  • 举报
回复
对啊,他要的是在数据库中的原始顺序
mengmou 2007-03-16
  • 打赏
  • 举报
回复
mengmou()mengmou() ( )
如果数据是这样呢?
insert A(userID,countryCode,SMS)
select 'aa@a.com','HK','259991' union all
select 'aa@a.com','MY','445588' union all
select 'a@a.com','AY','445588'
------------------------
你order by一下,我的结果与order by是一致的。
jacobsan 2007-03-16
  • 打赏
  • 举报
回复
为什么不能用临时表?
用存储过程封装以后前台调用也是一句sql语句啊
jacobsan 2007-03-16
  • 打赏
  • 举报
回复
mengmou()mengmou() ( )
如果数据是这样呢?
insert A(userID,countryCode,SMS)
select 'aa@a.com','HK','259991' union all
select 'aa@a.com','MY','445588' union all
select 'a@a.com','AY','445588'

你那样只能针对字段本身就有序的情况
mengmou 2007-03-16
  • 打赏
  • 举报
回复
--创建测试环境
create table A(userID varchar(20),countryCode varchar(10),SMS int)

--插入测试数据
insert A(userID,countryCode,SMS)
select 'aa@a.com','HK','259991' union all
select 'aa@a.com','MY','445588' union all
select 'ss@ww.com','MY','445588'

--求解过程
select * from A _a
where userID > 'aa@a.com' or (userID = 'aa@a.com'and countryCode > 'MY')

--删除测试环境
drop table A

/*--测试结果
userID countryCode SMS
-------------------- ----------- -----------
ss@ww.com MY 445588

(所影响的行数为 1 行)

*/

luwenshuo 2007-03-16
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into # from A
select userID,countryCode,SMS from # where id>(select max(id) from # where userID=@userID and countryCode=@country)

你是说这样吗?用@userID和@countryCode来代替hardcode的内容?
但这样写也用到了临时表对吗(select * in to #**)
jacobsan 2007-03-16
  • 打赏
  • 举报
回复
luwenshuo() ( ) 信誉:100 Blog 2007-03-16 17:27:04 得分: 0


请问可以不使用临时表来完成这种search吗?


------
建立一个存储过程,userID,countryCode作为两个参数传入,前台调用这个存储过程
luwenshuo 2007-03-16
  • 打赏
  • 举报
回复
to mengmou()mengmou() :

比如userID=aa@a.com and countryCode=MY这条记录在table的第2行,我就想取出整个table中从第二行开始到table最后一行的记录,但是userID=aa@a.com and countryCode=MY不确定在哪一行.
luwenshuo 2007-03-16
  • 打赏
  • 举报
回复
请问可以不使用临时表来完成这种search吗?
jacobsan 2007-03-16
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into # from A
select userID,countryCode,SMS from # where id>(select max(id) from # where userID='aa@a.com' and countryCode='MY')
mengmou 2007-03-16
  • 打赏
  • 举报
回复
userID=aa@a.com and countryCode=MY这条记录之后
-----
不明确
jacobsan 2007-03-16
  • 打赏
  • 举报
回复
userID和countryCode是无序的话要借助临时表
luwenshuo 2007-03-16
  • 打赏
  • 举报
回复
我是想要取出userID='aa@a.com' and countryCode='MY' A表中这条记录之后的所有记录.
加载更多回复(1)

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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