日期:2014-05-20 浏览次数:20594 次
select * from table where shopname in (textbox1.text.trim) and farename in (textbox2.text.trim)
------解决方案--------------------
select * from tableName where [shopname] in (0001,0002) and [farename] in ('促销费','进店费')
select * from tableName where [shopname] in (0001,0002) or [farename] in ('促销费','进店费')
------解决方案--------------------
string[] shopArr= textbox1.split(',');
string[] fareArr = textbox2.split(',');
string shops="";
string fares="";
for(int i=0;i< shopArr.Length;i++)
{
if(i==0) shops+="'"+ shopArr [i]+"'";
else shops+=",'"+ shopArr [i]+"'";
}
for(int i=0;i< fareArr .Length;i++)
{
if(i==0) fares +="'"+ fareArr [i]+"'";
else fares +=",'"+ fareArr [i]+"'";
}
拼Sql语句
"select * from 表 where shopName in("+ shops+") and farename in("+ fares +")"
------解决方案--------------------
string[] array1=textbox1.text.trim.tostring().split(",")
string[] array2=textbox1.text.trim.tostring().split(",")
select * from table where shopname ='"+array1[0].tostring+"' and farename ='"+array2[0].tostring+"'
union
select * from table where shopname ='"+array1[1].tostring+"' and farename ='"+array2[1].tostring+"'
不过这种方式不是很灵活,当array1的长度与array1的长度不一样时
------解决方案--------------------
select * from tableName where [shopname] in (0001,0002) and [farename] in ('促销费','进店费')
select * from tableName where [shopname] in (0001,0002) or [farename] in ('促销费','进店费')
------解决方案--------------------
drop function Split
go
CREATE FUNCTION [Split]
(
@sText varchar(8000), --分割目标串
@sDelim varchar(20) = ' '--分割字符串
)
RETURNS @retArray TABLE --返回分割后的串数据表
(
idx int Primary Key, --主键
Evalue varchar(8000)--值
)
AS
BEGIN
declare @idx int
declare @value varchar(8000)
declare @bcontinue bit
declare @iStrike int
declare @iDelimlength tinyint
if @sDelim = 'Space'
BEGIN
SET @sDelim = ' '
END
SET @idx = 0
SET @sText = LTrim(RTrim(@sText))
SET @iDelimlength = len(@sDelim)
SET @bcontinue = 1
IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty')) --如果分割串不为空时
BEGIN
WHILE @bcontinue = 1
BEGIN
IF CHARINDEX(@sDelim, @sText)>0 --如果在@sText是找到分割字符@sDelim的位置则将第一个元素插入表
BEGIN
SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1)
INSERT @retArray (idx, Evalue) VALUES (@idx, @value)
SET @iStrike = len(@value) + @iDelimlength--增加@idex值并 取回下一个目标串
SET @idx = @idx + 1
SET @sText = LTrim(Right(@sText,len(@sText) - @iStrike))
END
ELSE --如果在@sText里找不到@sDelim时,说明@sDelim已经不能再分割了,将@sText插入返回表@retArray
BEGIN
SET @value = @sText
INSERT @retArray (idx, Evalue) VALUES (@idx, @value)
SET @bcontinue = 0--设置退出循环标识
END
END
END
ELSE
BEGIN
WHILE @bcontinue=1
BEGIN
IF len(@sText)>1 --如果分割字符为空串时,将字符串中每个字符插入@retArray
BEGIN
SET @value = SUBSTRING(@sText,1,1)
INSERT @retArray (idx, Evalue) VALUES (@idx, @value)
SET @idx = @idx+1
SET @sText = SUBSTRING(@sText,2,len(@sText)-1)
END
ELSE
BEGIN --插入字符并设置退出while标识
INSERT @retArray (idx, Evalue) VALUES (@idx, @sText)
SET @bcontinue = 0
END
END
END
RETURN
END
go
--你的存储过程
if exists (select * from sysobjects where id = object_id(N'Search_Pro') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure Search_Pro
go
create procedure Search_Pro
@idvalues varchar(500),
@farenameValues varchar(500)
as begin
declare @SQL varchar(1000)
set @SQL = 'select * from yourTableName where 1=1 '
set @SQL = @SQL + ' and (1<>1 '
-----------------------------------------
declare @idvalues_id int
declare @ShopName_Text nvarchar(100)
declare cursor_ShopName_Text cursor for select idx from Split(@idvalues,',') order by idx ASC
open cursor_ShopName_Text
fetch cursor_ShopName_Text into @idvalues_id
while @@fetch_status=0
begin
if exists(select * from Split(@idvalues,',') where idx=@idvalues_id)
begin
set @ShopName_Text=(select Evalue from Split(@idvalues,',') where idx=@idvalues_id)--去掉","后的号码
set @SQL = @SQL + ' or shopname ='''+@ShopName_Text+''''
end
fetch cursor_ShopName_Text into @idvalues_id
end
close cursor_ShopName_Text
deallocate cursor_ShopName_Text
set @SQL = @SQL + ' ) and ( 1<>1 '
------------------------------------------
declare @farenameValues_id int
declare @farename_Text nvarchar(100)
declare cursor_farename_Text cursor for select idx from Split(@farenameValues,',') order by idx ASC
open cursor_farename_Text
fetch cursor_farename_Text into @farenameValues_id
while @@fetch_status=0
begin
if exists(select * from Split(@farenameValues,',') where idx=@farenameValues_id)
begin
set @farename_Text=(select Evalue from Split(@farenameValues,',') where idx=@farenameValues_id)--去掉","后的号码
set @SQL = @SQL + ' or farename ='''+@farename_Text+''''
end
fetch cursor_farename_Text into @farenameValues_id
end
close cursor_farename_Text
deallocate cursor_farename_Text
set @SQL = @SQL + ' ) '
------------------------------------------
exec(@SQL)
print @SQL
end
go