Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can an expert review 5 lines of sql for me please.

Posted on 2014-11-30
1
Medium Priority
?
87 Views
Last Modified: 2014-11-30
Can someone take a look at my Sql below at lines 32 through 36. Basically I want the code to loop on lines 32 thru 36 until @Flag = 1
Meaning if the value for @lchFITS_SUFFIX is not found in column SMT_FCTS_FITS_STATE_CDS then set @Flag = 1 and quit incrementing
@lchFITS_SUFFIX by 1.

The query analyzer does not throw an error so the code appears to work, however I have not executed it against actual data. So if
someone can look at my new code, lines 32 through 36, I would appreciate it.

Lines 32 through 36 is new code I wrote. This code is inside a stored procedure that uses a cursor. The longer version of the stored procedure is
below the header "Stored Proc" listed below. You can just ignore that code, I just included it for reference.

-- Please experts, check on my code on lines 32 through 36 below to see if it is okay what I am doing.

28     DECLARE @Flag INT
29     SET @Flag = 0;

30     WHILE @@FETCH_STATUS = 0
31     BEGIN
32        WHILE @Flag = 0
33        BEGIN
34              SELECT @lchFITS_SUFFIX = @lchFITS_SUFFIX+1            
35              IF NOT EXISTS(SELECT FITS_SUFFIX FROM SMT_FCTS_FITS_STATE_CDS WHERE FITS_SUFFIX = @lchFITS_SUFFIX) @Flag = 1
36        END

37           UPDATE SMT_FCTS_FUAD_FOOTETS_USR SET
38           FUAD_ITS_CLERK_ID = @lchFITS_SUFFIX
39           WHERE PSUS_USER_NUM = @lchPSUS_USER_NUM
40           AND (FUAD_ITS_CLERK_ID = ' ' OR FUAD_ITS_CLERK_ID IS NULL


41        FETCH NEXT FROM CURS_EMP
42        INTO @lchPSUS_USER_NUM, @lchFITS_STATE_CD, @lchFITS_PREFIX

43     END

--=================================================================
--   S T O R E D   P R O C  (Just ignore the code below, I just included it to show that the stored procedure is using a cursor.
--=================================================================
1 DECLARE CURS_STATE CURSOR
2 FOR
3    SELECT FITS_STATE_CD, FITS_PREFIX, FITS_SUFFIX
4    FROM SMT_FCTS_FITS_STATE_CDS FITS
5   WHERE FITS.FITS_ACTIVE = 1
6   ORDER BY FITS_STATE_CD

7 OPEN CURS_STATE
8 FETCH NEXT FROM CURS_STATE
9 INTO @lchStateCode, @lchPrefix, @lchSuffix

10  WHILE @@FETCH_STATUS = 0
11  BEGIN
12     DECLARE CURS_EMP CURSOR
13     FOR
14     SELECT
15     USTY.PSUS_USER_NUM AS PSUS_USER_NUM,
16     USTY.FITS_STATE_CD AS FITS_STATE_CD,
17     FITS.FITS_PREFIX AS FITS_PREFIX
18     FROM SMT_FCTS_FUAD_FOOTETS_USR USTY,
19     SMT_FCTS_FITS_STATE_CDS FITS
20     WHERE USTY.FITS_STATE_CD = FITS.FITS_STATE_CD
21     AND FITS.FITS_ACTIVE = 1
22     AND USTY.FITS_STATE_CD = @lchStateCode
23     AND (USTY.FUAD_ITS_CLERK_ID = '' OR USTY.FUAD_ITS_CLERK_ID IS NULL)
24     AND USTY.PSTE_CODE = @lchProdSteCode
25     ORDER BY USTY.PSUS_USER_NUM

26     OPEN CURS_EMP
27     SELECT @lchFITS_SUFFIX = CONVERT(INT, @lchSuffix)
28     FETCH NEXT FROM CURS_EMP
29     INTO @lchPSUS_USER_NUM, @lchFITS_STATE_CD, @lchFITS_PREFIX

30     WHILE @@FETCH_STATUS = 0
31     BEGIN
32        SELECT @lchFITS_SUFFIX = @lchFITS_SUFFIX+1

33      UPDATE SMT_FCTS_FUAD_FOOTETS_USR SET
34        FUAD_ITS_CLERK_ID = @lchFITS_SUFFIX
35       WHERE PSUS_USER_NUM = @lchPSUS_USER_NUM
36       AND (FUAD_ITS_CLERK_ID = ' ' OR FUAD_ITS_CLERK_ID IS NULL


37        FETCH NEXT FROM CURS_EMP
38        INTO @lchPSUS_USER_NUM, @lchFITS_STATE_CD, @lchFITS_PREFIX

39       END

40   CLOSE CURS_EMP
41   DEALLOCATE CURS_EMP
0
Comment
Question by:brgdotnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 40473005
Add this line before your line 32:
31 SET @Flag = 0
32 WHILE @Flag = 0
33        BEGIN
34              SELECT @lchFITS_SUFFIX = @lchFITS_SUFFIX+1            
35              IF NOT EXISTS(SELECT FITS_SUFFIX FROM SMT_FCTS_FITS_STATE_CDS WHERE FITS_SUFFIX = @lchFITS_SUFFIX) @Flag = 1
36        END

Open in new window

It is required to reset the @Flag for the next cursor record
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question