Dear Masters;
I have a Messages table; I use this table to post System Messages to my users. But what I don't know is how can I add data for multiple users. I mean I want to add same message for multiple users (ex: Please update your infos). In the belove table I have some messages for users 100 and 200; How can I add same (please update your infos) messages to both users?
Ex: Messages Table
ID UserID Msg
1 100 "Hello"
2 100 "Hi"
3 200 "Hello"
Thanks..
try this to add your message to current message
UPDATE Messages
SET msg=msg + ' Please update your infos '
where userID in (100,200)
or this to create new content for message
UPDATE Messages
SET msg = 'Please update your infos '
where userID in (100,200)
Thanks
|||
Thanks for repliying;
But may I write a select statement for example in the in() section?
Cause I may need to add for all users so I gues it should be something like "for" statement.. But if I can write select statement inside in() That may solve...
I'll try thanks
|||Yes, you can try for example
where userID in (select UserID from messages where userID>100)
but your select statement should return only one column.
Thanks
|||I don't think you want an update statement for this purpose. If you update where UserID = 100, you will update both records 1 and 2. You will need to do multipl einsert statements. You can do this inside a stored procedure that you pass paramaters to (ie. parameter 1 is the message and parameter 2 is a comma seperated list of user ids). If you are not comfortable with stored procedures, you will need to construct and execute multiple insert statements in your code. Don't forget to use a transaction in case of failure.|||
Hi;
I guess I'm a little bit confused:) Could you explain me more deeply? I think I'm ok with stored procedures but what should this stored procedure include?
don't you think if J's solution works? I mean why shloud it update both records?
Thanks
|||
In your example table structure you have the same userID for 2 first records so f you do update for userID=100 both records will be updated. If you would like to do it for specific one you have to use ID column to select only one record. It was mistake or you can have two entries for the same userID in your table?
Thanks
|||Oh i c;
Thanks, I will actually insert a new row I'm not gonna update so problem solved thanks...
Thanks to both masters;)
|||Here is an article on how to write a stored proc to take a comma seperated value (CSV) list into a stored proc. You should be able to adapt this for your own needs.
No comments:
Post a Comment