Problems with SQL Server string concatenation and disassembling table queries

Source: Internet
Author: User
Tags rtrim

One, SQL based on a comma-delimited ID string of the type of person GUID to find its corresponding name is also stitched together into a comma-separated string:

1, demand: The administrator sends the notice (the notice divides into the general notice, the prize order, the questionnaire three types) to the user, and can view the statistic which person has viewed, which has not been viewed.

2. database table design: Notice form, Personnel table, Prize order form, questionnaire 3 tables, user notification form

3, SQL Statement inquiry has been sent notification information:

SelectA.*, E.noread,e.alread,tousername= STUFF((SELECT ',' +Name fromUsers Dwhere   PATINDEX('%,' + RTRIM(account)+ ',%',',' +A.touserid+ ',')>0                         ORDER  by PATINDEX('%,' + RTRIM(account)+ ',%',',' +A.touserid+ ',')--plus Sort the same (for example: 2,1,3)                        forXML PATH ("')),1,1,"'),
CaseA.noticetypeid when 2 Then 'Survey Questionnaire' when 3 Then 'Prize Order' Else 'General Notice' End asNoticetypename,b.awardordertitle,
B.awardorderdescription,c.questionnairetitle fromnotices a Left JoinAwardorders b onA.awardorderid=b.ID andB.status=1 Left JoinQuestionnairesinfor C onA.questionnaireid=C.id andC.status=1 Left Join(SelectNoticeid,COUNT(1) asSumcount,noread=sum( Case [Status] when 1 Then 1 Else 0 End),
Alread= sum( Case [Status] when 2 Then 1 Else 0 End)
fromUsernoticesGroup byNoticeid) E onA.noticesid=E.noticeidwhereA.status=1

The notification is sent to the user notification table according to the ID string of the comma-separated user GUID type stored in field Touserid:

1. Requirement: Distribute the notification to individual users (save to user table according to user's stripe)

2, the database design the same

3. SQL Send notification:

Insert  intoUsernotices (Id,noticeid,noticetype,awardsorderid,questionnaireid,creatmanaccount,creatmanname,creatdate,[Status]) ( Select NEWID(), Noticesid,noticetypeid,awardorderid,questionnaireid,b.account,b.name,GETDATE(),1 
fromNotices a Left JoinUsers b on PATINDEX('%,' + RTRIM(account)+ ',%',',' +A.touserid+ ',')>0
whereA.noticesid=@NoticeID)--@NoticeID is the parameter notification ID

Problems with SQL Server string concatenation and disassembling table queries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.