Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Wednesday, September 22, 2010

SQL Server - Stored Procedure returning table with dynamic columns

We had a stored procedure that returns some data, later we wanted to add some columns to this data, and these columns are dynamic.
We found several articles on how to use pivot to create dynamic columns in tables, for example
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx

and others,

All using the same method, constructing the column headers in a variable, then creating inline sql and using pivot statement and this variable as the columns

In our case we had already a very big select statement, that we didn't want to alter, we just wanted to add more columns to it.

We thought of using table user defined function, but then we had to define the table columns, which we can't do, as it should be dynamic.

We thought also of using a view, then to join with this view, but we can't use pivot in views

We thought also of using a separate stored procedure and calling this stored procedure from the original one, but we found that this is not supported solution.

Finally we tried something and it worked, we alerted the original stored procedure like the following: We inserted the output of the first select statement into a temp table, and then added the second select and pivot in an inline sql statement, and in this statement we inner joined with our temp table.

An example of what we did

Suppose the first statement is like the following

Select * from table1

and our pivot statement is like the following


@query = 'select * from table2 pivot (count(columnName1) for ColumnContainingData in (' + @columnNames + ')'

where columnsName1 is the column that we will perform the aggregate function on, @columnNames is a variable with column names comma seperated and surrounded with []

the final statement looked lke the following

select * into #tempTable from
(select Select * from table1) t
@query = 'select * from table2 pivot (count(columnName1) for ColumnContainingData in (' + @columnNames + ') t2 inner join #tempTable on #tempTable .SomeId = t2.SomeId'

Handling comma separated values parameter in a stored procedure without using inline sql

I have seen lots of implementation for comma separated values parameters, all using inline SQL to construct the query.
for example:
Suppose the parameter is @param having the value '1,2,3'
and we are selecting rows with state in (1,2,3)

Then the query would look like this

query='select * from TableName where state in (' + @param + ')'
exec query

Since i don't like inline SQL, i was looking for another methods

I found this method somewhere, i don't remember where exactly. It is based on constructing a table, and inserting the comma separated values in this  table, then in the query either we can inner join with this table to get the desired values only or we can use where clause

example on constructing this temp table



declare @param nvarchar(50)
set @param = N'1,2,3,4'
IF EXISTS
(SELECT * FROM tempdb.dbo.sysobjects WHERE ID = OBJECT_ID(N'tempdb..#temptable'))
BEGIN
DROP TABLE #temptable
END

CREATE TABLE #temptable(Code int)

   DECLARE @code varchar(10), @Pos int

    SET @param = LTRIM(RTRIM(@param))+ ','
    SET @Pos = CHARINDEX(',', @param, 1)

    IF REPLACE(@param, ',', '') <> ''
    BEGIN
        WHILE @Pos > 0
        BEGIN
                SET @code = LTRIM(RTRIM(LEFT(@param, @Pos - 1)))
                IF @code <> ''
                BEGIN
                        INSERT INTO #temptable (code)
                        VALUES (CAST(@code AS int)) --Use Appropriate conversion
                END
                SET @param = RIGHT(@param, LEN(@param) - @Pos)
                SET @Pos = CHARINDEX(',', @param, 1)

        END
    END
--select * FROM #temptable

Next in the main query, it can be like this

Select * FROM TableName
INNER JOIN #temptable on TableName.State = #temptable.code

or

Select * FROM TableName
where state in (select code from #temptable)