Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

INSERT multiple rows using xbasic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    INSERT multiple rows using xbasic

    Below is an example of the code I'd use to insert one row of new data into a SQL table.

    How do I insert multiple rows? ....some rows could be dirty and some will not. Basically, everything that is being displayed in my grid I want to be inserted into another SQL table.

    Code:
    function SomeFunctionName as c (e as p)
    
    dim cn as sql::connection
    cn.Open("::Name::SQL") 'saved connection string
    dim args as sql::arguments
    
    args.set("arg1",e._currentRowDataNew.StartTime)
    args.set("arg2",e._currentRowDataNew.EndTime)
    args.set("arg3",e._currentRowDataNew.StaffName)
    
    cn.Execute("INSERT INTO SomeTableName (STARTTIME, ENDTIME, Staffname) VALUES (:arg1, :arg2, :arg3)",args)
    
    cn.close()
    Mike Brown - Contact Me
    Programmatic Technologies, LLC
    Programmatic-Technologies.com
    Independent Developer & Consultant​​

    #2
    Re: INSERT multiple rows using xbasic

    If your "SomeFunctionName" name ajax callback is set up to return 'All' rows (Row number in the Ajax Callback Action), then you can use the 'e' object V property to get all the data currently displayed.

    e.V.R1.STARTTIME is the StartTime value in Row 1
    e.v.R8.Staffname is the Staffname value in Row 8

    Looping through that data you'd build up your SQL Insert statement to look like this...

    Code:
    INSERT INTO SomeTableName
      (STARTTIME, ENDTIME, Staffname)
    VALUES
      (ST1, ET1, SN1),
      (ST2, ET2, SN2),
      (ST3, ET3, SN3),
      (ST4, ET4, SN4);

    Comment


      #3
      Re: INSERT multiple rows using xbasic

      Originally posted by Davidk View Post
      If your "SomeFunctionName" name ajax callback is set up to return 'All' rows (Row number in the Ajax Callback Action), then you can use the 'e' object V property to get all the data currently displayed.

      e.V.R1.STARTTIME is the StartTime value in Row 1
      e.v.R8.Staffname is the Staffname value in Row 8

      Looping through that data you'd build up your SQL Insert statement to look like this...

      Code:
      INSERT INTO SomeTableName
        (STARTTIME, ENDTIME, Staffname)
      VALUES
        (ST1, ET1, SN1),
        (ST2, ET2, SN2),
        (ST3, ET3, SN3),
        (ST4, ET4, SN4);
      I won't know exactly how many rows there will be ...depends on what the user does. There could be two rows or thirty. Maybe I am misinterpreting what you wrote? ...from your example above if there was a 5th row it wouldn't go over to the other table.
      Mike Brown - Contact Me
      Programmatic Technologies, LLC
      Programmatic-Technologies.com
      Independent Developer & Consultant​​

      Comment


        #4
        Re: INSERT multiple rows using xbasic

        You'll get the current RowCount from

        Code:
        e.__si.RowCount
        You said you wanted to insert "everything that is being displayed in my grid" into another table.

        Using the row count and e.V you get everything... including dirty field values.

        If the user changes the number of displayed row (if you allow that) then e.__si.RowCount will reflect that change.

        It's all very cool.

        Comment


          #5
          Re: INSERT multiple rows using xbasic

          Originally posted by mikeallenbrown View Post
          I won't know exactly how many rows there will be ...depends on what the user does. There could be two rows or thirty. Maybe I am misinterpreting what you wrote? ...from your example above if there was a 5th row it wouldn't go over to the other table.
          Use a loop (FOR i = 1 TO x ... NEXT i) to generate your insert statement. Or use a loop to set your arguments and execute each insert individually. You can wrap your inserts in cn.beginTransaction() and cn.commitTransaction() (or cn.rollBackTransaction()) if you want prevent updating the db if there are any issues.
          ---
          Sarah
          Alpha Anywhere latest pre-release

          Comment


            #6
            Re: INSERT multiple rows using xbasic

            Thanks David for your help ...the user may add, delete, or alter the displayed data. When the user is finished they press a button with calls my XB function. I'd also need help with the syntax. I'm not entirely sure how to put all of this together.

            Code:
            function SomeFunctionName as c (e as p)
            
            dim cn as sql::connection
            cn.Open("::Name::SQL") 'saved connection string
            
            args.set("arg1",e.__si.RowCount.StartTime)
            args.set("arg2",e.__si.RowCount.EndTime)
            args.set("arg3",e.__si.RowCount.StaffName)
            
            cn.Execute (INSERT INTO SomeTableName STARTTIME, ENDTIME, Staffname)
                VALUES
                            (ST1, ET1, SN1),
                            (ST2, ET2, SN2),
                            (ST3, ET3, SN3),
                            (ST4, ET4, SN4);
            
            cn.close()
            Mike Brown - Contact Me
            Programmatic Technologies, LLC
            Programmatic-Technologies.com
            Independent Developer & Consultant​​

            Comment


              #7
              Re: INSERT multiple rows using xbasic

              Mike,
              Maybe something like the below is what you need? It will give you an idea on how to loop through the stuff in your grid. I used the primary keys which are available in the e object. I figured you could grab the primary keys and then you would be able to make one single query using "IN" (if you are using SQL) instead of multiple queries.

              Code:
              function TestGridData as c (e as p)
              
              dim jscmd as c
              dim keys as p = e.keys
              dim ids as c
              dim rw as n = convert_type(e.__si.RowCount,"N")
              dim cnt as n = 1
              dim nxtkey as c
              
              while rw >= cnt
              	nxtkey = "keys.R"+cnt+".[1]"	
              	ids = ids + eval(nxtkey) + ","
              	cnt = cnt + 1
              end while
              
              ids = rtrim(ids,",")
              
              jscmd = "alert('"+ids+"');"
              
              TestGridData = jscmd
              
              end function
              Of course you can format the ids any way you'd like.

              For instance, changing a few lines will give you the right format for an IN
              Code:
              function TestGridData as c (e as p)
              
              dim jscmd as c
              dim keys as p = e.keys
              dim ids as c = "("
              dim rw as n = convert_type(e.__si.RowCount,"N")
              dim cnt as n = 1
              dim nxtkey as c
              
              while rw >= cnt
              	nxtkey = "keys.R"+cnt+".[1]"	
              	ids = ids + eval(nxtkey) + ","
              	cnt = cnt + 1
              end while
              
              ids = rtrim(ids,",") + ")"
              
              jscmd = "alert('"+ids+"');"
              
              TestGridData = jscmd
              
              end function
              So, you could do an INSERT INTO SOMETABLE FROM (SELECT Fieldnames WHERE unique id IN ids)
              Last edited by -Jinx-; 04-24-2014, 05:38 PM.

              Comment


                #8
                Re: INSERT multiple rows using xbasic

                Here's one way... multiple INSERT statements.

                Code:
                dim cn as sql::Connection
                dim flag as l
                dim sqlStmt as c
                dim rs as sql::ResultSet
                dim args as sql::arguments
                
                flag = cn.open("::Name::Test")
                
                if flag then
                
                	dim currRowCount as n = val(e.__si.RowCount)
                	for i = 1 to currRowCount
                		args.set("FN",eval("e.V.R" + i + ".FirstName"))
                		args.set("LN",eval("e.V.R" + i + ".LastName"))
                		args.set("CO",eval("e.V.R" + i + ".Company"))
                		sqlStmt = "INSERT INTO tblCustomers_copy (FIRSTNAME, LASTNAME, COMPANY) VALUES (:FN, :LN, :CO);"
                
                		flag = cn.Execute(sqlStmt,args)
                		if flag then
                			'ALL OK
                		else
                			'there was an error - close the connection and exit
                			cn.Close()
                			dim msg as c 
                			msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
                			msg = js_escape(msg)
                			dim jscmd as c 
                			jscmd = "alert('" + msg + "');"
                			gridAllRows = jscmd
                			exit function
                		end if  
                	next i
                	
                else
                	'there was an error - close the connection and exit
                	dim msg as c 
                	msg = "Could not open the connection. Error reported was: " + cn.CallResult.text
                	msg = js_escape(msg)
                	dim jscmd as c 
                	jscmd = "alert('" + msg + "');"
                	gridAllRows = jscmd
                	exit function
                end if
                
                'cn.FreeResult()
                cn.close()

                or, a single INSERT statement with multiple values... and as a bonus you get to use the mergeDataIntoTemplate function... which is always a hoot.
                What you need to watch out for here is missing numeric values in your data... not sure about dates as well.
                You won't be putting quotes around numeric values... so you'd have to test the e.V row value and if there's nothing there put in a NULL or something appropriate to your field in the table.

                Code:
                template = <<%html%
                ({ds.data("firstname")}, {ds.data("lastname")}, {ds.data("company")})
                %html%
                
                
                dim cn as sql::Connection
                dim flag as l
                dim sqlStmt as c
                dim rs as sql::ResultSet
                dim args as sql::arguments
                
                flag = cn.open("::Name::Test")
                
                if flag then
                
                	dim currRowCount as n = val(e.__si.RowCount)
                	dim insertValues as c = ""
                	dim dataSource[0] as p
                
                	for i = 1 to currRowCount
                	
                		dataSource[].firstname = "'" + eval("e.V.R" + i + ".FirstName") + "'"
                		dataSource[..].lastname = "'" + eval("e.V.R" + i + ".LastName") + "'"
                		dataSource[..].company = "'" + eval("e.V.R" + i + ".Company") + "'"
                	
                	Next i
                
                	insertValues = a5_mergeDataIntoTemplate(template,dataSource)
                	
                	insertValues = crlf_to_comma(insertValues)
                	
                	sqlStmt = "INSERT INTO tblCustomers_copy (FIRSTNAME, LASTNAME, COMPANY) VALUES " + insertValues
                	flag = cn.Execute(sqlStmt,args)
                	if flag then
                		'ALL OK
                	else
                		'there was an error - close the connection and exit
                		cn.Close()
                		dim msg as c 
                		msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
                		msg = js_escape(msg)
                		dim jscmd as c 
                		jscmd = "alert('" + msg + "');"
                		gridAllRows = jscmd
                		exit function
                	end if  
                	
                else
                	'there was an error - close the connection and exit
                	dim msg as c 
                	msg = "Could not open the connection. Error reported was: " + cn.CallResult.text
                	msg = js_escape(msg)
                	dim jscmd as c 
                	jscmd = "alert('" + msg + "');"
                	gridAllRows = jscmd
                	exit function
                end if
                
                'cn.FreeResult()
                cn.close()

                Comment


                  #9
                  Re: INSERT multiple rows using xbasic

                  may be I shouldn't ask any question. but..
                  how is the grid populated, how the rows copy function is called?
                  why would there be different number of rows in different scenario?
                  is the grid better suited for this or dialog?
                  just curious.
                  thanks for reading

                  gandhi

                  version 11 3381 - 4096
                  mysql backend
                  http://www.alphawebprogramming.blogspot.com
                  [email protected]
                  Skype:[email protected]
                  1 914 924 5171

                  Comment


                    #10
                    Re: INSERT multiple rows using xbasic

                    David,

                    I'm just now getting back to this. I'm using your code from above for multiple inserts, however, I'm getting an error. Not sure what the problem is. "ClientID" is in the grid...



                    Code:
                    Function WriteIDT_MPC as c (e as p)
                    
                    dim cn as sql::Connection
                    dim flag as l
                    dim sqlStmt as c
                    dim rs as sql::ResultSet
                    dim args as sql::arguments
                    
                    flag = cn.open("::Name::SQL")
                    
                    if flag then
                    
                    	dim currRowCount as n = val(e.__si.RowCount)
                    	for i = 1 to currRowCount
                    		args.set("A",eval("e.V.R" + i + ".ClientID"))
                    		args.set("B",eval("e.V.R" + i + ".MRNum"))
                    		args.set("C",eval("e.V.R" + i + ".PatientName"))
                    		args.set("D",eval("e.V.R" + i + ".DateTimeFinalized"))
                    		args.set("E",eval("e.V.R" + i + ".FinalizedBy"))
                    		args.set("F",eval("e.V.R" + i + ".IDT_Meeting_Date"))
                    		args.set("G",eval("e.V.R" + i + ".Category"))
                    		args.set("H",eval("e.V.R" + i + ".StartDate"))
                    		args.set("I",eval("e.V.R" + i + ".Medication"))
                    		args.set("J",eval("e.V.R" + i + ".InjectPumps"))
                    		args.set("K",eval("e.V.R" + i + ".Dose"))
                    		args.set("L",eval("e.V.R" + i + ".Route"))
                    		args.set("M",eval("e.V.R" + i + ".Frequency"))
                    		args.set("N",eval("e.V.R" + i + ".Indication"))
                    		args.set("O",eval("e.V.R" + i + ".DC_Date"))
                    		args.set("P",eval("e.V.R" + i + ".C_NC"))
                    		args.set("Q",eval("e.V.R" + i + ".High_Alert"))
                    		
                    		sqlStmt = "INSERT INTO IDT_POC_MPC (ClientID, MRNum, PatientName, DateTimeFinalized, FinalizedBy, IDT_Meeting_Date, Category, StartDate, Medication, InjectPumps, Dose, Route, Frequency, Indication, DC_Date, C_NC, High_Alert) VALUES (:A, :B, :C, :D, :E, :F, :G, :H, :I, :J, :K, :L, :M, :N, :O, :P, :Q);"
                    		
                    
                    		flag = cn.Execute(sqlStmt,args)
                    		if flag then
                    			'ALL OK
                    		else
                    			'there was an error - close the connection and exit
                    			cn.Close()
                    			dim msg as c 
                    			msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
                    			msg = js_escape(msg)
                    			dim jscmd as c 
                    			jscmd = "alert('" + msg + "');"
                    			gridAllRows = jscmd
                    			exit function
                    		end if  
                    	next i
                    	
                    else
                    	'there was an error - close the connection and exit
                    	dim msg as c 
                    	msg = "Could not open the connection. Error reported was: " + cn.CallResult.text
                    	msg = js_escape(msg)
                    	dim jscmd as c 
                    	jscmd = "alert('" + msg + "');"
                    	gridAllRows = jscmd
                    	exit function
                    end if
                    
                    'cn.FreeResult()
                    cn.close()	
                    
                    end function
                    Mike Brown - Contact Me
                    Programmatic Technologies, LLC
                    Programmatic-Technologies.com
                    Independent Developer & Consultant​​

                    Comment


                      #11
                      Re: INSERT multiple rows using xbasic

                      if you put in a debug, can you find it in the "e" object? If so, it's probably the format of the eval statement, which can be fruity sometimes I've found.
                      You can also switch the first and second args.set lines and see if the error switches to "MRNum".

                      Comment

                      Working...
                      X