Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Parsing / Combining?
Message
General information
Forum:
ASP.NET
Category:
ADO.NET
Title:
SQL Parsing / Combining?
Miscellaneous
Thread ID:
01142755
Message ID:
01142755
Views:
49
I wrote an SQL Warpper Class that incorporates most of the main functions used when accessing an SQL Database into one class. Now, I am coming across a little problem. I wrote a method that opens up a Table inside a Dataset based on an SQL Query passed to the method. This portion works fine, the problem I am having when executing the Update method. I added in a section of code that adds a query immediatly after the Update that selects the recently inserted record. Basically, my Update method checks the table currently being updated, and if it finds a column named ID (AKA Primary Key, Int Identity), then it would add this secondary query which would retrieve the Identity fields value assigned at the time of insert and place that number into my Table/Dataset. The problem I ended up hitting was that if I originally created a complex Select Statement, then the WHERE Clause of my "added" query tacked on to the end of the original Select Statement crashed, because the Where Clause was in the wrong location. I will illustrate a simple example below:
'***************************************
' Notes:
'
'   strSQL            - SQL Select Statement Used To Open The Table
'   objCommandBuilder - Previously Set Using strSQL
'
'***************************************

...
...
...

'***************************************
' Check If Table Has an ID Column
'***************************************
If Me.Tables(strTableName).Columns.Contains("ID") Then
   objAdapter.InsertCommand = New SqlClient.SqlCommand
   With objAdapter.InsertCommand
      '***************************************
      ' Append Select SQL After INSERT Statement (For Int Identity Retrieval)
      '***************************************
      .CommandType = CommandType.Text
      .CommandText = objCommandBuilder.GetInsertCommand.CommandText
      .CommandText &= ";" & strSQL & " WHERE ID = SCOPE_IDENTITY()"
      .Connection = objConnection
   End With
End If

...
...
...
Now if my original SQL Select Statement was more complex then a simple Select Statement, I get the following problem...
'Original Select Statement
strSQL = "SELECT * from tblInfo ORDER BY [Year] DESC"

'Modified Insert Statement (When Using the Update Method)
strSQL = "INSERT INTO tblInfo(Year, Description, Description2, Address1, Address2, City, State, Zip, Phone, Fax, Contact, CenterID)" & _
         "VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12);" & _
         "SELECT * FROM tblInfo ORDER BY [Year] DESC WHERE ID = SCOPE_IDENTITY()"
As you can see, the WHERE Clause ended up after the ORDER BY Clause, which causes an error. I need to find a way (if possibly) to parse an SQL Statement into it's individual clauses, that way I can insert my WHERE Clause into the proper location. If anyone (possible) knows of a SQLPaserClass out there, or maybe something that I am overlooking in the .Net language that can help me, or even any other way of approaching this particular problem in another fasion that I don't know about...I would be greatful.

Thanks for any help.
________________________
Ben Santiago, MCP & A+
Programmer Analyst (SQL, FoxPro, VB, VB.Net, Java, HTML, ASP, JSP, VBS)
Eastern Suffolk BOCES - Student Data Services


Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-Rich Cook
Next
Reply
Map
View

Click here to load this message in the networking platform