Solved

Must declare the table variable

Posted on 2009-05-04
11
836 Views
Last Modified: 2012-05-06
I have another question open with suggestions to get variables into the stored procedure I provided, and while waiting I found something called dynamic sql and tried it, but I keep getting the error 'Msg 1087, Level 15, State 2, Line 2
Must declare the table variable "@TempItems"'

Can anyone point out the obvious or am I doing this incorrectly... it lets me create the stored procedure , but I cannot run it.

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

Open in new window

0
Comment
Question by:B_Dorsey
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 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:
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 @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
 
SET ROWCOUNT @maximumRows
 
EXEC('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
)
 
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
 
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

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24297843
THe problem is here....since your table variable is local only to your current scope, you can't use dynamic sql to access it.

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)
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24297846
you can change your table variable to be a global temp table ##tempItems, and it will fix your problem.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24297858
why are you using the table variable at all?  it looks like you're  just returning data, so its really not necessary.
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 24297973
sorry guys Im completly lost...

Are their any pages that can help me... Im just trying to page some results based on some keywords and options... it works fine if I hardcode everything in...(without the exec)
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24298075
please try to use my code as I posted it...

apart from that, reread my explanation:

the problem is that the temp table (variable) is not known inside the dynamic sql (as that has a different scope, so the @table variable declared outside of exec() is not visible inside of exec() ).
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 125 total points
ID: 24298122

As chapmandew suggested, use temp table instead of table variable.
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

 

create TABLE #TempItems 

(

	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

DROP TABLE #TempItems 

 

Open in new window

0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 24298181
@angel... well it made by that error except I got a new one (same kinda thing)

Must declare the scalar variable "@startRowIndex".

@chapmandew & @Sharath_123
How would this scenario run under pressure? Is it good for high traffic?

Thanks Guys
0
 
LVL 40

Expert Comment

by:Sharath
ID: 24298232
As your conditions in the WHERE clause vary with the variables (@SearchTerms , @CountryString ), you need to build to the dynamic sql.

At first shot, i always avoid using dynamic sql until it is necessary. If used, then use temp tables instead of table variables to avoid error like this.

In the attached script of my last post, i haven't updated the table variable to temp table in all places. check that and modify accordingly.
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 24298417
@Sharath_123

I tried the attached code, works fine unless I put a search term in... as soon as I do that I get

Msg 102, Level 15, State 1, Line 7
Incorrect syntax near '('.

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

 

create TABLE #TempItems 

(

	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

DROP TABLE #TempItems

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 24299490

Run this and check the dynamic sql executed for any syntax errors. I am not sure what are you doing in these parameters @startRowIndex ,@SearchTerms and @CountryString.
So better try to print your dynamic sql and debug the error. you can post the dynamic sql here for any assistance.
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

 

create TABLE #TempItems 

(

	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
 

print('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

DROP TABLE #TempItems

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Select * from 6 36
SQL Trigger selecting another database 4 34
How to simplify my SQL statement? 14 50
CROSS APPLY 4 44
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now