Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 13th, 2004, 09:25 AM
Prati Prati is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 5 Prati User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Order Listbox items while save

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

Reply With Quote
  #2  
Old October 21st, 2004, 01:32 AM
Leslie's Avatar
Leslie Leslie is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Honolulu
Posts: 184 Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level)Leslie User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 20 h 29 m 2 sec
Reputation Power: 9
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!

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Order Listbox items while save


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT