Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
cognos79
Posting Yak Master
241 Posts |
Posted - 2007-07-25 : 15:27:23
|
Below is the query that works well and I want to add new rows to result set. Create table #Test(BatchID int ,Status int)insert into #test select 1001 ,1insert into #test select 1001 ,2insert into #test select 1002 ,0insert into #test select 1002 ,3insert into #test select 1002 ,4insert into #test select 1003 ,4insert into #test select 1004 ,4declare @Status varchar(50)set @status = ''Select @Status = @Status + '[' + convert(varchar(2),a.status )+ '],' from (Select distinct a.Status from #Test a) aSET @Status = LEFT(@Status, LEN(@Status) - 1)exec('SELECT * FROM #test p PIVOT( count(Status) for status in (' + @Status + ')) AS pvt ORDER BY BatchId')drop table #testwhat I am looking for is to add new rows to this resultset.if you look at the result below the i want new rows newid, newid1 with 0's under all the columns. The batchids are not in the temp table they come for different table. The result set shld look like this. 0 1 2 3 4------------------------1001 0 1 1 0 01002 1 0 0 1 11003 0 0 0 0 11004 0 0 0 0 1newid 0 0 0 0 0newid2 0 0 0 0 0 Thanks, |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-25 : 15:35:38
|
Add "UNION ALL SELECT BatchId, 0, 0, 0, 0, 0 FROM YourBatchTable" to your SELECT statement in the EXEC.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
|
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2007-07-25 : 15:43:38
|
I dont know how many columns I have in the resultset. It changes everytime. |
|
|
|
|
|
|
|