Click here to Skip to main content
15,903,201 members
Home / Discussions / Database
   

Database

 
AnswerRe: create forenkey syntex Pin
Colin Angus Mackay25-May-06 9:41
Colin Angus Mackay25-May-06 9:41 
GeneralRe: create forenkey syntex Pin
vipinpaliwal198025-May-06 17:54
vipinpaliwal198025-May-06 17:54 
QuestionDbf Tables & Sql Strings Pin
Vinicius Pontes23-May-06 10:11
Vinicius Pontes23-May-06 10:11 
AnswerRe: Dbf Tables & Sql Strings Pin
Eric Dahlvang24-May-06 3:06
Eric Dahlvang24-May-06 3:06 
Question-- Is it possible??? -- Pin
WetRivrRat23-May-06 9:56
WetRivrRat23-May-06 9:56 
AnswerRe: -- Is it possible??? -- Pin
Eric Dahlvang24-May-06 3:36
Eric Dahlvang24-May-06 3:36 
GeneralRe: -- Is it possible??? -- Pin
WetRivrRat24-May-06 4:01
WetRivrRat24-May-06 4:01 
AnswerRe: -- Is it possible??? -- Pin
Eric Dahlvang24-May-06 8:35
Eric Dahlvang24-May-06 8:35 
There's probably a better way, but I had fun with this.
It isn't pretty and I don't know how it will perform, - but here is one solution:

DECLARE @iColumns INT, @iCounter INT, @sql VARCHAR(2500) 

SET @iCounter = 1
SET @sql = ''

select @iColumns = (SELECT  TOP 1 COUNT(*) AS cnt FROM phonenumbers GROUP BY empid ORDER BY cnt DESC)

WHILE (@iCounter <= @iColumns)
BEGIN
	SET @sql = @sql + ', Phone' + CAST(@iCounter AS VARCHAR) + ' VARCHAR(12)'
	SET @iCounter = @iCounter + 1
END

SET @sql =  'CREATE TABLE #CrossPhones (empid INT' + @sql + ');'
 +  'DECLARE @sql2 VARCHAR(100),@iCounter INT,@iEmpIDHolder INT, @iEmpID INT, @phone_num VARCHAR(12), @teltype INT;'
 +  'DECLARE curNumbers CURSOR FOR SELECT empid,phone_num,teltype FROM phonenumbers p ORDER BY empid,teltype;'
 +  'OPEN curNumbers;'
 +  'FETCH NEXT FROM curNumbers INTO @iEmpID, @phone_num, @teltype;'
 +  'WHILE @@FETCH_STATUS = 0'
 +  'BEGIN'
 +  '   SET @iEmpIDHolder = @iEmpID;'
 +  '   INSERT INTO #CrossPhones (empid) VALUES (@iEmpID);'
 +  '   SET @iCounter = 0;'
 +  '   WHILE @iEmpIDHolder = @iEmpID'
 +  '   BEGIN'
 +  '	 SET @iCounter = @iCounter + 1;'
 +  '	 SET @sql2 = ''UPDATE #CrossPhones SET Phone'' + CAST(@iCounter AS VARCHAR) + ''='''''' + CAST(@phone_num AS VARCHAR) + '''''' WHERE empid='' + cast(@iEmpID as VARCHAR);'
 +  '     EXEC(@sql2);'
 +  '	 FETCH NEXT FROM curNumbers INTO @iEmpID, @phone_num, @teltype;'
 +  '	 IF @@FETCH_STATUS != 0 BREAK;'
 +  '   END;'
 +  'END;'
 +  'CLOSE curNumbers;'
 +  'DEALLOCATE curNumbers;'
 +  'SELECT e.name,c.* FROM #CrossPhones c INNER JOIN Employees e ON c.empid = e.empid;'

EXEC(@sql)


----------
There go my people. I must find out where they are going so I can lead them.
- Alexander Ledru-Rollin

AnswerRe: -- Is it possible??? -- [modified] Pin
Michael Potter24-May-06 3:41
Michael Potter24-May-06 3:41 
GeneralRe: -- Is it possible??? -- Pin
WetRivrRat24-May-06 4:16
WetRivrRat24-May-06 4:16 
GeneralRe: -- Is it possible??? -- Pin
Eric Dahlvang24-May-06 9:31
Eric Dahlvang24-May-06 9:31 
QuestionPls Help MS Access Pin
achrafus23-May-06 8:46
achrafus23-May-06 8:46 
QuestionSQL Express to Access Pin
Kschuler23-May-06 8:29
Kschuler23-May-06 8:29 
AnswerRe: SQL Express to Access Pin
Eric Dahlvang23-May-06 9:29
Eric Dahlvang23-May-06 9:29 
QuestionBest way to get Today in DateTime Pin
Rob Philpott23-May-06 7:07
Rob Philpott23-May-06 7:07 
AnswerRe: Best way to get Today in DateTime Pin
Eric Dahlvang23-May-06 8:00
Eric Dahlvang23-May-06 8:00 
GeneralRe: Best way to get Today in DateTime Pin
Rob Philpott23-May-06 8:07
Rob Philpott23-May-06 8:07 
GeneralRe: Best way to get Today in DateTime Pin
Eric Dahlvang23-May-06 8:29
Eric Dahlvang23-May-06 8:29 
QuestionInserting Image in my Report (ReportViewer) Pin
anderslundsgard23-May-06 4:28
anderslundsgard23-May-06 4:28 
QuestionProgrammatically Updating a Table with Detail from derived from another Table Pin
AnneThorne23-May-06 3:45
AnneThorne23-May-06 3:45 
QuestionData base connection problem Pin
iz723-May-06 2:28
iz723-May-06 2:28 
QuestionSQL Server does not exist or access denied [modified] Pin
sarah_chandran23-May-06 0:57
sarah_chandran23-May-06 0:57 
AnswerRe: SQL Server does not exist or access denied [modified] Pin
sathish s23-May-06 2:11
sathish s23-May-06 2:11 
GeneralRe: SQL Server does not exist or access denied [modified] Pin
sarah_chandran23-May-06 3:14
sarah_chandran23-May-06 3:14 
Questionwriting query using comma separator Pin
dayakar_dn23-May-06 0:09
dayakar_dn23-May-06 0:09 

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.