|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
The following piece of code allows me to insert multiple rows in the database.
But the problem is varaiable @Protocols has values 27,1,3 but it inserts in the database as 1,3,27(in different rows). Can anybody provide a solution as to how to save the values as 27,1,3(in different rows) into the database. BEGIN SET @SQLString = 'INSERT INTO ERP_TASKS_LOG( ' + '[NRC_ID], ' + '[ProjectNum], ' + '[ERP_ID], ' + '[ProtocolNum], '+ '[TaskDtTime]) ' + 'SELECT ' + CONVERT(NVARCHAR(100), @NRC_ID, 126) + ', ' + CAST(@ProjectNum AS NVARCHAR(100)) + ', ' + '''' + @ERP_ID + ''', ' + '[ProtocolNum], getDate() ' + 'FROM lu_PROTOCOLS ' + 'WHERE [ProtocolNum] IN (' + @Protocols + ')' -- Execute it --PRINT @SQLString EXECUTE sp_executesql @SQLString END Thanks for your help in advance |
|
#2
|
||||
|
||||
|
It's a problem since 27,1,3 is neither ascending nor descending so you cannot use ORDER BY to solve the problem. If you must return the records out of order, one solution is to add a column to the table lu_PROTOCOLS called SortOrder and for your records where ProtocolNum is (27,1,3) put the values (1,2,3) then add SortOrder to your SELECT statement in the procedure and then ORDER BY SortOrder.
IF for some reason you cannot add a field to this table, create a paramaters table in your dastabse to store the two fields: ProtocolNum with values (27,1,3) and SortOrder with values (1,2,3) and JOIN to the new table on ProtocolNum. If you have tons more PrococolNum values than just the three you mentioned and maintaining this sort order field is to big of a job you must evaluate the data in the lu_Protocols table for what data it is that should determine the insert order in ERP_TASK_LOG and leverage the field(s) that already exist to help you get your data inserted in a proper order in the ERP_TASK_LOG (although I don't know why you can't just order the result talbe however you want it!) If you know for a fact you will only ever have those three values, then a less elegant solution is to split your SQL into three seperate INSERT statements, once for 27, then for 1 and finally for 3 but then you loose the dynamic SQL in your WHERE IN (@protocols). You should be able to leverage or derrive a solution from one of the three ideas. Good luck! |
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > Order Listbox items while save |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|