多个sql语句读取信息

小土bibox 2011-12-08 03:12:37
根据邮件地址查注册用户使用的名字名;
其中。a 的count是没法确定的所以不知道该怎么用存储过程。
List<string> selectinfo = new List<string>();
for (int i = 0; i < a.Count; i++) {
string sqlstr = string.Format("select fldloginName as name from tblUser where fldEmail='{0}'",a[i][0]);
selectInfo.add(sqlstr);
}

问: 现在组织好这么多的sql语句,结果集要返回给跟selectInfo.count一样多行的记录。
查询无此记录的返回null,或者""都行

这个sql 该怎么组织,要根据结果如果数据库里没返回结果。就要移除a里面的对应的Index的项,

该用啥写。把我给拷到了。忘大虾给出思路。给出代码。给出建议。在此先谢过
...全文
124 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
小土bibox 2011-12-08
  • 打赏
  • 举报
回复
功能通过测试。谢谢
唐诗三百首 2011-12-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 black2bi 的回复:]
如果查询的结果为null.可不可以让结果为""呢。谢谢
[/Quote]

create proc black2bi(@intput varchar(2000))
as
begin
declare @sql varchar(2000)
create table #tab (fldEmail varchar(200))
select @sql='insert into #tab select '''+replace(@intput,',',''' union all select ''')+''' '
exec(@sql)

select a.fldEmail,isnull(b.fldloginName,'') fldloginName
from #tab a
left join tblUser b
on a.fldEmail=b.fldEmail

drop table #tab
end
唐诗三百首 2011-12-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 black2bi 的回复:]
如果查询的结果为null.可不可以让结果为""呢。谢谢
[/Quote]

create proc black2bi(@intput varchar(2000))
as
begin
declare @sql varchar(2000)
create table #tab (fldEmail varchar(200))
select @sql='insert into #tab select '''+replace(@intput,',',''' union all select ''')+''' '
exec(@sql)

select a.fldEmail,isnull(b.fldloginName,'') fldloginName
from tab a
left join tblUser b
on a.fldEmail=b.fldEmail
end
小土bibox 2011-12-08
  • 打赏
  • 举报
回复
如果查询的结果为null.可不可以让结果为""呢。谢谢
唐诗三百首 2011-12-08
  • 打赏
  • 举报
回复
前端把各个邮件地址串接起来(以逗号分隔),作为输入参数,调用以下存储过程即可.

create proc black2bi(@intput varchar(2000))
as
begin
declare @sql varchar(2000)
create table #tab (fldEmail varchar(200))
select @sql='insert into #tab select '''+replace(@intput,',',''' union all select ''')+''' '
exec(@sql)

select a.fldEmail,b.fldloginName
from tab a
left join tblUser b
on a.fldEmail=b.fldEmail
end
小土bibox 2011-12-08
  • 打赏
  • 举报
回复
在问一下。如果查询出中间的一列没有值,改怎么弄呢


select fldloginName as name from tblUser where fldEmail='5065@qq.com' union all select fldloginName as name from tblUser where fldEmail='60483506' union all select fldloginName as name from tblUser where fldEmail='5065@qq.com'


name
admin
admin

结果指出来两个。而中间的那个语句的结果没了。

希望
-晴天 2011-12-08
  • 打赏
  • 举报
回复
在循环中,将查询语句字串连接合并(每两句间加一个" union all "),循环结束后一次执行查询.

22,302

社区成员

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

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