|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
order by with insert into?
I'm selecting records from a table and inserting them into another existing table
Use DSRBQ000 INSERT INTO dbo.db_table_information Select Table_Name, Column_Name + ' ' + Upper(data_type) + CASE WHEN data_type IN('binary','char','nchar','nvarchar','varbinary','varchar') THEN '(' + Cast(character_maximum_length AS varchar(10))+')' WHEN data_type IN('decimal','numeric') THEN '(' + Cast(numeric_precision as varchar(3)) + ',' + Cast(numeric_scale as varchar(3))+ ')' Else '' End + CASE WHEN columnproperty(object_id(table_name),column_name,'IsIdentity')= 1 THEN ' IDENTITY' + '(' + Cast(ident_seed(table_name) AS varchar(10)) + ',' + Cast(ident_incr(table_name) AS varchar(10)) + ')' Else '' End + CASE WHEN is_nullable = 'YES' THEN ' NULL' ELSE '' END 'Column_Definition', ordinal_position from information_schema.columns where table_name IN(select distinct table_name from information_schema.tables where table_type = 'BASE TABLE') and table_name NOT IN('dtproperties','dbo.db_table_information') order by table_name, ordinal_position I'm trying to first order by table_name and then ordinal_position. However, when viewing the table that it data is getting inserted into, I notice that even though it is ordered by table_name, sometimes a row is out of order according to ordinal position. Is there a valid reason for this? Are you not allow to select the order from which a recordset gets inserted into a table? If thats the case, how can I update the db_table_information and save it so it is in table_name, ordinal_position order? |
|
#2
|
|||
|
|||
|
Look at the results of your select query. Are the results in the order you are expecting? When you are retrieving the inserted data are you issuing an order by clause? Does the target table have existing indexes that might effect your display? Does the target table have a clustered index?
|
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > order by with insert into? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|