944,181 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 3565
  • MS SQL RSS
Jan 13th, 2007
0

MSSQL Fields Out Of Order

Expand Post »
For years I have been working with MS Access and enjoying the simplicity and ease of use. I am now making the move and converting all of my sites to MSSQL. Virtually everything works but in the process, I have found an oddity that I am not used to.

I have a question about fields in an MSSQL DB or any type of SQL DB for that matter. My question concerns pulling data from fields out of a particular order.

Let’s say that you have a table with the following fields in their specific order:

tblMyTable:

fld_id
fld _name
fld _email
fld_ip


Using ASP/VBScript I can access the data by connecting to the correct MSSQL server. I can display the information with no problems unless I attempt to pull the information out of order. For instance if I want to display the fld_ip and then the fld_name it will not work. Only fld_ip would be displayed. I presume that this is because fld_name comes BEFORE fld_ip.

SQL Statement: "SELECT * FROM tblMyTable"

Code Examples:

In this example, I am calling the fields in the order in which they appear in the table. Both items display correctly.

<table>
 <tr>
   <td>ID:</td>
   <td><%=(rsRecordset.Fields.Item("fld_id").Value)%></td>
 </tr>
 <tr>
   <td>Name:</td>
   <td><%=(rsRecordset.Fields.Item("fld_name").Value)%></td>
 </tr>
 <tr>
   <td>Email:</td>
   <td><%=(rsRecordset.Fields.Item("fld_email").Value)%></td>
 </tr>
 <tr>
   <td>IP:</td>
   <td><%=(rsRecordset.Fields.Item("fld_ip").Value)%></td>
 </tr>
</table>


In this example, I am calling the fields out of order. Only fld_ip displays correctly. fld_name does not display at all again I presume that this is because fld_name comes BEFORE fld_ip.


<table>
 <tr>
   <td>ID:</td>
   <td><%=(rsRecordset.Fields.Item("fld_id").Value)%></td>
 </tr>
 <tr>
   <td>IP:</td>
   <td><%=(rsRecordset.Fields.Item("fld_ip").Value)%></td>
 </tr>
 <tr>
   <td>Name:</td>
   <td><%=(rsRecordset.Fields.Item("fld_name").Value)%></td>
 </tr>
 <tr>
   <td>Email:</td>
   <td><%=(rsRecordset.Fields.Item("fld_email").Value)%></td>
 </tr>
</table>

In addition to this, I am not able to display fields more than once.


<table>
 <tr>
   <td>ID:</td>
   <td><%=(rsRecordset.Fields.Item("fld_id").Value)%></td>
 </tr>
 <tr>
   <td>Name:</td>
   <td><%=(rsRecordset.Fields.Item("fld_name").Value)%></td>
 </tr>
 <tr>
   <td>Email:</td>
   <td><%=(rsRecordset.Fields.Item("fld_email").Value)%></td>
 </tr>
 <tr>
   <td>IP:</td>
   <td><%=(rsRecordset.Fields.Item("fld_ip").Value)%></td>
 </tr>
 <tr>
   <td>Name:</td>
   <td><%=(rsRecordset.Fields.Item("fld_name").Value)%></td>
 </tr>
</table>


The second instance of the field “fld_name” will not display at all.

With MS Access I could display the data in any order I wanted. I could also display them multiple times without any problems.

The BIG QUESTION… Why on Earth would MSSQL behave this way?

Now, I realize that I could easily assign the data to variables and use them any way I wanted to like this:

<% 
varID = (rsRecordset.Fields.Item("fld_id").Value)
varName = (rsRecordset.Fields.Item("fld_name").Value)
varEmail = (rsRecordset.Fields.Item("fld_email").Value)
varIP = (rsRecordset.Fields.Item("fld_ip").Value)
%>

That’s great but not necessarily the most efficient way to reduce code.

So what is the deal with the limitation on this more advanced DB? What am I missing?

Thanks!

Dean
Last edited by DVHost; Jan 13th, 2007 at 9:53 pm. Reason: Change notification type.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DVHost is offline Offline
20 posts
since Apr 2004
Jan 15th, 2007
0

Re: MSSQL Fields Out Of Order

Click to Expand / Collapse  Quote originally posted by DVHost ...
For years I have been working with MS Access and enjoying the simplicity and ease of use. I am now making the move and converting all of my sites to MSSQL. Virtually everything works but in the process, I have found an oddity that I am not used to.

I have a question about fields in an MSSQL DB or any type of SQL DB for that matter. My question concerns pulling data from fields out of a particular order.

Let’s say that you have a table with the following fields in their specific order:

tblMyTable:

fld_id
fld _name
fld _email
fld_ip


Using ASP/VBScript I can access the data by connecting to the correct MSSQL server. I can display the information with no problems unless I attempt to pull the information out of order. For instance if I want to display the fld_ip and then the fld_name it will not work. Only fld_ip would be displayed. I presume that this is because fld_name comes BEFORE fld_ip.

SQL Statement: "SELECT * FROM tblMyTable"

Code Examples:

In this example, I am calling the fields in the order in which they appear in the table. Both items display correctly.

<table>
<tr>
  <td>ID:</td>
  <td><%=(rsRecordset.Fields.Item("fld_id").Value)%></td>
</tr>
<tr>
  <td>Name:</td>
  <td><%=(rsRecordset.Fields.Item("fld_name").Value)%></td>
</tr>
<tr>
  <td>Email:</td>
  <td><%=(rsRecordset.Fields.Item("fld_email").Value)%></td>
</tr>
<tr>
  <td>IP:</td>
  <td><%=(rsRecordset.Fields.Item("fld_ip").Value)%></td>
</tr>
</table>


In this example, I am calling the fields out of order. Only fld_ip displays correctly. fld_name does not display at all again I presume that this is because fld_name comes BEFORE fld_ip.


<table>
<tr>
  <td>ID:</td>
  <td><%=(rsRecordset.Fields.Item("fld_id").Value)%></td>
</tr>
<tr>
  <td>IP:</td>
  <td><%=(rsRecordset.Fields.Item("fld_ip").Value)%></td>
</tr>
<tr>
  <td>Name:</td>
  <td><%=(rsRecordset.Fields.Item("fld_name").Value)%></td>
</tr>
<tr>
  <td>Email:</td>
  <td><%=(rsRecordset.Fields.Item("fld_email").Value)%></td>
</tr>
</table>

In addition to this, I am not able to display fields more than once.


<table>
<tr>
  <td>ID:</td>
  <td><%=(rsRecordset.Fields.Item("fld_id").Value)%></td>
</tr>
<tr>
  <td>Name:</td>
  <td><%=(rsRecordset.Fields.Item("fld_name").Value)%></td>
</tr>
<tr>
  <td>Email:</td>
  <td><%=(rsRecordset.Fields.Item("fld_email").Value)%></td>
</tr>
<tr>
  <td>IP:</td>
  <td><%=(rsRecordset.Fields.Item("fld_ip").Value)%></td>
</tr>
<tr>
  <td>Name:</td>
  <td><%=(rsRecordset.Fields.Item("fld_name").Value)%></td>
</tr>
</table>


The second instance of the field “fld_name” will not display at all.

With MS Access I could display the data in any order I wanted. I could also display them multiple times without any problems.

The BIG QUESTION… Why on Earth would MSSQL behave this way?

Now, I realize that I could easily assign the data to variables and use them any way I wanted to like this:

<% 
varID = (rsRecordset.Fields.Item("fld_id").Value)
varName = (rsRecordset.Fields.Item("fld_name").Value)
varEmail = (rsRecordset.Fields.Item("fld_email").Value)
varIP = (rsRecordset.Fields.Item("fld_ip").Value)
%>

That’s great but not necessarily the most efficient way to reduce code.

So what is the deal with the limitation on this more advanced DB? What am I missing?

Thanks!

Dean
Hi Dean,
I think the problem may be with the type of connection you are making with MS SQL Server. The behaviour you describe sounds like the behaviour you get from a connection using a 'forward only' cursor. This is the default cursor type when you call the Execute method of the Command and Connection objects. To provide the functionality you are used to with Access use the static, keyset or dynamic cursor. The forward-only cursor is optimized for performance - a case of 'horses for courses'

Regards

Rob
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RobStocks is offline Offline
7 posts
since Jun 2006
Jan 15th, 2007
0

Re: MSSQL Fields Out Of Order

Click to Expand / Collapse  Quote originally posted by RobStocks ...
Hi Dean,
I think the problem may be with the type of connection you are making with MS SQL Server. The behaviour you describe sounds like the behaviour you get from a connection using a 'forward only' cursor. This is the default cursor type when you call the Execute method of the Command and Connection objects. To provide the functionality you are used to with Access use the static, keyset or dynamic cursor. The forward-only cursor is optimized for performance - a case of 'horses for courses'

Regards

Rob
Hi Rob!

You are right on the money. I have found out this very thing from another forum I posted the problem in as well. But there is something else that I need to address and that is the different objects involved.

* Command Object
* Connection Object
* Recordset Object

I use Dreamweaver v8.0.2 to develop and when you create a new Recordset (Query) it automatically writes the best possible code for you as I am sure you might be aware. Here is a sample of what it codes:

MS SQL Syntax (Toggle Plain Text)
  1. <%
  2. Dim Recordset1
  3. Dim Recordset1_cmd
  4. Dim Recordset1_numRows
  5.  
  6. SET Recordset1_cmd = Server.CreateObject ("ADODB.Command")
  7. Recordset1_cmd.ActiveConnection = MM_ADSystemSQL_STRING
  8. Recordset1_cmd.CommandText = "SELECT * FROM tblGBook"
  9. Recordset1_cmd.Prepared = true
  10.  
  11. SET Recordset1 = Recordset1_cmd.Execute
  12. Recordset1_numRows = 0
  13. %>

As you can see, DW by default uses the Command object and from what I can tell, there is no way to use the CursorType directive with the Command object alone.

Different sources of information on the subject offer different explanations of these objects. For instance, one source indicated that when using the Command object to connect to a database that the Connection object is automatically called into the scenario. In addition to this, using a SELECT statement with a Command object calls the Recordset object as well which I interpreted to mean that all three objects are in use despite only coding the Command object.

Still, the only way it seems possible to specify CursorType is to write in a call to the Recordset object and then specify the CursorType. Typically in these scenarios, you would not code the Command object at all, instead, you would write calls to the Connection and Recordset objects solely.

Why does DW do this?

I have looked into developing a custom Server Behavior but there is no way to mimic the complete automation of DW's default Recordset (Query) behavior. I was successful in getting it to mimic the default behavior to an extent using my default code, special parameters and such. But I cannot get it to automatically create a Connection script and place it into the Connections folder or create a folder for that matter. Nor can I duplicate the tables column selection feature with radio buttons (All, Selected) or the filter and sort features.

Here is a screen shot of DW's default Recordset behavior:

http://aboutdean.com/images/subject/...dwrsquery1.jpg


This is the closest I can get to duplicating it:

http://aboutdean.com/images/subject/...dwrsquery2.jpg


DW gives you a plethora of options that you can use to create these windows:

http://aboutdean.com/images/subject/...dwrsquery3.jpg

But there is no automated means of making many of them work without actually editing the EDML files that contain their logic.

This is certainly off topic and more work than I need. However, the prospect is great if you can actually get it to code what you need on the fly. Unfortunately, not without digressing from your true work to learn how DW works, EDML etc. :rolleyes:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DVHost is offline Offline
20 posts
since Apr 2004
Jan 17th, 2007
0

Re: MSSQL Fields Out Of Order

Dean,
I do not have any experience with Dreamweaver. I had a quick search on the internet and found an article that says that the cursor type can be changed by 'selecting the recordset in Server Behaviors window and then look in the Property Inspector. The properties of the recordset can easily be changed by selecting different values in the dropdowns'

I'm not sure that this is the information you were looking for

Regards

Rob
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RobStocks is offline Offline
7 posts
since Jun 2006
Jan 17th, 2007
0

Re: MSSQL Fields Out Of Order

Hi Rob, hey thanks for the effort. The 8.02 release of Dreamweaver updated the server-side code generated by Dreamweaver to protect databases against SQL Injection. Unfortunately, Dreamweaver 8.02 no longer allows the database property called CursorLocation to be set from the Property inspector.

Would have been sweet though!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DVHost is offline Offline
20 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Access & MS SQL Server 2005
Next Thread in MS SQL Forum Timeline: Test Connectivity





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC