Hi I’m working on a strange project that has led me to some strange places and here I am again. Essentially there is a search of oh about 100 some odd controls (criteria which can each have nested multiple or’s) (Multi Select ListBoxes) … I don’t like using Dynamic SQL but due to the nature of the app I found it necessary I wrote a rather short C# back end (this is in ASP) that checks which controls are selected, takes the name value for the control (which is the column name) and builds a dynamic where clause … Example …
SELECT TOP 50000 [lname] as 'last',[fname] as 'first',[phone],[wphone],[fax],[cellphone],[email],[state],[zip],[state_w],[zip_w],[employer],CONVERT(char, [datebirth], 101) FROM [RT2DBSQL_Review].[dbo].[Respondent] WHERE (education = '5' ) and (race = 'H' ) and (hours = '1' ) and (',' + CC5 like '%,2,%' or ',' + CC5 like '%,26,%' or ',' + CC5 like '%,12,%') and (',' + CC6 like '%,9,%' or ',' + CC6 like '%,23,%') and (',' + CC4 like '%,Nintendo DS,%') and (gender = 'M') AND Pro_resp is null
This is checked and binds a grid to show the matching person(s).
The next step is. Say there are 1000 viable applicants choose x number of random applicants and insert dynamic values from respondent table (see below), static jobnum, static quota, all while feeding in the crazy ass where clause. I have all the individual pieces of this but am getting jammed up on getting it to work together.
Something Like:
CREATE PROCEDURE TEMPTEMP
@jobnum varchar(100),
@quota varchar(100),
@X int,
@Dynamic varchar(max)
AS
INSERT INTO [RT2DBSQL_Review].[dbo].[tbl_job_respondents]
([job_resp_job_number]
,[job_resp_respondent]
,[job_resp_name]
,[job_resp_phone]
,[job_resp_wphone]
,[job_resp_wphone_ext]
,[job_resp_cellphone]
,[job_resp_call_status]
,[job_resp_scheduled]
,[job_resp_recruited_by]
,[job_resp_part_status]
,[job_resp_coop_amt]
,[job_resp_amt_paid]
,[job_resp_status]
,[job_resp_part_check_no]
,[job_resp_session]
,[job_resp_date_pulled]
,[job_resp_date_recruited]
,[job_resp_quota]
,[job_resp_source]
,[job_resp_confirmed]
,[job_resp_referred_by]
,[query_print_field]
,[job_resp_exported]
,[job_resp_notes]
,[job_resp_childname])
VALUES
(@jobnum
,(Select TOP (@X) RecordId
,lname + ', ' + fname
,phone
,wphone
,wphone_ext
,cellPhone
,'0' --static
,0 --static
,'' --static
,'' --static
,0 --static
,0 --static
,'0' --static
,null--static
,null--static
,GETDATE()
,GETDATE()
,@quota
,'Q' --static
,null--static
,null--static
,0 --static
,0 --static
,null
,null/*static*/FROM Respondent
WHERE @Dynamic
ORDER BY NEWID()))
GO
The alternative is to load everything on the select into an array list whatever ?? Any Ideas I’m having a brain fart with this.
Thanks in advance for the help
First of all I am going to strongly recommend that you view SQL as any other code and add consistent formatting to it.
That said, the following trick should pick 1000 random records out of your 50,000 first records that you already had.
Based on that creating your insert should not be too bad. (It will be a mess, but that’s life sometimes. More importantly it is a repetitive mess, and repetitive means that you can template it.)