Question: I have data as follow

```
DECLARE @tmp TABLE(cid int, colspan int, rowspan int, corder int)
INSERT INTO @tmp
SELECT 1,2,null,1
UNION
SELECT 2,null,null,2
UNION
SELECT 3,null,null,3
UNION
SELECT 4,3,null,4
UNION
SELECT 5,null,null,5
UNION
SELECT 6,null,null,6
UNION
SELECT 7,null,null,7

```

I want to query to return

```
cid   colspan  rowspan  corder
-------------------------------------
1      2       null      1
3      null    null      3
4      3       null      4
7      null    null      7

```

Records will be ordered by corder and then using colspan value skip next records (if colspan = 2 skip next 1, if 3 skip next 2). Is this possible to do using query?

Why I want this data -
I'm trying to bind this data to repeater control (table template) to create a dynamic table and when there is colspan > 0 i dont want my repeater to generate td items so that it will span correctly.

Answer: 
Try using a recursive CTE, like so:

```
with cte as (
select t.* from @tmp t where corder = 1
union all 
select t.* from @tmp t join cte c on t.corder = (c.corder + coalesce(c.colspan,1))
)
select * from cte

```
You can use CLR Table Valued functions that returns the actual table. CLR functions are functions you can write in your native language C#, VB etc. and the dll are embedded in the database.

Learn more about it from here:

<http://msdn.microsoft.com/en-us/library/ms131103.aspx>