在使用游标tsql处理之前计算行数

时间:2010-09-17 10:51:21

标签: sql-server tsql count cursor rows

我有一个使用游标的SQL Server sp:

DECLARE TestCursor CURSOR FOR
    SELECT
        tblHSOutcomes.strOutcomeName, 
        tblHSData.fkHSTest
    FROM
        tblHSData 
        INNER JOIN tblHSOutcomes ON tblHSData.fkOutcome = tblHSOutcomes.uidOutcome 
        INNER JOIN tblHSTests ON tblHSData.fkHSTest = tblHSTests.uidTest
    WHERE
        tblHSData.fkEpisode = @uidHSEpisodes

OPEN TestCursor
    FETCH NEXT FROM TestCursor
    INTO @Result, @TestID

WHILE @@FETCH_STATUS = 0
BEGIN
...etc

它工作正常,但是在继续处理游标之前能够检查游标查询是否有任何记录会很好。 如果有@@ var我可以用来检查这个吗? 我知道有@@ RowCount - 但这只有当前处理的行数 - 所以不是很有帮助

理想情况下,我希望能够做到这样的事情:

if @@cursorQueryHasRecords 
BEGIN
WHILE @@FETCH_STATUS = 0
BEGIN
...etc

感谢

NAT

3 个答案:

答案 0 :(得分:11)

如果您能够将光标声明为STATIC,那么您可以使用内置函数@@Cursor_Rows

Cursor Options (Static/ReadOnly/Dynamic)

@@Cursor_Rows

答案 1 :(得分:4)

if exists(
    SELECT
        tblHSOutcomes.strOutcomeName, 
        tblHSData.fkHSTest
    FROM
        tblHSData 
        INNER JOIN tblHSOutcomes ON tblHSData.fkOutcome = tblHSOutcomes.uidOutcome 
        INNER JOIN tblHSTests ON tblHSData.fkHSTest = tblHSTests.uidTest
    WHERE
        tblHSData.fkEpisode = @uidHSEpisodes
)
...

答案 2 :(得分:0)

这里是如何使用private SliderImageAdapter sliderImageAdapter; private List<Banner> bannerList; bannerList = new ArrayList<>(); sliderImageAdapter = new SliderImageAdapter(getActivity(),bannerList); sliderMyshop = view.findViewById(R.id.imageSlider); sliderMyshop.setSliderAdapter(sliderImageAdapter); sliderMyshop.setIndicatorAnimation(IndicatorAnimations.WORM); //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!! sliderMyshop.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION); sliderMyshop.setIndicatorSelectedColor(Color.WHITE); sliderMyshop.setIndicatorUnselectedColor(Color.GRAY); sliderMyshop.startAutoCycle(); mFirestore.collection("Banner").orderBy("priority", Query.Direction.DESCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(@javax.annotation.Nullable QuerySnapshot documentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) { if (e != null) { Log.d(TAG, "Error : " + e.getMessage()); } assert documentSnapshots != null; for (DocumentChange doc : documentSnapshots.getDocumentChanges()) { if (doc.getType() == DocumentChange.Type.ADDED) { String doc_id = doc.getDocument().getId(); Banner banner = doc.getDocument().toObject(Banner.class).withDocId(doc_id); bannerList.add(doc.getNewIndex(), banner); sliderImageAdapter.notifyDataSetChanged(); } else if (doc.getType() == DocumentChange.Type.MODIFIED) { String docID = doc.getDocument().getId(); Banner changedModel = doc.getDocument().toObject(Banner.class).withDocId(docID); if (doc.getOldIndex() == doc.getNewIndex()) { // Item changed but remained in same position bannerList.set(doc.getOldIndex(), changedModel); // sliderImageAdapter.notifyItemChanged(doc.getOldIndex()); } else { // Item changed and changed position bannerList.remove(doc.getOldIndex()); bannerList.add(doc.getNewIndex(), changedModel); // sliderImageAdapter.notifyItemMoved(doc.getOldIndex(), doc.getNewIndex()); } } else if (doc.getType() == DocumentChange.Type.REMOVED) { // remove bannerList.remove(doc.getOldIndex()); // sliderImageAdapter.notifyItemRemoved(doc.getOldIndex()); } } } });

的示例
@@Cursor_Rows

请注意,您需要声明游标DECLARE TestCursor CURSOR STATIC FOR SELECT <snip> OPEN TestCursor IF @@Cursor_Rows > 0 BEGIN FETCH NEXT FROM TestCursor INTO @compid, @logid, @category WHILE @@FETCH_STATUS = 0 BEGIN <snip> END CLOSE TestCursor DEALLOCATE TestCursor (或STATIC