Structuring a good INSERT INTO statement

Results 1 to 3 of 3

Thread: Structuring a good INSERT INTO statement

  1. #1
    cinD Guest

    Default Structuring a good INSERT INTO statement

    I&#039m just wondering how some of you would go about inserting new values into a table in SQL Server in a certain situation. If my entry was straightforward with no conditions, I would likely use something like:<BR><BR>sql = "INSERT INTO Tablename (Field1, Field2) VALUES (&#039" & txt1 & "&#039," & txt2 & ")"<BR><BR>Let&#039s say, though, that I&#039m using a form that has a few radio buttons where each value represents a different bit-type field in the table. So if radio button #1 is checked, then I&#039d like the corresponsing field to be checked. Likewise with the other values/fields. Can I achieve this with an INSERT INTO statement or will I have to stick to my usual method, which is:<BR>myRS.AddNew<BR>If Request("radiobutton")="1" Then<BR>myRS("field1") = 1<BR>ElseIf Request("radiobutton")="2" Then<BR>myRS("field2") = 1<BR>ElseIf Request("radiobutton")="3" Then<BR>myRS("field3")= 1<BR>End If<BR><BR>Thanks for any help!<BR>

  2. #2
    Join Date
    Dec 1969

    Default One condition...

    Conditionally build your SQL String<BR><BR>Select Case Request.Form("RadioButton")<BR>Case "1"<BR>sFieldList = "Field1"<BR>Case "2"<BR>sFieldList = "Field2"<BR>Case "3"<BR>sFieldList = "Field3"<BR>End select <BR>sSql = "INSERT INTO Tablename (" & sFieldList & ") VALUES(1)"<BR><BR>your sample code didn&#039t seem to reqire it but you could also build the Value List in the same Select Case construct ...<BR><BR>HTH<BR><BR><BR>

  3. #3
    cinD Guest

    Default RE: One condition...

    Good thinking. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts