set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[JobSearch]
(
@startRowIndex int,
@maximumRows int,
@firstString varchar(50)='',
@secondString varchar(50)='',
@thirdString varchar(50)='',
@CountryRequest int = 0
)
AS
DECLARE @TempItems TABLE
(
ID int IDENTITY,
jobs_id uniqueidentifier,
jobs_title varchar(255),
jobs_refid bigint,
jobs_date smalldatetime,
jobs_preview varchar(500),
companys_name varchar(200),
companys_id bigint,
citys_text varchar(50),
countrys_short varchar(50),
jobs_citys_id int,
jobs_countrys_id int,
citys_id numeric(18, 0),
countrys_id numeric(18, 0),
jobs_description text,
jobs_keywords text
)
DECLARE @SearchTerms nvarchar(500);
IF NOT @firstString = ''
BEGIN
SET @SearchTerms = '(' + @firstString + ')'
END
IF NOT @secondString = '' AND NOT @SearchTerms = ''
BEGIN
SET @SearchTerms = @SearchTerms + ' OR (' + @secondString + ')'
END
IF NOT @thirdString = '' AND NOT @SearchTerms = ''
BEGIN
SET @SearchTerms = @SearchTerms + ' OR (' + @thirdString + ')'
END
IF NOT @SearchTerms = ''
BEGIN
SET @SearchTerms = ' AND (CONTAINS (job.jobs_title,' + @SearchTerms + ') OR CONTAINS (job.jobs_description,' + @SearchTerms + ') OR CONTAINS (job.jobs_keywords,' + @SearchTerms + '))'
END
DECLARE @CountryString varchar(20);
IF NOT @CountryRequest = 0
BEGIN
SET @CountryString = ' AND jobs_citys_id = ' + @CountryRequest
END
DECLARE @maxRow int
SET @maxRow = (@startRowIndex + @maximumRows) - 1
SET ROWCOUNT @maxRow
INSERT INTO @TempItems (jobs_id,jobs_title,jobs_refid,jobs_date,jobs_preview,companys_name,companys_id,citys_text,countrys_short,jobs_citys_id,jobs_countrys_id,citys_id,countrys_id,jobs_description,jobs_keywords)
SELECT jobs_id,jobs_title,jobs_refid,jobs_date,jobs_preview,companys_name,companys_id,citys_text,countrys_short,jobs_citys_id,jobs_countrys_id,citys_id,countrys_id,jobs_description,jobs_keywords
FROM jobs
LEFT JOIN companys ON companys_id = jobs_companys_id
LEFT JOIN citys ON citys_id = jobs_citys_id
LEFT JOIN countrys ON countrys_id = jobs_countrys_id
ORDER BY jobs_date DESC
SET ROWCOUNT @maximumRows
EXEC('SELECT job.jobs_id,job.jobs_title,job.jobs_refid,job.jobs_date,job.jobs_preview,company.companys_name,company.companys_id,city.citys_text,country.countrys_short
FROM @TempItems tmp
LEFT JOIN jobs job ON job.jobs_id = tmp.jobs_id
LEFT JOIN companys company ON company.companys_id = tmp.companys_id
LEFT JOIN citys city ON city.citys_id = tmp.citys_id
LEFT JOIN countrys country ON country.countrys_id = tmp.countrys_id
WHERE ID >= @startRowIndex'+ @SearchTerms + @CountryString)
SET ROWCOUNT 0
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
by: angelIIIPosted on 2009-05-04 at 11:37:12ID: 24297840
the problem is that the temp table is not known inside the dynamic sql (different scope).
so, unless you find a method to get rid of the dynamic sql, you have to put all that part into the dynamic sql:
Select allOpen in new window