问题描述
|
嗨,我正在做一个奇怪的项目,这个项目使我到达了一些奇怪的地方,而我又来了。本质上来说,大约有100个奇数控件(每个条件可以嵌套多个or \'s的条件)(Multi Select ListBoxes)的搜索。。。我不喜欢使用Dynamic sql,但是由于应用程序的性质我发现有必要编写一个简短的C#后端(在ASP中),该后端检查选择了哪些控件,获取控件的名称值(即列名称),并构建一个动态where子句... 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 \',26,12,%\') and (\',\' + CC6 like \'%,9,23,\' + CC4 like \'%,Nintendo DS,%\') and (gender = \'M\') AND Pro_resp is null
选中此选项并绑定网格以显示匹配的人。
下一步是。假设有1000个可行的申请人选择x个随机申请人,然后从受访者表(请参见下文),静态jobnum,静态配额中插入动态值,同时填写“疯狂的驴子” where子句。我拥有所有这些单独的部分,但是却陷入了使其无法协同工作的困境。
就像是:
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,null--static,GETDATE(),@quota,\'Q\' --static,null,null/*static*/FROM Respondent
WHERE @Dynamic
ORDER BY NEWID()))
GO
另一种方法是将选择中的所有内容加载到数组列表中,无论任何想法,我对此都很放屁。
先谢谢您的帮助
解决方法
首先,我强烈建议您将SQL视为其他任何代码,并向其添加一致的格式。
就是说,以下技巧应从您已有的50,000条第一条记录中随机抽取1000条记录。
SELECT TOP(1000) *
FROM (
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),RAND() as priority
FROM [RT2DBSQL_Review].[dbo].[Respondent]
WHERE (education = \'5\' )
AND (race = \'H\' )
AND (hours = \'1\' )
AND (\',\' + CC5 like \'%,2,%\' or \',26,12,%\')
AND (\',\' + CC6 like \'%,9,23,\' + CC4 like \'%,Nintendo DS,%\') and (gender = \'M\')
AND Pro_resp is null
) full_sample
ORDER BY priority
基于此,创建插入内容应该不会太差。 (这将是一团糟,但这有时是一生。更重要的是,它是重复的一团糟,重复意味着您可以对其进行模板化。)