求这种SQL用存储过程怎么写!(80分)

  • 主题发起人 主题发起人 kantaguai
  • 开始时间 开始时间
K

kantaguai

Unregistered / Unconfirmed
GUEST, unregistred user!
SQLALL:='';<br>&nbsp; if edit_cate.Text&lt;&gt;'' then<br>&nbsp; &nbsp; arrstr[1] := '(category like'''+edit_cate.Text+'%'+''')';<br>&nbsp; if edit_custname.Text&lt;&gt;'' then<br>&nbsp; &nbsp; arrstr[2] := '(company like'''+'%'+edit_custname.Text+'%'+''')';<br>&nbsp; if edit_address.Text&lt;&gt;'' then<br>&nbsp; &nbsp; arrstr[3] := '(address like'''+'%'+edit_address.Text+'%'+''')';<br>&nbsp; for i:=1 to 3 do<br>&nbsp; &nbsp; if arrstr&lt;&gt;'' then<br>&nbsp; &nbsp; &nbsp; SQLAll := SQLAll + 'and' +arrstr;<br>&nbsp; SQLStr := 'select * from [custom] where ';<br>&nbsp; with frm_data.Qry_cust do<br>&nbsp; begin<br>&nbsp; &nbsp; close;<br>&nbsp; &nbsp; SQL.Text := SQLStr;<br>&nbsp; &nbsp; Open;<br>&nbsp; end;
 
sqlwhere:=' where 1=1 ';<br>if edit_cate.Text&lt;&gt;'' then<br>&nbsp;sqlwhere := sqlwhere+' and category like '+Quotedstr('%'+edit_cate.Text+'%');<br>if edit_custname.Text&lt;&gt;'' then<br>sqlwhere := sqlwhere+' and company like '+Quotedstr('%'+edit_custname.Text+'%');<br>if edit_address.Text&lt;&gt;'' then<br>sqlwhere := sqlwhere+' and address like '+Quotedstr('%'+edit_address.Text+'%');<br>&nbsp; with frm_data.Qry_cust do<br>&nbsp; begin<br>&nbsp; &nbsp; disablecontrols;<br>&nbsp; &nbsp; close;<br>&nbsp; &nbsp; SQL.Text := 'select * from custom '+sqlwhere;<br>&nbsp; &nbsp; Open;<br>&nbsp; &nbsp; enablecontrols;<br>&nbsp; end;
 
我想知道这种查询在存储过程里面怎么写啊
 
在存储过程中加三个IN参数,分别对应你的三个EDIT。<br><br>在存储教程中判断方式和上面的基本相同。
 
最好不要用存儲過程,這樣就會寫限制就很大了<br>還不如使用query控件,動態組合條件生成sql語句來得方便、強大
 
CREATE &nbsp;Procedure dbo.mypro<br> /* Param List */<br> @category nvarchar(100),<br> @company nvarchar(100), <br> @address nvarchar(100) <br>&nbsp; &nbsp;<br>AS<br><br>DECLARE @StrSql varchar(4000)<br>SET @StrSql = '[custom] where 1=1'<br>IF @category&lt;&gt;''<br> SET @StrSql=@StrSql+' &nbsp;AND category='''+@category+''''<br>IF @company&lt;&gt;''<br> SET @StrSql=@StrSql+' &nbsp;AND company='''+@company+''''<br>IF @address&lt;&gt;''<br>&nbsp;SET @StrSql =@StrSql+' AND address='''+@address+''''<br><br>&nbsp;PRINT @StrSql<br>&nbsp;EXEC (@StrSql)<br>go
 

Similar threads

后退
顶部