读书人

sql存储过程(SOS),该如何解决

发布时间: 2012-12-19 14:13:14 作者: rapoo

sql存储过程(SOS)
把两条sql当参数传入存储过程@sql1,@sql2
执行EXEC可以查到两张表,怎么组合成一张表,这两张表有相等的字段prod
[最优解释]
多种组合是啊
[其他解释]
select * from a join b on b.prod=a.prod
[其他解释]

引用:
select * from a join b on b.prod=a.prod

存储过程啊,请问能具体点吗
[其他解释]
你的sql语句为什么不在存储过程里写呢?
一般的查询不需要用存储过程吧?
多数复杂的查询使用试图实现的吧
把你的sql帖出来,帮你合一下
[其他解释]
引用:
你的sql语句为什么不在存储过程里写呢?
一般的查询不需要用存储过程吧?
多数复杂的查询使用试图实现的吧
把你的sql帖出来,帮你合一下


sql太麻烦
一:这是sqlwhere
public string GetWhere()
{
Hashtable hsParams = new Hashtable();
string strSQLWhere = string.Empty;

string sAreaCode = "000";
dStart = m_strMonth1;
dEnd = m_strMonth2;
if (m_strDrlTT.Trim() != "全部")
{
if (m_strDrlTT.Contains("最新"))
{
strSQLWhere += " and V_MONTHLYCAN.是否淘汰= '" + m_strDrlTT.Substring(0, 1) + "'";
}
else
{
strSQLWhere += " and MONTHLYSUM.ifout='" + m_strDrlTT.Substring(0, 1) + "'";
}
}

if (m_strDrlXP.Trim() != "全部")
{
strSQLWhere += " and V_MONTHLYCAN.是否新品='" + m_strDrlXP + "'";
}
if (m_strDrlTTP.Trim() != "全部")
{
strSQLWhere += " and nvl(V_MONTHLYCAN.淘汰品小库存,'否')= '" + m_strDrlTTP + "'";
}
if (m_strDrlDaQ.Trim() != "全部")
{
strSQLWhere += " and V_BSC.大区名称= '" + m_strDrlDaQ + "'";
}
if (m_strDrlPianQ.Trim() != "全部")
{
strSQLWhere += " and V_BSC.片区名称='" + m_strDrlPianQ + "'";
}
if (m_strDrlDiQ.Trim() != "全部")
{
int lastindex = m_strDrlDiQ.IndexOf(":");
sAreaCode = m_strDrlDiQ.Substring(0, lastindex);
strSQLWhere += " and V_BSC.mergercode='" + sAreaCode + "'";
}
if (m_strDrlXS.Trim() != "全部")
{
strSQLWhere += " and MONTHLYSUM.SELRANK='" + m_strDrlXS + "'";
}
if (m_strDrlSC.Trim() != "全部")
{
strSQLWhere += " and MONTHLYSUM.PRODRANK='" + m_strDrlSC + "'";
}

if (m_strTxtXL.Trim() != "")
{
strSQLWhere += " and V_MONTHLYCAN.系列 in (" + m_strTxtXL + ")";
}
else if (m_strDrlXL != "全部")
{
strSQLWhere += " and V_MONTHLYCAN.系列='" + m_strDrlXL + "'";
}
if (m_strTxtProd.Trim() != "")
{
strSQLWhere += " and prod in (" + m_strTxtProd + ")";
hsParams.Add("@m_strTxtProd", m_strTxtProd);
}
else if (m_strDrlProd != "全部")


{
strSQLWhere += " and V_MONTHLYCAN.系列='" + m_strDrlProd + "'";
}

if (m_strRdoXL == "1")
{
strSQLWhere += " group by V_BSC.mergercode,V_BSC.mergername ,V_MONTHLYCAN.系列 order by V_BSC.mergercode,XL";
strSQL += "select distinct ISNULL(V_MONTHLYCAN.系列,' ') as XL, V_BSC.mergercode,V_BSC.mergername as mergername";
}
else
{
strSQLWhere += " group by V_BSC.mergercode,V_BSC.mergername,MONTHLYSUM.prod ,V_MONTHLYCAN.系列 order by V_BSC.mergercode,prod";
strSQL += "select distinct ISNULL(MONTHLYSUM.prod,' ') as prod, V_BSC.mergercode,V_BSC.mergername as mergername,V_MONTHLYCAN.系列 XL";
}
//if (m_strDrlDaQ == "全部" && m_strDrlPianQ == "全部" && m_strDrlDiQ == "全部")
//{
//'如果是全部地区、入库或发出统计,则总公司不统计
//}
return strSQLWhere;
}
二:这是sql1
public string GetstrMainData()
{
string SQLwhere = GetWhere();
Hashtable hsParams = new Hashtable();
if (m_strDrlLB == "销售")
{
if (m_strDrlQD == "全部")
{
SQL = strSQL + " , round(ISNULL(sum((monthlysum.selsell+monthlysum.ensell)*MONTHLYSUM.[convert]),0),0) SELL";
}
if (m_strDrlQD == "经销")
{
SQL = strSQL + " ,round(ISNULL(sum((monthlysum.selsell)*MONTHLYSUM.[convert]),0),0) SELL";
}
if (m_strDrlQD == "工程")
{
SQL = strSQL + " , round(ISNULL(sum((monthlysum.ensell)*MONTHLYSUM.[convert]),0),0) SELL";
}
}
if (m_strDrlLB == "入库")
{
if (m_strDrlQD == "全部")
{
SQL = strSQL + " ,round(ISNULL(sum((monthlysum.pselin+monthlysum.penin)*MONTHLYSUM.[convert]),0),0) PIN";
}
if (m_strDrlQD == "经销")
{
SQL = strSQL + " ,round(ISNULL(sum((monthlysum.pselin)*MONTHLYSUM.[convert]),0),0) PIN";
}
if (m_strDrlQD == "工程")
{
SQL = strSQL + " ,round(ISNULL(sum((monthlysum.penin)*MONTHLYSUM.[convert]),0),0) PIN";
}

}
if (m_strDrlLB == "发出")
{
if (m_strDrlQD == "全部")
{
SQL = strSQL + " ,round(ISNULL(sum((monthlysum.pselsent+monthlysum.pensent)*MONTHLYSUM.[convert]),0),0) SENT";

}
if (m_strDrlQD == "经销")
{
SQL = strSQL + " ,round(ISNULL(sum((monthlysum.pselsent)*MONTHLYSUM.[convert]),0),0) SENT";
}
if (m_strDrlQD == "工程")
{
SQL = strSQL + " ,round(ISNULL(sum((monthlysum.pensent)*MONTHLYSUM.[convert]),0),0) SENT";
}
}
if (m_strDrlLB == "库存")
{
if (m_strDrlQD == "全部")
{
SQL = strSQL + " ,round(ISNULL(sum(decode(sign(MONTHLYSUM.ymonth-" + dMaxYmonth + "),0,decode(sign(MONTHLYSUM.weeknum-" + dMaxWeek + "),0,(monthlysum.pselstock+monthlysum.penstock)*MONTHLYSUM.[convert],0) ,0)),0),0) STOCK";
}
if (m_strDrlQD == "经销")
{
SQL = strSQL + " ,round(ISNULL(sum(decode(sign(MONTHLYSUM.ymonth-" + dMaxYmonth + "),0,decode(sign(MONTHLYSUM.weeknum-" + dMaxWeek + "),0,(monthlysum.pselstock)*MONTHLYSUM.[convert],0) ,0)),0),0) STOCK";


}
if (m_strDrlQD == "工程")
{
SQL = strSQL + " ,round(ISNULL(sum(decode(sign(MONTHLYSUM.ymonth-" + dMaxYmonth + "),0,decode(sign(MONTHLYSUM.weeknum-" + dMaxWeek + "),0,(monthlysum.penstock)*MONTHLYSUM.[convert],0) ,0)),0),0) STOCK";
}
}
if (m_strDrlLB == "在途")
{
if (m_strDrlQD == "全部")
{
SQL = strSQL + " ,round(ISNULL(sum(decode(sign(MONTHLYSUM.ymonth-" + dMaxYmonth + "),0,decode(sign(MONTHLYSUM.weeknum-" + dMaxWeek + "),0,(monthlysum.pztstock+monthlysum.ztstock+monthlysum.penztstock+monthlysum.enztstock)*MONTHLYSUM.[convert],0) ,0)),0),0) ZTSTOCK";
}
if (m_strDrlQD == "经销")
{
SQL = strSQL + " ,round(ISNULL(sum(decode(sign(MONTHLYSUM.ymonth-" + dMaxYmonth + "),0,decode(sign(MONTHLYSUM.weeknum-" + dMaxWeek + "),0,(monthlysum.pztstock+monthlysum.ztstock)*MONTHLYSUM.[convert],0) ,0)),0),0) ZTSTOCK";
}
if (m_strDrlQD == "工程")
{
SQL = strSQL + " ,round(ISNULL(sum(decode(sign(MONTHLYSUM.ymonth-" + dMaxYmonth + "),0,decode(sign(MONTHLYSUM.weeknum-" + dMaxWeek + "),0,(monthlysum.penztstock+monthlysum.enztstock)*MONTHLYSUM.[convert],0) ,0)),0),0) ZTSTOCK";
}
}

SQL += " from MONTHLYSUM,V_BSC,V_MONTHLYCAN where MONTHLYSUM.AREACODE = V_BSC.号码 and MONTHLYSUM.prod=V_MONTHLYCAN.品名 and monthlysum.ymonth>='" + m_strMonth1 + "'and monthlysum.ymonth<='" + m_strMonth2 + "'"
+ SQLwhere;

return SQL;
}
三:这是sql2
public string GetstrNew()
{
string SQLwhere = GetWhere();
Hashtable hsParams = new Hashtable();
string sql = string.Empty;
if (m_strRdoProd == "1")
{
sql = "select distinct ISNULL(V_MONTHLYCAN.是否生产,' ')as sfsc ,ISNULL(V_MONTHLYCAN.是否新品,' ') as sfxp,MONTHLYSUM.SELRANK as xsdj,,monthlysum.prod"
+ " from monthlysum,V_MONTHLYCAN where monthlysum.prod=V_MONTHLYCAN.品名 and monthlysum.ymonth>='" + m_strMonth1 + "'and monthlysum.ymonth<='" + m_strMonth2 + "'";
}
return sql;
}
[其他解释]
多种组合
1.拼接联合查询的sql传进存储过程行
2.在存储过程直接对两张表联合查询也行
3.你在视图里面写好多表的查询,然后调用视图也一样

读书人网 >asp.net

热点推荐