C# 存储过程中的While循环
这是我的第一张桌子 表1:C# 存储过程中的While循环,c#,sql,sql-server,stored-procedures,C#,Sql,Sql Server,Stored Procedures,这是我的第一张桌子 表1: CatID TenderNumber 1 AA01012013 2 AA01012013 3 AA01012013 SubCatID CatID 1 1 2 1 3 2 4 2 5 2 6 3 表2: CatID TenderNumber 1 A
CatID TenderNumber
1 AA01012013
2 AA01012013
3 AA01012013
SubCatID CatID
1 1
2 1
3 2
4 2
5 2
6 3
表2:
CatID TenderNumber
1 AA01012013
2 AA01012013
3 AA01012013
SubCatID CatID
1 1
2 1
3 2
4 2
5 2
6 3
这就是我的数据库的样子。现在我想使用C将SubCatID
和CatID
存储到新表中。这些值将从TenderNumber
中检索,如下所示:
Select CatID from Table1 where TenderNumber='AA01012013'
while(SQLDataReader1.read())
{
select SubCatID from Table2 where CatID=Reader1["CatID"]
While(SQLDataReader2.read())
{
insert into Table3(SubCatID,CatID)Values(Reader2["SubCatID"],Reader1["CatID"])
}
}
如何使用存储过程执行此操作 像这样的东西怎么样
INSERT INTO Table3
SELECT t2.SubCatID,
t2.CatID
FROm Table1 t1 INNER JOIN
Table2 t2 ON t1.CatID = t2.CatID
WHERE t1.TenderNumber='AA01012013'
DECLARE @TenderNumber VARCHAR(50) = 'AA01012013'
INSERT INTO Table3
SELECT t2.SubCatID,
t2.CatID
FROm Table1 t1 INNER JOIN
Table2 t2 ON t1.CatID = t2.CatID
WHERE t1.TenderNumber=@TenderNumber
或者甚至使用一个参数,比如
INSERT INTO Table3
SELECT t2.SubCatID,
t2.CatID
FROm Table1 t1 INNER JOIN
Table2 t2 ON t1.CatID = t2.CatID
WHERE t1.TenderNumber='AA01012013'
DECLARE @TenderNumber VARCHAR(50) = 'AA01012013'
INSERT INTO Table3
SELECT t2.SubCatID,
t2.CatID
FROm Table1 t1 INNER JOIN
Table2 t2 ON t1.CatID = t2.CatID
WHERE t1.TenderNumber=@TenderNumber
在数据库中创建一个存储过程(对于主体,我使用了@astander:)中的代码): 在你的c代码中:
我想在C#中使用存储proc@punter你为什么要在C#中这样做?还是使用
while
循环?或者甚至使用存储过程?这只是一个基于单行集合的insert
语句,不是吗?@marcgravel这只是我实际需求的一个样本。那么你能给出你的实际需求吗?给出一个可以简单地用更直接的方式解决的问题,并不能为您的实际需求提供有用的答案。