-2
我想编写一个游标来比较两个表数据并查找fifo成本。如何在sql中编写游标以比较两个表数据并从一个记录中找到fifocost
我已经写了这个查询的游标,但它没有得到正确的成本。
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[SP_GETFIFOCOST](@pid nvarchar(250))
as
Begin
Declare @qty decimal(18,2),@cost decimal(18,2),@line int
Declare @qty1 decimal(18,2),@cost1 decimal(18,2),@line1 int
Declare MyCur Cursor for
select QTY_IN,UNIT_COST,LINE
from STOCKDETAILS
where STOCK_JR in ('IN','GR','TR','SA','OS','SR','AS','OG','PO')
and [email protected] order by STOCK_DATE
Declare @fqty decimal(18,2)
Open MyCur
Fetch next from mycur into @qty, @cost, @line
While @@Fetch_Status=0
Begin
Declare MyCur1 Cursor for
select QTY_IN,UNIT_COST,LINE
from STOCKDETAILS
where STOCK_JR in ('OU','IS','SJ','TR','SA','CS','AS','PR','DO')
and [email protected] order by STOCK_DATE
Open MyCur1
Fetch next from mycur1 into @qty1, @cost1, @line1
While @@Fetch_Status = 0
Begin
if @qty>[email protected]
set @[email protected]@qty1
set @[email protected][email protected]
if @qty<@qty1
set @[email protected]@qty1
set @[email protected][email protected]@qty1
Fetch Next From MyCUr into @qty,@cost,@line
Fetch Next From MyCUr1 into @qty1,@cost1,@line1
while @fqty=0
Begin
update STOCKDETAILS
set [email protected]
where [email protected]
End
End
Close Mycur
Close Mycur1
Deallocate Mycur
Deallocate Mycur1
End
End
Exec SP_GETFIFOCOST 'F1'
我已经写了同样的逻辑在这样
b = table2.Rows.Count - 1;
try
{
for (a = table1.Rows.Count - 1; a >= 0; a--)
{
total();
while (fqty != 0 && fqty >= 0)
{
if (b >= 0)
{
b = b - 1;
total();
}
else
{
fqty = 0;
}
}
}
}
catch (Exception EX) { }
void total()
{
try
{
fout = Convert.ToDecimal(table2.Rows[b]["QTY_OUT"].ToString());
}
catch (Exception EX) { fout = 0; }
try
{
fin = Convert.ToDecimal(table1.Rows[a]["QTY_IN"].ToString());
}
catch (Exception EX) { fin = 0; }
if (fin >= fout)
{
fqty = fin - fout;
table1.Rows[a]["QTY_IN"] = fqty;
try
{
string fc = table1.Rows[a]["UNIT_COST"].ToString();
string sn = table2.Rows[b]["LINE"].ToString();
TRcmd.Connection = cn;
TRcmd.CommandText = "update STOCKDETAILS set FIFO_COST='" + fc + "' where LINE='" + sn + "'";
TRcmd.CommandType = CommandType.Text;
TRcmd.ExecuteNonQuery();
}
catch (Exception EX) { }
if (fqty == 0)
{
b = b - 1;
}
}
else
{
fqty = 0;
table1.Rows[a]["QTY_IN"] = fqty;
try
{
string fc = table1.Rows[a]["UNIT_COST"].ToString();
string sn = table2.Rows[b]["LINE"].ToString();
TRcmd.Connection = cn;
TRcmd.CommandText = "update STOCKDETAILS set FIFO_COST='" + fc + "' where LINE='" + sn + "'";
TRcmd.CommandType = CommandType.Text;
TRcmd.ExecuteNonQuery();
}
catch (Exception EX) { }
a = a - 1;
try
{
table1.Rows[a]["QTY_IN"] = Convert.ToDecimal(table1.Rows[a]["QTY_IN"].ToString()) + (fin - fout);
}
catch (Exception EX) { }
a = a + 1;
b = b - 1;
}
}
前端你能帮我吗?
哪个RDBMS用于这个?请添加一个标签来指定您是使用'mysql','postgresql','sql-server','oracle'还是'db2' - 或者其他的东西。 –
Sql server 2008 –
备注:您应该**不要**为存储过程使用'sp_'前缀。微软已经保留了这个前缀以供自己使用(参见*命名存储过程*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx),以及你将来有可能冒着名字冲突的风险。 [这对你的存储过程性能也是不利的](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix)。最好只是简单地避免使用'sp_'并将其他内容用作前缀 - 或者根本没有前缀! –