I need to submit data from a form to an SQL2005 database via a stored procedure. The difficult part is that i also need to get 5 random records from a secondary table and insert these as part of the inserted record in table 1.
My structure is akin to this:
Tbl_Organisations (table to get the 5 random records from)
Key | organisation_name |
Tbl_Campaigns (table to be inserted to)
Key | name | date | organisation_1 | organisation_2 | organisation_3 | etc……..
I need to get 5 unique / random records from ‘tbl_Organisations’, catch them and insert them into a single record along with the data input to the stored procedure. I understand arrays arent an option in SQL2005(?).
So how do i catch these records and then insert them as a single record along with the SP Inputs?
Any help would be greatly appreciated as i am trying my best to get to grips with the complexities of SQL
Thanks.
this should work if you have a sequential numeric key (like an identity) and never delete any rows or rollback any transactions. You don’t post many details so this is just a guess, that you can base your code on. If you delete rows, you can use ROW_NUMBER() to make a similar approach work.
from here, try this:
EDIT based on need to use row_number()
try this code:
here is the output