Solved

Help with update query

Posted on 2007-03-23
5
163 Views
Last Modified: 2010-03-20
Hello,

I have a particular field in a sql database that is text and I went to use it to log information.  So what I am asking is what is the syntax for adding lines to existing data in a field?  How would I write the update query to add text the the data in the existing field so it can appear as a log of events.  Thanks your help.
0
Comment
Question by:2326ac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18778580
what database?

most of the times:
UPDATE yourtable
  SET yourfield = yourfield & "new data"

the & could be + or ||  ... depends on the database type you are using.

note however that when really want to make a kind of "log", you should make each log entry 1 row in a log table.
0
 

Author Comment

by:2326ac
ID: 18779057
Thank you I have tried your suggestion - see below:  

I get an error at cmd,executeNonQuery

Invalid Opertator for data type.  Operator equals add, type equals text.

Dim cmd As New SqlCommand("Update tblReqMain set Status = 'Approved', LManComments = '" + StrCom + "', ProbCat = '" + strProbCat + "', AuthName = '" + LbUsername.Text + "', History = History + '*** Authorised by line manager' Where ReqID = " & StrID, New SqlConnection(strconn))

All fields are either Text or VarChar
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18779115
if the field is TEXT (so you are using SQL Server), that is the problem, as you CANNOT update TEXT/NTEXT fields like that. you have to use UPDATETEXT command to update that field (the other fields can be updated normally!)

as noted above, using this as "log" is a conceptual and practical BAD idea, you should not do it that way.

if you have SQL Server 2005, you could use a workaround by using VARCHAR(MAX) instead of TEXT, then the update will work that way. (using + instead of & )
0
 

Author Comment

by:2326ac
ID: 18779922
There will only be a max three actions in this log of about a few lines each.  Thank you all for your advice.  I have tried the suggestion of updatetext (see below) but I get a syntax error near the value of strcom

Dim cmd2 As New SqlCommand("UPDATETEXT tblReqMain.history '" + StrCom + "' WHERE ReqID = " & StrID, New SqlConnection(strconn))
        cmd2.Connection.Open()
        cmd2.ExecuteNonQuery()
        cmd2.Connection.Close()

Any advice would be great
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18780404
you did not read the sample in the documentation, did you?
http://msdn2.microsoft.com/en-us/library/aa260658(SQL.80).aspx
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question