I have a stored procedure which inserts my data into the table after selecting data from…
- User
I have a stored procedure which inserts my data into the table after selecting data from top subqueries. But the order by clause after select doesn't work, when i look at the data in my inserted table. Here is the end part of the code of my stored procedure:- INSERT INTO [curated].[dbo].[M107_Flat_File] (Unique_Key, Barcode, Start_Datetime, Recipe, Batch_ID, Mixing_Time, Cycle_Time, BWB_Time, CT_Sec_Incremental, MixEnergy_Sec_Incremental, MixPower_Sec_Incremental, MixPressure_Sec_Incremental, MixSpeed_Sec_Incremental, MixTemp_Sec_Incremental, Batch_Total_Energy, Alarms)
codeSelectCAST(CASE WHEN Y.Unique_Key_Y IS NULL tHEN mix_term_act_merge.Unique_Key_mix_term_act_merge ELSE Y.Unique_Key_Y end as bigint) as Unique_Key, CAST(X.Barcode as bigint) as Barcode, CAST(X.start_datetime as datetime) as start_datetime , CAST(X.Recipe as varchar(25)) as Recipe, CAST(X.Batch_ID as INT) as Batch_ID , CAST(X.Mixing_Time as INT) as Mixing_Time, CAST(X.Cycle_Time as INT) as Cycle_Time, CAST(X.BWB_Time as INT) as BWB_Time, Cast(y.CT_Sec_Incremental as decimal(10,2)) as CT_Sec_Incremental, CAST(Y.MixEnergy_Sec_Incremental as decimal(10,2)) as MixEnergy_Sec_Incremental, CAST(Y.MixPower_Sec_Incremental as decimal(10,2)) as MixPower_Sec_Incremental, CAST(Y.MixPressure_Sec_Incremental as decimal(10,2)) as MixPressure_Sec_Incremental, CAST(Y.MixSpeed_Sec_Incremental as decimal(10,2)) as MixSpeed_Sec_Incremental, CAST(Y.MixTemp_Sec_Incremental as decimal(10,2)) as MixTemp_Sec_Incremental, CAST(X.Batch_Total_Energy as decimal(10,2)) as Batch_Total_Energy, CAST(X.Alarms as varchar(max)) as Alarms
codefrom x left join y on X.Barcode = Y.Barcode full outer join mix_term_act_merge on Y.Unique_Key_Y = mix_term_act_merge.Unique_Key_mix_term_act_mergeand mix_term_act_merge.MixTime = Y.MixtimeX ORDER BY Y.Unique_Key_Y asc, X.Start_datetime asc
- Assistant
The
ORDER BYclause in yourSELECTstatement only affects the order of the result set returned by the query, not the order in which the data is inserted into the table. If you want the data to be inserted in a specific order, you would need to use anIDENTITYcolumn or some other method to maintain the order.However, SQL Server does not guarantee the order of rows returned by a
SELECTstatement without an clause, and even with an clause, the order is not guaranteed to be maintained when the data is inserted into a table.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.