Click here to Skip to main content
15,918,976 members
Home / Discussions / Database
   

Database

 
Questionhow i run the .sql file in either microsoft SQL 2005? Pin
sherjeel28-Aug-07 6:09
sherjeel28-Aug-07 6:09 
AnswerRe: how i run the .sql file in either microsoft SQL 2005? Pin
Mike Dimmick28-Aug-07 9:38
Mike Dimmick28-Aug-07 9:38 
Questioncan't find childrows.... Pin
gericooper28-Aug-07 6:04
gericooper28-Aug-07 6:04 
AnswerRe: can't find childrows.... Pin
gericooper28-Aug-07 19:52
gericooper28-Aug-07 19:52 
QuestionTranspose in SQL Pin
Elizma28-Aug-07 1:54
Elizma28-Aug-07 1:54 
AnswerRe: Transpose in SQL Pin
Pete O'Hanlon28-Aug-07 2:21
mvePete O'Hanlon28-Aug-07 2:21 
GeneralRe: Transpose in SQL Pin
Elizma28-Aug-07 3:37
Elizma28-Aug-07 3:37 
AnswerRe: Transpose in SQL Pin
John-ph29-Aug-07 3:49
John-ph29-Aug-07 3:49 
Create table tblMatrix(Column1 int,Column2 char,Column3 int)

Create Procedure sp_Transpose As
DECLARE @SQLQuery AS NVARCHAR(2000)
DECLARE @ColumnName Nvarchar(50)
DECLARE @AddedColumns Nvarchar(500)
DECLARE @AddedValues Nvarchar(500)
DECLARE @Col1Val Nvarchar(50)
DECLARE @Col2Val Nvarchar(50)
DECLARE @Val2Add Nvarchar(50)

declare NavigatorRecords cursor for
SELECT DISTINCT(COLUMN2) FROM TBLMATRIX

open NavigatorRecords 
fetch next from NavigatorRecords into @ColumnName 

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblTemp]') and OBJECTPROPERTY(id, N'IsTable') = 1)
drop table dbo.tblTemp

SET @SQLQuery = 'CREATE TABLE tblTemp(Col Int,'
SET @AddedColumns = 'Col,'
SET @AddedValues = ''
SET @Val2Add = ''

while(@@FETCH_STATUS = 0) 
begin
SET @AddedColumns = @AddedColumns + @ColumnName
SET @SQLQuery = @SQLQuery + @ColumnName + ' ' + 'NVARCHAR(50)'
fetch next from NavigatorRecords into @ColumnName 
IF (@@FETCH_STATUS = 0)
begin
SET @SQLQuery = @SQLQuery + ','
SET @AddedColumns = @AddedColumns + ','
end
end

SET @SQLQuery = @SQLQuery + ')'
EXEC sp_executesql @SQLQuery 

close NavigatorRecords
deallocate NavigatorRecords


declare CursorI cursor for
SELECT DISTINCT(COLUMN1) FROM TBLMATRIX

open CursorI 
fetch next from CursorI into @Col1Val

while(@@FETCH_STATUS = 0) 
begin
SET @AddedValues = @Col1Val + ','
declare CursorJ cursor for
SELECT DISTINCT(COLUMN2) FROM TBLMATRIX

open CursorJ 
fetch next from CursorJ into @Col2Val

while(@@FETCH_STATUS = 0) 
begin
set @Val2Add = '0'
select @Val2Add = Isnull(column3,'0') from tblMatrix where column1 = @Col1Val and column2 = @Col2Val
SET @AddedValues = @AddedValues + @Val2Add
fetch next from CursorJ into @Col2Val
IF (@@FETCH_STATUS = 0)
SET @AddedValues = @AddedValues + ','
end

SET @SQLQuery = 'INSERT INTO tblTemp(' + @AddedColumns +') values(' + @AddedValues +')'
EXEC sp_executesql @SQLQuery 

close CursorJ
deallocate CursorJ

fetch next from CursorI into @Col1Val
end
close CursorI
deallocate CursorI

Select * from tblTemp

Go

Exec sp_transpose 



Regards
J O H N Rose | [Rose]

"Even eagles need a push." David McNally

Question. MDF Pin
plural28-Aug-07 1:37
plural28-Aug-07 1:37 
AnswerRe: . MDF Pin
Colin Angus Mackay28-Aug-07 2:32
Colin Angus Mackay28-Aug-07 2:32 
GeneralRe: . MDF Pin
plural28-Aug-07 2:41
plural28-Aug-07 2:41 
QuestionGetting Last Day of Month: Pin
Shahzad.Aslam28-Aug-07 1:30
Shahzad.Aslam28-Aug-07 1:30 
AnswerRe: Getting Last Day of Month: Pin
mihirhp28-Aug-07 1:53
mihirhp28-Aug-07 1:53 
GeneralRe: Getting Last Day of Month: Pin
Shahzad.Aslam28-Aug-07 3:34
Shahzad.Aslam28-Aug-07 3:34 
QuestionRadioButtons are not working Pin
RaveRare27-Aug-07 18:46
RaveRare27-Aug-07 18:46 
AnswerRe: RadioButtons are not working Pin
Widgets30-Aug-07 22:43
Widgets30-Aug-07 22:43 
Questionsqlserver and msoffice Pin
kalyan_241627-Aug-07 18:13
kalyan_241627-Aug-07 18:13 
QuestionUsing SqlFacet Attributes for an SQL function Pin
~~~Johnny~~~27-Aug-07 11:17
~~~Johnny~~~27-Aug-07 11:17 
QuestionSELECT MAX Pin
RichardBerry27-Aug-07 6:16
RichardBerry27-Aug-07 6:16 
QuestionRe: SELECT MAX Pin
Kschuler27-Aug-07 9:59
Kschuler27-Aug-07 9:59 
AnswerRe: SELECT MAX Pin
RichardBerry27-Aug-07 21:55
RichardBerry27-Aug-07 21:55 
GeneralRe: SELECT MAX Pin
Kschuler30-Aug-07 4:24
Kschuler30-Aug-07 4:24 
GeneralRe: SELECT MAX [modified] Pin
RichardBerry30-Aug-07 4:53
RichardBerry30-Aug-07 4:53 
AnswerRe: SELECT MAX Pin
Chris Meech27-Aug-07 10:45
Chris Meech27-Aug-07 10:45 
GeneralRe: SELECT MAX Pin
RichardBerry27-Aug-07 22:00
RichardBerry27-Aug-07 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.