请高手帮忙优化一下这条SQL语句

来源:百度知道 编辑:UC知道 时间:2024/06/15 04:28:54
有两张表,都是一连串的数字,现在想查出这两张表,A表中数字前面有包含B表中的数字的,就挑出来到一张新的表.

A表样式:
10629625
8826512201248
10325335752
10629625123
103253353752
……
B表样式
10629
103253
88265122
……

我的语句是这样的select a.col001 into C from A a,B where left(a.col002,len(B.col001))=B.col001
,不过数据比较大,执行起来很慢,有没有高手帮一下要怎样优化这条语句,谢谢了

用存储过程。。。。

写一个函数.
把A表,循环用函数对比就比你哪个快多了!

use tempdb
create table a(a bigint)
create table b(b bigint)

insert into a
select 10629625 union all
select 8826512201248 union all
select 10325335752 union all
select 10629625123 union all
select 103253353752

insert into b
select 10629 union all
select 103253 union all
select 88265122

select * from a
select * from b

select distinct cast(b.b as varchar) into c from a,b where cast(a.a as varchar) like (cast(b.b as varchar) + '%')
select * from c

要用到储存过程和游标(有小小难度)
if exists(select * from sysobjects where name='aa' and type='p')
drop proc aa
go
create proc aa
as
declare @s varchar(20)
declare t cursor for
select n from b
open t
fetch next from t
into @s
--set @s=rtrim(@s)+'%'

select * from a where N like rtrim(