下面我展示了我到目前为止所尝试的内容。
我使用游标进行SQL Server while
循环。我的查询中的问题与以下代码一致:
SELECT NAME
FROM TRDR
WHERE TRDR = MyCursor.TRDR
我不确定如何从光标@MyCursor
DECLARE @MyCursor CURSOR;
DECLARE @MyField INT;
DECLARE @FromDate DATE;
DECLARE @ToDate DATE;
DECLARE @Serie INT;
DECLARE @Sosource INT;
DECLARE @trdr INT;
SET @FromDate = '20160901';
SET @ToDate = '20160930';
SET @Serie = 4700;
SET @Sosource = 1416;
SET @trdr = 2835;
BEGIN
SET @MyCursor = CURSOR FOR
SELECT TOP 1000 FINDOC, TRDR
FROM FINDOC
WHERE SOSOURCE = @Sosource
AND TRNDATE >= @FromDate
AND TRNDATE <= @ToDate
AND SERIES = @Serie
AND TRDR = 2835
END
OPEN @MyCursor
FETCH NEXT FROM @MyCursor INTO @MyField
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT NAME
FROM TRDR
WHERE TRDR = MyCursor.TRDR
FOR XML PATH('GrpHdr'), ROOT ('CstmrCdtTrfInitn')
FETCH NEXT FROM @MyCursor INTO @MyField
END;
CLOSE @MyCursor ;
DEALLOCATE @MyCursor;
预期输出应如下:
<CstmrCdtTrfInitn>
<GrpHdr>
<name>Chris</name>
</GrpHdr>
<GrpHdr>
<name>Martin</name>
</GrpHdr>
<GrpHdr>
<name>John</name>
</GrpHdr>
<GrpHdr>
<name>George</name>
</GrpHdr>
<GrpHdr>
<name>Michael</name>
</GrpHdr>
</CstmrCdtTrfInitn>
答案 0 :(得分:0)
更新您真正想要的结果。试试这个,看看是否有效。
Select TOP 1000 T.Name
FROM FINDOC F
JOIN TRDR T ON F.TRDR = T.TRDR
WHERE F.SOSOURCE = @Sosource
AND F.TRNDATE >= @FromDate
AND F.TRNDATE <= @ToDate
AND F.SERIES = @Serie
AND F.TRDR = 2835
FOR XML PATH('GrpHdr'), ROOT ('CstmrCdtTrfInitn')