You must Sign In to post a response.
  • Category: SQL Server

    Unable to update desired rows

    Hi,

    I have the following query which is executed in a single string through C#.

    SET @out_param := '';
    SELECT
    sightinguid
    FROM
    pc
    INNER JOIN
    c ON uid = id
    WHERE
    //... other conditions
    AND (@out_param:=CONCAT_WS(',', sightinguid, @out_param))
    LIMIT 50 FOR UPDATE;
    UPDATE pc
    SET
    last_accessed_timestamp = NOW()
    WHERE
    sightinguid IN (@out_param);
    SELECT @out_param;

    I am basically trying to put the first 50 values of the first query in a comma separated string, and return this string at the end. Before doing so, I would like the update statement to execute on those same records. However, only the very first sightinguid is being updated. When I hardcode multiple values in the sightinguid IN (@out_param) part it works and updates them all - so I am assuming there is something wrong with that part.

    I cannot put the SELECT in a subquery and update from there, due to the LIMIT 50 part, since MySQL does not let you put a LIMIT in a subquery.

    Any ideas?

    Thank You
    kosmiktechnologies.com
  • #769517
    I have prepared the same what you need with my temp tables,

    DECLARE @Codes VARCHAR(100) = ''
    SELECT @Codes = CASE WHEN @Codes = '' THEN CONVERT(VARCHAR(100),EmployeeID) ELSE @Codes+','+CONVERT(VARCHAR(100),EmployeeID) END
    FROM Employees

    DECLARE @MyQuery VARCHAR(MAX) = ''
    SET @MyQuery = 'update Employees set UpdatedDate = getdate() WHERE EmployeeID IN('+@Codes+')'

    EXEC (@MyQuery)

    You have to keep write all of this in stored procedure and call that stored procedure. You don't need to write an inline query.

    It's very easy. In my above query itself, you can just replace your table and field names that will work.

    Let me know if you have any questions.

    Regards,
    V.M. Damodharan
    "Your talent will be worthless, when you have fear and tension."


  • Sign In to post your comments