0

I am working on a sql server DB mail sending task in which the mail body should be as HTML. Data has to be pulled from two different table.

Ex.

Table -1[Staging table-Rows keeps on adding]

ID      Name    C_Name

 1     john    Mumbai
 2     Adam    pune
 3     Kevin   Delhi

Table -2[Static table,config kind]

FieldID   FieldName      FieldOrder

  1      CustomerName   1
  2      City           2

My expected HTML mail body is:

<table >
    <tr>
        <td>
            CustomerName</td>
        <td>
            john</td>
        <td>
            Adam</td>
        <td>
            Kevin</td>
    </tr>
    <tr>
        <td>
            City</td>
        <td>
            Mumbai</td>
        <td>
            pune</td>
        <td>
            Delhi</td>
    </tr>
 Table rows continues....
</table>

HTML Table Design:

CustomerName John   Adam  Kevin

City         Mumbai Pune  Delhi

Struggling for an optimized query ,please suggest.

4

2 に答える 2

0

このようなカーソルを使用してみてください。

DECLARE @Name nvarchar(50)
DECLARE @City nvarchar(50)

DECLARE @Row1 nvarchar(4000)
DECLARE @Row2 nvarchar(4000)

SET @Row1 = '<tr><td>Customer Name</td>'
SET @Row2 = '<tr><td>City</td>'

DECLARE db_cursor CURSOR FOR  
SELECT Name, C_Name 
FROM Table1

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @Name, @City   

WHILE @@FETCH_STATUS = 0   
BEGIN   
   SET @Row1 = @Row1 + '<td>' + @Name + '</td>'
   SET @Row2 = @Row2 + '<td>' + @City + '</td>'
END   
CLOSE db_cursor   
DEALLOCATE db_cursor

SET @Row1 = @Row1 + '</tr>'
SET @Row2 = @Row2 + '</tr>'

SELECT '<table>' + @Row1 + @Row2 + '</table>'
于 2013-07-29T09:06:36.000 に答える