Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72

    Unanswered: Variable as Field Name in CURSOR FOR UPDATE

    I'm trying something like:

    UPDATE tbl SET @varFieldName = @varValue

    The procedure runs, and when I PRINT @varFieldName, it looks fine, but the table isn't getting updated, and no errors, wierd.

    I have the CURSOR open for update, but I didn't list the field names, that shouldn't be a problem, as all fields should be updateable then.

    To get the field name, I :

    SET @varFieldName = 'SomeChars' + LTRIM(STR(asmallint)) + 'SomeMoreChars'

    Thanks,
    Carl

  2. #2
    Join Date
    Apr 2004
    Location
    Kansas City, MO
    Posts
    734
    You would have to use dynamic SQL for this and EXEC(@sqlstring) or use sp_executesql. Look up the terms in books online. Basically, you will need to create a string that, when you run SELECT @sqlstring, looks like your entire update statement. If you EXEC that string, you will get the actual update to occur.
    MeanOldDBA
    derrickleggett@hotmail.com
    When life gives you a lemon, fire the DBA.

  3. #3
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    Thanks for the prompt reply Derrick. I figure i was simply setting some variables, not updating the table, that's why there were no errors.

    Used the EXEC (@sqlString), works lovely, now I just have to tidy up the rest of the slop.

    Carl

Posting Permissions

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