Order By
-
I Have two Tables like Code and Details... In Code(SrNo,Name) Table Records Like A,B,C etc In Details(SrNo,CodeName)Table looks like SrNo A B C 1 0 1 0 2 0 0 0 3 1 0 0 4 0 0 0 5 0 0 1 6 1 1 1 7 0 0 0 If I add another Name in Code Table Like 'D'. Select query for Details table also contain column 'D'. So Select query like this DECLARE @Code NVARCHAR(MAX) SET @Code = (SELECT ('CONVERT(VARCHAR,'+Name+ ')'''+Name+''',') FROM Code ORDER BY SrNo FOR XML PATH('')) SET @Code = SUBSTRING(@Code,0,LEN(@Code)) DECLARE @SQL VARCHAR(MAX) SET @SQL = 'SELECT '+@Code+' FROM Details' EXEC (@SQL) Output:I need to check any '1' in columns and srno in order by without mentioning column names. It has to be like below. How can I do in SqlServer2005 SrNo A B C 6 1 1 1 1 0 1 0 3 1 0 0 5 0 0 1 2 0 0 0 4 0 0 0 7 0 0 0
-
I Have two Tables like Code and Details... In Code(SrNo,Name) Table Records Like A,B,C etc In Details(SrNo,CodeName)Table looks like SrNo A B C 1 0 1 0 2 0 0 0 3 1 0 0 4 0 0 0 5 0 0 1 6 1 1 1 7 0 0 0 If I add another Name in Code Table Like 'D'. Select query for Details table also contain column 'D'. So Select query like this DECLARE @Code NVARCHAR(MAX) SET @Code = (SELECT ('CONVERT(VARCHAR,'+Name+ ')'''+Name+''',') FROM Code ORDER BY SrNo FOR XML PATH('')) SET @Code = SUBSTRING(@Code,0,LEN(@Code)) DECLARE @SQL VARCHAR(MAX) SET @SQL = 'SELECT '+@Code+' FROM Details' EXEC (@SQL) Output:I need to check any '1' in columns and srno in order by without mentioning column names. It has to be like below. How can I do in SqlServer2005 SrNo A B C 6 1 1 1 1 0 1 0 3 1 0 0 5 0 0 1 2 0 0 0 4 0 0 0 7 0 0 0
You can refer to columns by ordinal as
select foo from bar order by 1, 2
."If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me
-
You can refer to columns by ordinal as
select foo from bar order by 1, 2
."If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me
you mean to say by Srno. If so its not my requirement.
-
you mean to say by Srno. If so its not my requirement.
So elucidate as it did appear that's what you asked for. In any case you can use
select foo from bar order by 2 or 3 or 1000
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me
modified on Monday, August 2, 2010 6:26 AM