Query only returns one value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    Query only returns one value

    The following query only returns one value when several are expected.

    SELECT @contactid = Con.CONTACTID, @accountid = Con.ACCOUNTID, @lastname = LASTNAME, @firstname = FIRSTNAME, @email = EMAIL, @phone = WORKPHONE, @sokey = SO.SOKey
    FROM QMC_Saleslogix. sysdba.CONTACT Con INNER JOIN
    (QMC_Saleslogix .sysdba.OPPORTU NITY_CONTACT OpCon INNER JOIN
    (QMC_Saleslogix .sysdba.OPPORTU NITY Op INNER JOIN
    mas500_app.dbo. tsoSalesOrder SO
    ON SO.CustPONo = Op.DESCRIPTION)
    ON Op.OPPORTUNITYI D = OpCon.OPPORTUNI TYID)
    ON OpCon.CONTACTID = Con.CONTACTID
    WHERE SO.Status = 1 AND SO.CntctKey IS NULL

    If I take out the joins and run the simplified version of:

    SELECT * FROM tsoSalesOrder WHERE Status = 1 AND CntctKey IS NULL

    I receive several values. Essentially I am searching for all sales orders of status equal to 1 and do not have a contact key. I then am performing joins to get the appropriate contact information from a separate database. Am I going about this in an incorrect manner?
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    #2
    Edit:

    Query returns several values when I remove the variables. Given this is part of a stored procedure, do I have to loop through the results somehow, or should this not be affected?

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      The reason the query returns one value to your variable is because the query will store the last row that it return on your variable. As you can see when you remove the variable, it returns the entire result set.

      If you need to do a row-by-row processing on these records, yes you need to iterate through the entire resultset. If the processing can be handled by a query, then No, you just need to build the necessary sql.

      The question would depend on what you want to do with the resultset.More like, so you have these rows returned by your query, so?

      -- CK

      Comment

      • mcfly1204
        New Member
        • Jul 2007
        • 233

        #4
        Originally posted by ck9663
        The reason the query returns one value to your variable is because the query will store the last row that it return on your variable. As you can see when you remove the variable, it returns the entire result set.

        If you need to do a row-by-row processing on these records, yes you need to iterate through the entire resultset. If the processing can be handled by a query, then No, you just need to build the necessary sql.

        The question would depend on what you want to do with the resultset.More like, so you have these rows returned by your query, so?

        -- CK
        I run the first query to get contact information from our crm db for all sales orders in our erp db with status of 1 and no contact key. The following query is to create a new contact in the erp db based off the info. gathered from the first query.

        So, I would need to run the second query for each row in the result set of query 1. I will admit I am not familiar with this process in stored procedures.

        Comment

        • mcfly1204
          New Member
          • Jul 2007
          • 233

          #5
          I am going to replace my variables by inserting the results of the first query into a temporary table. I should be able to loop through the rows, inserting each one into the appropriate table. Does this sound correct?

          Comment

          • ck9663
            Recognized Expert Specialist
            • Jun 2007
            • 2878

            #6
            Would you mind posting some sample data and your desired output? You might not need to iterate through the entire resultsets, a simple INSERT INTO...SELECT might be fine.

            -- CK

            Comment

            • mcfly1204
              New Member
              • Jul 2007
              • 233

              #7
              Originally posted by ck9663
              Would you mind posting some sample data and your desired output? You might not need to iterate through the entire resultsets, a simple INSERT INTO...SELECT might be fine.

              -- CK
              Well, every row in the result set needs to be used to insert a contact into the contact table, so here is what I currently have:

              BEGIN

              DECLARE @count int,
              @name varchar(65),
              @cntctkey int,
              @cntctownerkey int,
              @account varchar(128)

              SELECT @count = @@rowcount

              SELECT Con.CONTACTID, Con.ACCOUNTID, LASTNAME, FIRSTNAME, EMAIL, WORKPHONE, SO.SOKey
              INTO #cntctkey_updat e
              FROM QMC_Saleslogix. sysdba.CONTACT Con INNER JOIN
              (QMC_Saleslogix .sysdba.OPPORTU NITY_CONTACT OpCon INNER JOIN
              (QMC_Saleslogix .sysdba.OPPORTU NITY Op INNER JOIN
              mas500_app.dbo. tsoSalesOrder SO
              ON SO.CustPONo = Op.DESCRIPTION)
              ON Op.OPPORTUNITYI D = OpCon.OPPORTUNI TYID)
              ON OpCon.CONTACTID = Con.CONTACTID
              WHERE SO.Status = 1 AND SO.CntctKey IS NULL


              WHILE @count > 0
              BEGIN

              SELECT @name = FIRSTNAME + ' ' + LASTNAME FROM #cntctkey_updat e
              SELECT @cntctkey = (SELECT MAX(CntctKey) FROM tciContact) +1
              SELECT @account = (SELECT ACCOUNT FROM QMC_Saleslogix. sysdba.ACCOUNT WHERE ACCOUNTID = (SELECT ACCOUNTID FROM #cntctkey))
              SELECT @cntctownerkey = (SELECT CustKey FROM tarCustomer WHERE CustName = @account)


              INSERT INTO tciContact (CntctKey, CntctOwnerKey, CreateType, EmailFormat, EntityType, ExtUser, Name, UpdateCounter)
              VALUES (@cntctkey, @cntctownerkey, '0', '3', '401', '0', @name, '0')

              END
              SET @count = @count - 1


              DROP TABLE #cntctkey_updat e
              END
              GO

              Comment

              • ck9663
                Recognized Expert Specialist
                • Jun 2007
                • 2878

                #8
                Originally posted by mcfly1204
                Well, every row in the result set needs to be used to insert a contact into the contact table, so here is what I currently have:

                BEGIN

                DECLARE @count int,
                @name varchar(65),
                @cntctkey int,
                @cntctownerkey int,
                @account varchar(128)

                SELECT @count = @@rowcount

                SELECT Con.CONTACTID, Con.ACCOUNTID, LASTNAME, FIRSTNAME, EMAIL, WORKPHONE, SO.SOKey
                INTO #cntctkey_updat e
                FROM QMC_Saleslogix. sysdba.CONTACT Con INNER JOIN
                (QMC_Saleslogix .sysdba.OPPORTU NITY_CONTACT OpCon INNER JOIN
                (QMC_Saleslogix .sysdba.OPPORTU NITY Op INNER JOIN
                mas500_app.dbo. tsoSalesOrder SO
                ON SO.CustPONo = Op.DESCRIPTION)
                ON Op.OPPORTUNITYI D = OpCon.OPPORTUNI TYID)
                ON OpCon.CONTACTID = Con.CONTACTID
                WHERE SO.Status = 1 AND SO.CntctKey IS NULL


                WHILE @count > 0
                BEGIN

                SELECT @name = FIRSTNAME + ' ' + LASTNAME FROM #cntctkey_updat e
                SELECT @cntctkey = (SELECT MAX(CntctKey) FROM tciContact) +1
                SELECT @account = (SELECT ACCOUNT FROM QMC_Saleslogix. sysdba.ACCOUNT WHERE ACCOUNTID = (SELECT ACCOUNTID FROM #cntctkey))
                SELECT @cntctownerkey = (SELECT CustKey FROM tarCustomer WHERE CustName = @account)


                INSERT INTO tciContact (CntctKey, CntctOwnerKey, CreateType, EmailFormat, EntityType, ExtUser, Name, UpdateCounter)
                VALUES (@cntctkey, @cntctownerkey, '0', '3', '401', '0', @name, '0')

                END
                SET @count = @count - 1


                DROP TABLE #cntctkey_updat e
                END
                GO

                In theory, you're doing the right thing. There are, however, other ways of doing it. For one, it would be better you use CURSOR .

                Also, you might want to create an identity column to handle the sequential (+1) portion. If you want to keep what you have right now, it would be better if you put this part, if possible, inside an INSERT trigger.

                -- CK

                Comment

                • FredSovenix
                  New Member
                  • Mar 2008
                  • 10

                  #9
                  This is probably still kind of messy, but could you accomplish what you want using a simple INSERT INTO...SELECT statement? (Please DO NOT use the following code; it's just to give you an idea of what your statement might look like):

                  INSERT INTO [tciContact]
                  (/* CntctKey, */ /* <<- Change to identity column */
                  CntctOwnerKey, CreateType,
                  EmailFormat, EntityType, ExtUser, Name, UpdateCounter)
                  SELECT
                  /* (SELECT MAX(CntctKey) FROM [tciContact]) + 1, */
                  (SELECT CustKey FROM [tarCustomer] WHERE CustName =
                  (SELECT ACCOUNT FROM QMC_Saleslogix. sysdba.ACCOUNT WHERE ACCOUNTID = Con.ACCOUNTID)) ,
                  '0',
                  '3',
                  '401',
                  '0',
                  FIRSTNAME + ' ' ++ LASTNAME,
                  '0'
                  FROM
                  QMC_Saleslogix. sysdba.CONTACT Con INNER JOIN
                  (QMC_Saleslogix .sysdba.OPPORTU NITY_CONTACT OpCon INNER JOIN
                  (QMC_Saleslogix .sysdba.OPPORTU NITY Op INNER JOIN
                  mas500_app.dbo. tsoSalesOrder SO ON SO.CustPONo = Op.DESCRIPTION)
                  ON Op.OPPORTUNITYI D = OpCon.OPPORTUNI TYID)
                  ON OpCon.CONTACTID = Con.CONTACTID
                  WHERE SO.Status = 1 AND SO.CntctKey IS NULL

                  I completely agree with the previous statement about using an identity column; the above code sample would probably bomb or generate strange results trying to manipulate the ID manually like this.

                  You can probably avoid the (SELECT...) sub-statement that pulls the [CustKey] column by making that part of your join as well.

                  Comment

                  • mcfly1204
                    New Member
                    • Jul 2007
                    • 233

                    #10
                    Well, I was enlightened on a few tables that makes querying the data a lot simpler. I also switched to a table variable. Here is what I have:


                    BEGIN

                    DECLARE @tblcontact table
                    (
                    SOKey int,
                    Cntctkey int,
                    Cntctownerkey int,
                    LASTNAME varchar(32),
                    FIRSTNAME varchar(32),
                    WORKPHONE varchar(32),
                    EMAIL varchar(128),
                    processed int DEFAULT 0
                    )


                    INSERT INTO @tblcontact (SOKey, Cntctkey, Cntctownerkey, LASTNAME, FIRSTNAME, WORKPHONE, EMAIL)
                    SELECT SO.Sokey, DL_CON.CONTACTI D, Cust.Custkey, CON.LASTNAME, CON.FIRSTNAME, CON.WORKPHONE, CON.EMAIL
                    FROM mas500_app.dbo. tarCustomer Cust INNER JOIN
                    (QMC_Saleslogix .sysdba.DL_CONT _REF DL_CON INNER JOIN
                    (QMC_Saleslogix .sysdba.CONTACT CON INNER JOIN
                    (QMC_Saleslogix .sysdba.OPPORTU NITY_CONTACT OPP_CON INNER JOIN
                    (QMC_Saleslogix .sysdba.SALESOR DER SALES INNER JOIN
                    mas500_app.dbo. tsoSalesOrder SO
                    ON SO.Userfld2 = SALES.SALESORDE RID)
                    ON SALES.OPPORTUNI TYID = OPP_CON.OPPORTU NITYID)
                    ON OPP_CON.CONTACT ID = CON.CONTACTID)
                    ON CON.CONTACTID = DL_CON.CONTACTI D)
                    ON DL_CON.ACCT_ID = Cust.CustID
                    WHERE SO.Status = 1 AND SO.Cntctkey IS NULL AND Cust.CompanyID = 'QMC' AND CON.EMAIL IS NOT NULL AND CON.EMAIL <> ''

                    DECLARE @ID int,
                    @sokey int,
                    @cntctkey int,
                    @cntctownerkey int,
                    @name varchar(65),
                    @email varchar(128),
                    @phone varchar(32)

                    WHILE EXISTS (SELECT * FROM @tblcontact WHERE processed = 0)
                    BEGIN
                    SELECT @ID = MIN(SOKey) FROM @tblcontact WHERE processed = 0

                    SELECT @cntctkey = Cntctkey, @cntctownerkey = Cntctownerkey, @name = FIRSTNAME + ' ' + LASTNAME, @phone = WORKPHONE, @email = EMAIL, @sokey = SOKey
                    FROM @tblcontact
                    WHERE SOKey = @ID

                    INSERT INTO mas500_app.sysd ba.tciContact (Cntctkey, Cntctownerkey, CreateType, EMailAddr, EmailFormat, EntityType, ExtUser, Name, Phone, UpdateCounter)
                    VALUES (@cntctkey, @cntctownerkey, '0', @email, '3', '401', '0', @name, @phone, '0')

                    UPDATE mas500_app.sysd ba.tsoSalesOrde r
                    SET Cntctkey = @cntctkey, UserFld4 = 'temp'
                    WHERE SOKey = @sokey

                    UPDATE @tblcontact
                    SET processed = 1 WHERE cntctkey = @ID
                    END


                    END

                    GO

                    Comment

                    • mcfly1204
                      New Member
                      • Jul 2007
                      • 233

                      #11
                      @tblcontact contains all the needed data. When I go to assign the data to the local variables, all the variables end up being NULL. Any thoughts?

                      Comment

                      • mcfly1204
                        New Member
                        • Jul 2007
                        • 233

                        #12
                        Fixed:

                        SELECT @cntctkey = (CAST(LTRIM(REP LACE(Cntctkey,' CN',' '))AS int)),@cntctown erkey = Cntctownerkey, @name = FIRSTNAME + ' ' + LASTNAME, @phone = WORKPHONE, @email = EMAIL, @sokey =SOKey
                        FROM @tblcontact
                        WHERE @ID = SOKey AND *cntctkey <> '43778'

                        *was @cntctkey

                        Comment

                        • ck9663
                          Recognized Expert Specialist
                          • Jun 2007
                          • 2878

                          #13
                          Actually, the technique you are using is technically correct. However, that style is sometimes referred to as 3GL programming technique. The reason why I am asking you to post some sample data and your desired data, is because we might resolve your problem with just a couple of T-SQL statements without the loop.

                          However, if you would rather go to with "if it ain't broke don't fix it", well it's not broken.

                          -- CK

                          Comment

                          • mcfly1204
                            New Member
                            • Jul 2007
                            • 233

                            #14
                            Originally posted by ck9663
                            Actually, the technique you are using is technically correct. However, that style is sometimes referred to as 3GL programming technique. The reason why I am asking you to post some sample data and your desired data, is because we might resolve your problem with just a couple of T-SQL statements without the loop.
                            -- CK
                            Sample data:

                            1st query = 173321, CN45006, 21604, Contact, Admin,573555254 2,sales@supplyi nc.com

                            The local variable data types match that of the corresponding columns of tciContact. I need to drop 'CN' off DL_CON.CONTACT_ ID and cast the value as an integer so that it can be inserted into tciContact as an integer.

                            Comment

                            Working...