循环遍历sql查询返回的所有行

时间:2014-05-27 06:21:33

标签: sql sql-server tsql stored-procedures sql-server-2012

我正在运行一个问题,我正在运行一个查询,它返回我想要的多行,完美。我不得不向移动设备发送消息,因此我为其编写了程序,但问题是我无法向不同的移动设备发送消息,即不同行返回的消息。简而言之,我的意思是我想将消息发送到查询返回的所有数字。

查询:

SELECT CL.[Subject], D.Name as Designation, D.MobileNo, CL.LetterNo, 
'Please Reply To Our Letter No : ' + convert(varchar(50), CL.LetterNo) as SMSMessage
FROM CPOCMS.dbo.ComposedLetter CL 
JOIN CPOCMS.dbo.Designations D ON CL.Designation_ID = D.DesignationID
WHERE CONVERT(date,CL.DeadLineDate) = CONVERT(date,DATEADD(DAY,1,GETDATE()))

Exec dbo.ADD_SMS_InQueue 'Requires number', 'requires message'

1 个答案:

答案 0 :(得分:1)

DECLARE @Subject VARCHAR(10)--.....Declare used variable here...,
DECLARE vendor_cursor CURSOR FOR
  SELECT CL.[subject],
         D.name                              AS Designation,
         D.mobileno,
         CL.letterno,
         'Please Reply To Our Letter No : '
         + CONVERT(VARCHAR(50), CL.letterno) AS SMSMessage
  FROM   cpocms.dbo.composedletter CL
         JOIN cpocms.dbo.designations D
           ON CL.designation_id = D.designationid
  WHERE  CONVERT(DATE, CL.deadlinedate) = CONVERT(DATE, Dateadd(day, 1, Getdate(
                                                        )))

OPEN vendor_cursor

FETCH next FROM vendor_cursor INTO @Subject, @Designation, @MobileNumber,
@LetterNo, @SMSMessage

WHILE @@FETCH_STATUS = 0
  BEGIN
      EXEC dbo.Add_sms_inqueue
        @MobileNumber,
        @SMSMessage

      FETCH next FROM product_cursor INTO @Subject, @Designation, @MobileNumber,
      @LetterNo, @SMSMessage
  END

CLOSE product_cursor

DEALLOCATE product_cursor