the query is as below.. dynamic query is taken from some other table.. Please suggest what logic can be applied before deciding on to convert to xml.. I am not able to find the solution..
DECLARE @istrType VARCHAR(8);
DECLARE @strSQL NVARCHAR(MAX);
DECLARE @strParam VARCHAR(MAX);
DECLARE @intRows INT;
DECLARE @intLoop INT;
DECLARE @ItemCode VARCHAR(50);
SET @istrType='daily';
DECLARE @tblQuery TABLE
(RunId INT IDENTITY(1,1),
ID VARCHAR(20),
ItemCode VARCHAR(50),
Query VARCHAR(MAX));
INSERT INTO @tblQuery (ID, ItemCode, Query)
SELECT ID, VariableCode, SelectQry FROM [dbo].[Tb_ReportQuery];
SELECT @intRows = COUNT(*) FROM @tblQuery;
SET @intLoop = 1;
WHILE @intLoop <= @intRows
BEGIN
SELECT @ItemCode = ItemCode FROM @tblQuery WHERE RunId = @intLoop;
SELECT @strSQL = Query FROM @tblQuery WHERE RunId = @intLoop;
IF(Output of @strSQL returns more than 1 record)
BEGIN
SELECT @strSQL = '(' + @strSQL + ' FOR XML PATH (''Element''), ROOT(''Root''))';
END
ELSE
BEGIN
SELECT @strSQL = '(' + @strSQL + ')';
END
SET @strSQL = 'Insert Into Tb_Process_Values
(CurDate, Code, ReportType, Value)
Select ''' + GETDATE() + ''',''' + @ItemCode + ''',''' + @istrType + ''',' + @strSQL;
EXEC (@strSQL);
SET @intLoop = @intLoop + 1;
END