是这样的:
   我这个asp是实现对两个表的查询。有一个客户输入框,输入要查询的内容。可以实现输入空格。如果有空格输入,那么就截取各个空格之间的字符串进行查询,(例如说客户输入(str1 str2)那么sql语句为:where str1 like NewField1 and srt2 like NewField1),NewField1为两个表所有字段加在一起的字段。
下面我把asp代码粘出来,求把代码装换成存储过程怎么实现?
dim key
 key = request.Form("keyword")
 if  key=""  then 
 response.write "请输入字符串"
 else
  dim j
  dim pos
  dim a(255)
  dim k
  dim i
  dim str
  pos=1
  k=0
  i=1
  j=1
  dim strsql1
  dim strsql2
  dim strsql3
   pos=instr(i,key," ")
   if pos<>0 then
 strsql1 = "select  a.autoid,a.weburl,a.companyname,(a.CompanyName+'<br>'+(case when a.Address is null then '' else a.Address end)" &_
"+' '+(case when a.TelePhone is null then '' else a.TelePhone end)" &_
"+' '+(case when a.fax is null then '' else a.fax end)" &_
"+' '+(case when a.email is null then '' else a.email end)" &_
"+' '+(case when a.weburl is null then '' else a.weburl end)" &_
"+' '+(case when a.postcode is null then '' else a.postcode end)" &_
"+' '+(case when a.person is null then '' else a.person end)" &_
"+' '+(case when a.produceability is null then '' else a.produceability end)" &_
"+' '+cast(a.export as nvarchar)" &_
"+' '+(case when a.masterproduce is null then '' else a.masterproduce end)+' '+(case when a.memo is null then '' else a.memo end)) ggg from(" &_
"select  autoid,companyname,weburl,newfield1 from (" &_
"select a.AutoID,a.companyname,a.weburl,b.customerid," &_
"(a.CompanyName + (case when a.Address is null then '' else a.Address end)+" &_
"(case when a.TelePhone is null then '' else a.TelePhone end) + (case when a.fax is null then '' else a.fax end)+" &_
"(case when a.email is null then '' else a.email end)+" &_
"(case when a.weburl is null then '' else a.weburl end)+" &_
"(case when a.postcode is null then '' else a.postcode end)+" &_
"(case when a.person is null then '' else a.person end)+" &_
"(case when a.produceability is null then '' else a.produceability end)+" &_
"cast(a.export as nvarchar)+(case when a.masterproduce is null then '' else a.masterproduce end)+" &_
"(case when a.memo is null then '' else a.memo end)+(case when b.memo is null then '' else b.memo end)) " &_
"as NewField1,b.memo from customers a left join phonehistorydatum b on a.autoid=b.customerid ) NewTable where "  
do while(pos<>len(key))
pos=instr(i,key," ")   //设I=1
//那么pos得到的是空格的位置,pos-I就是第1个字符串了
if pos=0 then
a(k)=mid (key,i,len(key))
pos=len(key)
else
a(k)=mid (key,i,pos-i)
end if
'response.Write (a(k))
if j=1 then
strsql2="NewTable.NewField1 like '%"+a(k)+"%'"
else
strsql2=strsql2+" and NewTable.NewField1 like '%"+a(k)+"%'"
end if
i=pos+1
k=k+1
j=j+1
loop
strsql3=" group by autoid,companyname,weburl,newfield1" &_
")nt left join customers a on nt.autoid=a.autoid group by a.autoid,a.CompanyName,a.Address,a.TelePhone,a.fax,a.email,a.weburl,a.postcode,a.person," &_
"a.produceability,a.export,a.masterproduce,a.memo"
strsql1=strsql1+strsql2+strsql3

解决方案 »

  1.   

    曾经有那个比较经典的
    select * from table1 where 1=1语句
    动态的去增加条件
      

  2.   

    我只是想把我写的这些sql语句的拼接,转换成存储过程!!!求一个存储过程!!
      

  3.   

    create proc test
    (
     inputparam1 dataType(size),
    inputparam2 dataType(size),
    ...)
    as
    declare @temp table(@tt int)--dim ==declare
    set @temp=N'select * from'+ @temp
    --where (条件判断的和ASP一样可以直接用case when then ) 
    连接字符用+号,循环
    while 条件
    begin  
    循环体
    //
    end
    字符长度的用LEN,SUBSTRING()查下帮助就可以。
    存储过程没什么难得,和SQL一样,慢慢来
      

  4.   

    do   while(pos <> len(key)) 
    pos=instr(i,key,"   ")       //设I=1 
    //那么pos得到的是空格的位置,pos-I就是第1个字符串了 
    if   pos=0   then 
    a(k)=mid   (key,i,len(key)) 
    pos=len(key) 
    else 
    a(k)=mid   (key,i,pos-i) 
    end   if 
    'response.Write   (a(k)) 
    if   j=1   then 
    strsql2="NewTable.NewField1   like   '%"+a(k)+"%'" 
    else 
    strsql2=strsql2+"   and   NewTable.NewField1   like   '%"+a(k)+"%'" 
    end   if 
    i=pos+1 
    k=k+1 
    j=j+1 
    loop 
    这部分我用到 数组了,在存储过程中不能用数组,怎么办?最好能帮我写出完整的存储过程,谢谢各位高手!!