I want to select Column from Table based on Its Order like
create Table Products
(
ProductId Int,
ProductName varchar(50)
)
lets Say I don't Know the name of the second column.
How I can get it like :
Select Col1,Col2 From Product
For SQL Server:
You can't do this in the SELECT
clause. You can't select based on the order number of the column. You have to list the columns' names you need to select explicitly, otherwise, use SELECT *
to list all. Me be if you are using a data reader object or any other ado.net methods to get the data from database you can do something like this, but this will be based on the column names list listed in your SQL statement.
However, you can do something like this dynamically, by reading columns' metadata ordinal_position
from information_schema.columns
as explained in the following answer:
But, you can do this in the ORDER BY
clause. You can ORDER BY
column number:
SELECT *
FROM TableName
ORDER BY 2; -- for col2
But this is not recommended to use in ORDER BY
or in the SELECT
(if any). Furthermore, columns order is not significant in the relational model.
Update: If you want to select at least 3 columns from any table parameter passed to your stored procedure. Try this as follows:
Your stored procedure supposed to receive a parameter @tableNameParam
. The folowing code should return the first three columns from the @tablenameParam
passed to the stored procedure:
DECLARE @col1 AS VARCHAR(100);
DECLARE @col2 AS VARCHAR(100);
DECLARE @col3 AS VARCHAR(100);
DECLARE @tableNameParam AS VARCHAR(50) = 'Tablename';
DECLARE @sql AS VARCHAR(MAX) ;
SELECT @col1 = column_name FROM information_schema.columns
WHERE table_name = @tableNameParam
AND ordinal_position = 1;
SELECT @col2 = column_name FROM information_schema.columns
WHERE table_name = @tableNameParam;
AND ordinal_position = 2;
SELECT @col3 = column_name FROM information_schema.columns
WHERE table_name = @tableNameParam;
AND ordinal_position = 3;
SET @sql = 'SELECT ' + col1 + ',' + col2 ' + 'col3 ' + FROM ' + @tablename;
你总是可以做
select * from Product
I'd like to share the following code as a solution to CRUD processing on Ordinal Position within a table. I had this problem today and it took me quite a long time to research and find a working solution. Many of the posted answers indicated that it was not possible to interact with the tables columns on an Ordinal bases but as indicated in the post above using the information_schema table will allow using the column position.
My situation was interacting with a table populated through the use of a pivot view so the columns are always changing based on the data, which is fine in a view result but when the dataset is stored into a table the columns are dynamic. The column names are a Year-Month combination such as 201801, 201802 with an Item Number as a primary key. This pivot table is to indicate manufacturing quantities by Year-Month on a rolling 12 month period so each month the column names with change/shift which changes their ordinal position when the table is rebuilt each month.
The Pivot view is used to build the Staging table, The Staging table is used to build the Target table so the ordinal position of the staging and target tables are lined up with the same ordinal position.
Declare @colname Varchar(55) -- Column Name
Declare @ordpos INT -- Ordinal Position
Declare @Item Varchar(99) -- PK
Declare @i INT -- Counter
Declare @cnt INT -- Count
Declare @ids table(idx int identity(1,1), Item Varchar(25))
-- Item List
Insert INTO @ids Select Item From DBName.Schema.TableName
select @i = min(idx) - 1, @cnt = max(idx) from @ids
-- Row Loop
While @i < @cnt
Begin
Select @i = @i + 1
Set @ordpos=3
Set @Item = (select Item from @ids where idx = @i)
-- Column Loop
While @ordpos < 27
Begin
Select @colname =column_name From INFORMATION_SCHEMA.Columns Where table_name='TargetTable' and ordinal_position=@ordpos
Exec ('Update TargetTable set ['+@colname+']= (Select ['+@colname+'] From StagingTable Where Item='''+@Item+''') where Item='''+@Item+'''')
Set @ordpos=@ordpos + 1
End -- End Column Loop
End -- End Row Loop
The code here will loop through the Item matrix by rows and by columns and uses Dynamic SQL to build the action, in this case the action is an update but it could just as easily be a select. Each column is processed through the While Loop and then loops through the next row. This allows updates to a specific cell in the matrix by (Item X YearMonth) without actually knowing what the column name at a given position.
The one concern is that depending on the size of the data in this matrix it can be SLOW. I just wanted to show this as a way to use unknown column names in an ordinal position.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.