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 |
|
maleksafa
Starting Member
1 Post |
Posted - 2011-09-29 : 09:05:23
|
| declare @st varchar(max)declare @ss varchar(max)hello, i have a table that contains only one row but many columns, i want to loop on the columns to find the criteria that m looking for:declare @i intdeclare @Ttype varchar(15)declare @v intset @i = 3set @v = 3while (@i < 35)BEGIN--HERE I LOOP ON THE COLUMNS NAMEset @st = (select column_name from information_schema.columns WHERE TABLE_NAME = 'F590036N' and TABLE_SCHEMA = 'TESTDTA' and ORDINAL_POSITION = @i)--FOR EACH COLUMN NAME I INSERT IT IN Ttyope, the problem is that--it's returning the column name and not the column valueselect @Ttype = @st from testdta.f590036n where DSRJOB = 1print (@Ttype) |
|
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2011-09-29 : 11:35:42
|
Have a look at this. It is based on your table name of TESTDTA. If you actual table is called something else, do a search and replace to change all references to this.--drops table if already existsIF Object_ID('tmpFieldNames') IS NOT NULLBEGIN DROP TABLE tmpFieldNamesEND--build a table of field names for the table TESTDTA i the current serverDECLARE @SQLAddToTable VARCHAR(1000)SET @SQLAddToTable = ' SELECT col.Name as FieldName ,ROW_NUMBER() OVER (ORDER BY ob.Name) AS Row INTO tmpFieldNames FROM sysobjects AS ob INNER JOIN dbo.syscolumns AS col ON ob.ID=col.ID WHERE ob.xType = ''U'' AND ob.Name LIKE ''TESTDTA''' EXEC (@SQLAddToTable)--build counter for loopDECLARE @Counter INTSET @Counter = 1--initialize variableDECLARE @SQLSEL VARCHAR(1000)SET @SQLSEL = 'SELECT '--run through your temp table. with each iteration add the selected fieldname to the SELECT statementBEGIN WHILE @Counter <= (SELECT COUNT(*) FROM tmpFieldNames ) -- runs once for each row in the table BEGIN SELECT @SQLSEL = @SQLSEL + FieldName + ', ' FROM tmpFieldNames WHERE Row = @Counter SET @Counter = @Counter +1 ENDEND--finish off the SELECT statement (The NULL is simply to account for the additional comma at the end of the fieldnamesSET @SQLSEL = @SQLSEL + 'NULL FROM TESTDTA'EXEC(@SQLSEL)--housekeepingDROP TABLE tmpFieldNamesI'm actually pretty chuffed with this piece of code. Any of you who can reach, please give me a pat on the back.---------------------------------------------------------------------------------http://www.mannyroadend.co.uk A Bury FC supporters website and forum |
 |
|
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2011-09-29 : 11:39:28
|
| Of course, having done all that I have just realised that you could simply use SELECT * FROM TESTDTA, but what's the fun in that eh?---------------------------------------------------------------------------------http://www.mannyroadend.co.uk A Bury FC supporters website and forum |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2011-09-29 : 12:43:14
|
I expect nothing less from a Bury fan.Now an Arsenal fan would have started with SELECT * http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2011-09-29 : 13:08:11
|
| >>i have a table that contains only one row but many columnsI can't think of a good reason for this design. Why did you model it like this?Be One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-29 : 13:55:48
|
if you want to get column value you need to use dynamic sql using sp_executesqlsomething like:declare @sql nvarchar(4000),@params nvarchar(2000),@ret varchar(1000)set @sql=N'select @Ttype = '+ @st + ' from testdta.f590036n where DSRJOB = 1'set @params=N'@Ttype varchar(1000) OUT'EXEC sp_executesql @sql,@params,@Ttype =@ret OUT... ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|