Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get output data from sql store procedure ?

How to get output data from sql store procedure ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mucahit
    wrote on 28 Sept 2020, 11:47 last edited by Mucahit
    #1

    Hi all,

    I have a problem about to get output data from sql store procedure. I want to see deger variable is 45 because in procedure it gives 45 but i see deger variable is still 5. I tried all posibility that i now but i couldnt succeed. If you help me i will be apprisuaded.

    My Code:

            QSqlDatabase db;
        
        
        
            db=QSqlDatabase::addDatabase("QODBC","myAwsomDatabase");
        
        
        
            db.setDatabaseName("DRIVER={FreeTDS};SERVER=serverip;PORT=port;DATABASE=database;UID=database;PWD=database");
    

    if(db.open())
    {

    qry.setForwardOnly(true);
    int pers_num=424;
    QString tarih="2020-09-25";
    int deger=5;
    
    qry.prepare("exec bant_pers_list ?,?,?");
    qry.bindValue(0,pers_num);
    qry.bindValue(1,tarih);
    qry.bindValue(2,deger,QSql::Out);
    
    if(qry.exec())
    {
    while(qry.next())
    {
    qDebug()<<qry.boundValue(2);
    }
    }
    }
    

    Stored Procedure:

    ALTER PROCEDURE [dbo].[bant_pers_list]
    @pers_num bigint,@tarih smalldatetime,@deger int output
    as
    SET NOCOUNT ON

    set @deger=45
    SELECT top 1 * FROM dbo.bant_no

    return

    J 1 Reply Last reply 28 Sept 2020, 14:12
    0
    • M Mucahit
      28 Sept 2020, 11:47

      Hi all,

      I have a problem about to get output data from sql store procedure. I want to see deger variable is 45 because in procedure it gives 45 but i see deger variable is still 5. I tried all posibility that i now but i couldnt succeed. If you help me i will be apprisuaded.

      My Code:

              QSqlDatabase db;
          
          
          
              db=QSqlDatabase::addDatabase("QODBC","myAwsomDatabase");
          
          
          
              db.setDatabaseName("DRIVER={FreeTDS};SERVER=serverip;PORT=port;DATABASE=database;UID=database;PWD=database");
      

      if(db.open())
      {

      qry.setForwardOnly(true);
      int pers_num=424;
      QString tarih="2020-09-25";
      int deger=5;
      
      qry.prepare("exec bant_pers_list ?,?,?");
      qry.bindValue(0,pers_num);
      qry.bindValue(1,tarih);
      qry.bindValue(2,deger,QSql::Out);
      
      if(qry.exec())
      {
      while(qry.next())
      {
      qDebug()<<qry.boundValue(2);
      }
      }
      }
      

      Stored Procedure:

      ALTER PROCEDURE [dbo].[bant_pers_list]
      @pers_num bigint,@tarih smalldatetime,@deger int output
      as
      SET NOCOUNT ON

      set @deger=45
      SELECT top 1 * FROM dbo.bant_no

      return

      J Offline
      J Offline
      JonB
      wrote on 28 Sept 2020, 14:12 last edited by JonB
      #2

      @Mucahit
      I might be wrong about this :) but I have a theory....

      I'm not sure the output parameter gets bound during fetching of the result set (inside your while loop), only on conclusion. Try the qDebug()<<qry.boundValue(2); after the while loop?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mucahit
        wrote on 28 Sept 2020, 14:30 last edited by
        #3

        Thanks for your theory but it didnt work :) I tried to get it came out on different lines (before while,inside of while and after while) but still got value that 5 .

        J 1 Reply Last reply 28 Sept 2020, 14:36
        0
        • M Mucahit
          28 Sept 2020, 14:30

          Thanks for your theory but it didnt work :) I tried to get it came out on different lines (before while,inside of while and after while) but still got value that 5 .

          J Offline
          J Offline
          JonB
          wrote on 28 Sept 2020, 14:36 last edited by JonB
          #4

          @Mucahit
          Hmmm. Shame!

          Then since the code looks OK to me, though I don't know the ins & outs of ODBC, I would start by declaring a simpler stored proc with just a single parameter, the int output, and see how that went?

          BTW, is exec right across ODBC for call a stored proc? I kinda thought it was call for ODBC? Have a read of maybe https://docs.microsoft.com/en-us/sql/relational-databases/native-client-odbc-how-to/running-stored-procedures-call-stored-procedures?view=sql-server-ver15 ?

          P.S.
          I'd like to see your qry.executedQuery() after you have executed the query. I want to see if it has implemented the OUTPUT....

          P.P.S.
          Lookie here:
          https://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values

          I think you're supposed to do this via an example there:

              QSqlQuery query;
              query.prepare("CALL AsciiToInt(?, ?)");
              query.bindValue(0, "A");
              query.bindValue(1, 0, QSql::Out);
              query.exec();
              int i = query.boundValue(1).toInt(); // i is 65
          

          See that it's CALL like I thought?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mucahit
            wrote on 28 Sept 2020, 15:00 last edited by Mucahit
            #5

            When i change exec to call it gives this error:

            QODBCResult::exec: Unable to execute statement: "[FreeTDS][SQL Server]Incorrect syntax near '424'."

            I am sure when i use exec with prepare it works because i print with qDebug() it gives me result. And when i take normal select data with stored procedure it gives me result.Why i cant run this "call bant_pers_list ?,?,?".
            qDebug()<<qry.executedQuery()
            ="exec bant_pers_list ?,?,?"

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mucahit
              wrote on 29 Sept 2020, 07:22 last edited by Mucahit
              #6

              I managed to run the call command but qry.next() didnt work and i cant get output from data on store proc. Could there be a problem with the driver FreeTDS ? I tried this example but it didnt work, i am getting i=0 :

              QSqlQuery query;
              query.prepare("CALL AsciiToInt(?, ?)");
              query.bindValue(0, "A");
              query.bindValue(1, 0, QSql::Out);
              query.exec();
              int i = query.boundValue(1).toInt(); // i is 65

              J 1 Reply Last reply 29 Sept 2020, 08:08
              0
              • M Mucahit
                29 Sept 2020, 07:22

                I managed to run the call command but qry.next() didnt work and i cant get output from data on store proc. Could there be a problem with the driver FreeTDS ? I tried this example but it didnt work, i am getting i=0 :

                QSqlQuery query;
                query.prepare("CALL AsciiToInt(?, ?)");
                query.bindValue(0, "A");
                query.bindValue(1, 0, QSql::Out);
                query.exec();
                int i = query.boundValue(1).toInt(); // i is 65

                J Offline
                J Offline
                JonB
                wrote on 29 Sept 2020, 08:08 last edited by
                #7

                @Mucahit said in How to get output data from sql store procedure ?:

                Could there be a problem with the driver FreeTDS

                That is exactly what I was thinking about. Is there any documentation on this driver, any mention of how/whether it supports OUTPUT parameters, or whether it supports them when operating on an MS SQL Server database rather than some other database like MySQL? Are you able to test code against some other driver than this one?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mucahit
                  wrote on 29 Sept 2020, 11:01 last edited by Mucahit
                  #8

                  I am already using MSSQL 17 server. I found this sources, please read.

                  1-) https://lists.ibiblio.org/pipermail/freetds/2014q1/028857.html
                  2-)https://www.freetds.org/userguide/ChoosingTdsProtocol.html
                  3-)https://raspberrypi.stackexchange.com/questions/108329/cant-get-the-msodbcsql-package-for-debian-10
                  4-) https://demirten.gitbooks.io/linux-sistem-programlama/content/misc/sqlserver.html

                  Also i work on raspberry bi card, for this I need to install msodbc17 package but it does not support arm based. I changed the freetds version to 4.2 but it still didn't work .What do you think i should do ? (i am working raspberry pi card os raspbian and qt is running this card but I'm connecting to a server on another Windows machine.)

                  1 Reply Last reply
                  0

                  8/8

                  29 Sept 2020, 11:01

                  • Login

                  • Login or register to search.
                  8 out of 8
                  • First post
                    8/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved