Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Using EXISTS with 2 temp tables

Author  Topic 

MaverickUK
Yak Posting Veteran

89 Posts

Posted - 2006-11-02 : 06:08:28
I'm attempting to delete rows from one temp table, where they appear in another temp table. However SQL Server gives me an error message:

Error 137: Must declare the variable '@TT_Matches'

Here is the SQL code
-- Delete duplicate rows
DELETE FROM
@TT_Matches
WHERE
NOT EXISTS
(
SELECT
TT_SR.PageID FROM @TT_SearchResults AS TT_SR WHERE TT_SR.PageID = @TT_Matches.PageID
)


This is running on SQL Server 2000 with the latest service pack.

I've used exists lots of times before, but I don't think I have with two temp tables.

Please can someone help me out with this!
TIA

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-11-02 : 06:10:38
well you're using a table variable which you have to populate in the same context as the delete statement.



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

MaverickUK
Yak Posting Veteran

89 Posts

Posted - 2006-11-02 : 06:14:50
spirit1: Thanks for replying, but I'm confused by your statement?

Are you saying this is possible or not possible?

If it is possible, can you illustrate what you are saying with an example please?
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-11-02 : 06:29:53
If you post whole lenght of code then someone from here can help you....

you can have a read on book online about the scope of the Table variable, it will give you the better idea why are you getting this error.

Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

MaverickUK
Yak Posting Veteran

89 Posts

Posted - 2006-11-02 : 06:36:26
Here is the entire procedure, it's quite a monster...


CREATE PROCEDURE sppb_Search_Public_WithSpider

@SearchKeywords VARCHAR(1000),
@SearchTerm VARCHAR(100),
@PageSize INT,
@CurrentPage INT

/*********************************************************************************
10 Feburary 2005 - Created - Peter Bridger
Do a search of the website, public version

Search Page table and PCL against pages.
Only return pages that are
* Live
* Searchable
* Directly accessible

***********************************************************************************
16 August 2005 - Modified - Peter Bridger
Procedure upgraded to also search
* Information Centre
* Newsroom
* Documents
* Local pages

PLUS paging has been added

DataTypeID >
1: Gandalf page
**** News ****
2: Newsroom
3: Newsroom (Local pages)
**** Local structure system ****
4: District
5: Sector
6: Beat
*** Documents ***
7: Document

***********************************************************************************
15 February 2006 - Modified - Peter Bridger

*** Persons database ***
8: Person

Added the ability to search the persons database. Primarly to make
it easier for the public to find a police officer, their beat officer for example.

***********************************************************************************
8 March 2006 - Modified - Peter Bridger

*** Information Centre ***
9: Info Centre

We've *finally* got the Force-Internet middleware database server in
place and working. So now a syncronised copy of the Information
Centre database is held on HQIDB01, meaning it can now be included
in the website search.

***********************************************************************************
5 September 2006 - Modified - Peter Bridger

Added a very basic logging system, so that we have a record of
what people search for and how many results get returned

***********************************************************************************
10: Search Spider (Page)

Wrote a basic search spider, this is also searched and the results
are combined with the database searches.
*********************************************************************************/

AS

SET NOCOUNT ON

-- Create a less strict version of the search term
DECLARE
@SearchTerm_LessStrict VARCHAR(100)

SELECT @SearchTerm_LessStrict = REPLACE( @SearchTerm, ' AND ', ' OR ' )

-- Create table to hold results
DECLARE
@TT_SearchResults TABLE
(
SearchID INT IDENTITY(1,1),
Rank INT,
PageFileName VARCHAR(100),
DataID INT,
DataTypeID TINYINT,
PageTitle VARCHAR(150),
Static_Lineage VARCHAR(1000),
DescriptionPublic VARCHAR(1000),
Friendly_Lineage VARCHAR(1000)
)

-- Search Page table, against Title, PublicDescription and Keywords
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic
)
SELECT
dbo.GandalfRank( CT.Rank, P.PageID, S.IndexPageID ),
P.PageFileName,
P.PageID,
1, -- Gandalf page
P.Title,
S.Static_Lineage,
DescriptionPublic
from
CONTAINSTABLE( Pages, *, @SearchTerm ) AS CT
JOIN Pages AS P ON
P.PageID = CT.[KEY]
INNER JOIN
Sections AS S ON
S.SectionID = P.SectionID
WHERE
P.IsSearchable = 1 AND
P.IsLive = 1 AND
P.IsDirectlyAccessible = 1 AND
dbo.IsPageLineageLive( P.SectionID ) = 1 -- Check lineage of page is all live
ORDER BY Rank DESC

-- Search against PCL
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic
)
SELECT
CT.Rank,
P.PageFileName,
P.PageID,
1, -- Gandalf page
P.Title,
S.Static_Lineage,
P.DescriptionPublic
FROM
CONTAINSTABLE( Lookup_Categories, *, @SearchTerm ) AS CT
JOIN Lookup_Categories AS LC ON
LC.CategoryID = CT.[KEY]
INNER JOIN
PageCategorys AS PC ON
PC.CategoryID = LC.CategoryID
INNER JOIN
Pages AS P ON
P.PageID = PC.PageID
INNER JOIN
Sections AS S ON
S.SectionID = P.SectionID
LEFT JOIN -- Where PageID doesn't already exist in table
@TT_SearchResults AS TT_SR ON
TT_SR.DataID = P.PageID AND
TT_SR.DataTypeID = 1
WHERE
P.IsSearchable = 1 AND
P.IsLive = 1 AND
P.IsDirectlyAccessible = 1 AND
dbo.IsPageLineageLive( P.SectionID ) = 1 AND -- Check lineage of page is all live
TT_SR.DataID IS NULL

-- Add friendly lineage data
UPDATE
@TT_SearchResults
SET
Friendly_Lineage = dbo.GetFriendlyLineage( Static_Lineage )


-- Search newsroom - News stories
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,

DescriptionPublic,
Friendly_Lineage
)
SELECT
CT.Rank,
' ',
NS.NewsStoryID,
dbo.NewsType( NS.IsLocalNews ),
NS.Title + ' (' + NS.Location + ')',
' ',
CravenSystemV2.dbo.ReturnFirstParagraph( NS.StoryText ),
' '
from
CONTAINSTABLE( CravenSystemV2.dbo.NewsStories, *, @SearchTerm ) AS CT
JOIN CravenSystemV2.dbo.NewsStories AS NS ON
NS.NewsStoryID = CT.[KEY]
WHERE
NS.IsLive = 1 AND
NS.IsDeleted = 0
ORDER BY Rank DESC

-- Search newsroom - News story updates
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic,
Friendly_Lineage
)
SELECT
CT.Rank,
' ',
NS.NewsStoryID,
dbo.NewsType( NS.IsLocalNews ),
NS.Title + ' (' + NS.Location + ')',
' ',
CravenSystemV2.dbo.ReturnFirstParagraph( NS.StoryText ),
' '
FROM
CONTAINSTABLE( CravenSystemV2.dbo.NewsStoriesUpdates, *, @SearchTerm ) AS CT
JOIN CravenSystemV2.dbo.NewsStoriesUpdates AS NSU ON
NSU.NewsStoryUpdateID = CT.[KEY]
JOIN CravenSystemV2.dbo.NewsStories AS NS ON
NS.NewsStoryID = NSU.NewsStoryID
LEFT JOIN -- Where DataID doesn't already exist in table
@TT_SearchResults AS TT_SR ON
TT_SR.DataID = NS.NewsStoryID AND
( TT_SR.DataTypeID = 2 OR TT_SR.DataTypeID = 3 )
WHERE
NS.IsLive = 1 AND
NS.IsDeleted = 0 AND
NSU.IsLive = 1 AND
NSU.IsDeleted = 0 AND
TT_SR.DataID IS NULL -- Story doesn't already exist
ORDER BY CT.Rank DESC

-- Search Local Structure System - Districts
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic,
Friendly_Lineage
)
SELECT
CT.Rank + 20,
' ',
D.DistrictID,
4, -- LSS, district
D.DistrictName,
' ',
DS.DescriptionShort,
' '
FROM
CONTAINSTABLE( LocalStructureSystem.dbo.Districts, *, @SearchTerm ) AS CT
JOIN LocalStructureSystem.dbo.Districts AS D ON
D.DistrictID = CT.[KEY]
INNER JOIN
LocalPagesV3.dbo.DistrictsSettings AS DS ON
DS.DistrictID = D.DistrictID
ORDER BY CT.Rank DESC

-- Search Local Structure System - Sectors
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic,
Friendly_Lineage
)
SELECT
CT.Rank + 20,
' ',
S.SectorID,
5, -- LSS, sector
S.SectorName,
' ',
SS.DescriptionShort,
' '
FROM
CONTAINSTABLE( LocalStructureSystem.dbo.Sectors, *, @SearchTerm ) AS CT
JOIN LocalStructureSystem.dbo.Sectors AS S ON
S.SectorID = CT.[KEY]
INNER JOIN
LocalPagesV3.dbo.SectorsSettings AS SS ON
SS.SectorID = S.SectorID
ORDER BY CT.Rank DESC

-- Search Local Structure System - Beats
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic,
Friendly_Lineage
)
SELECT
CT.Rank + 20,
' ',
B.BeatID,
6, -- LSS, beat
B.BeatName,
' ',
BS.DescriptionShort,
' '
FROM
CONTAINSTABLE( LocalStructureSystem.dbo.Beats, *, @SearchTerm ) AS CT
JOIN LocalStructureSystem.dbo.Beats AS B ON
B.BeatID = CT.[KEY]
INNER JOIN
LocalPagesV3.dbo.BeatsSettings AS BS ON
BS.BeatID = B.BeatID
ORDER BY CT.Rank DESC

-- Search Documents
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic,
Friendly_Lineage
)
SELECT
CT.Rank,
SS.SectionID,
D.DocumentID,
7, -- Document
D.Title,
' ',
D.Summary,
' '
FROM
CONTAINSTABLE( DocumentsSystemV2.dbo.Documents, *, @SearchTerm ) AS CT
JOIN DocumentsSystemV2.dbo.Documents AS D ON
D.DocumentID = CT.[KEY]
INNER JOIN
DocumentsSystemV2.dbo.SubSections AS SS ON
SS.SubSectionID = D.SubSectionID

-- Search Persons database
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic,
Friendly_Lineage
)
SELECT
CT.Rank,
' ',
P.PersonID,
8, -- Person
P.FirstName + ' ' + P.LastName,
' ',
' ',
' '
FROM
CONTAINSTABLE( PersonsDatabase.dbo.Persons, *, @SearchTerm_LessStrict ) AS CT
JOIN PersonsDatabase.dbo.Persons AS P ON
P.PersonID= CT.[KEY]
WHERE
P.IsLive = 1


-- Search Information Centre
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic,
Friendly_Lineage
)
SELECT
CT.Rank,
' ',
I.ItemID,
9, -- Info Centre
I.Title,
' ',
dbo.ReturnFirstParagraph( I.PublicText ),
' '
FROM
CONTAINSTABLE( YodaSystemSynced.dbo.Items, *, @SearchTerm ) AS CT
JOIN YodaSystemSynced.dbo.Items AS I ON
I.SyncItemID = CT.[KEY]
WHERE
I.IsLive = 1 AND
I.IsSyncData = 0

-- Search using SearchSpider (Daedalus)
DECLARE
@TT_Matches TABLE
(
Frequency SMALLINT,
PageID INT
)

-- Search for keywords
INSERT INTO
@TT_Matches
SELECT
SUM( PK.Frequency ) 'TotalMatches',
PK.PageID
FROM
SearchSpider.dbo.Keywords AS K
INNER JOIN
SearchSpider.dbo.PageKeywords AS PK ON
PK.KeywordID = K.KeywordID
INNER JOIN
dbo.Split( @SearchKeywords, ',' ) KW ON
K.Keyword = KW.Data
GROUP BY
PK.PageID


-- Delete duplicate rows
DELETE FROM
@TT_SearchResults
WHERE
EXISTS
( SELECT TT_M.PageID FROM @TT_Matches AS TT_M WHERE TTM.PageID = @TT_SearchResults.PageID )


-- Add results to table
INSERT @TT_SearchResults
(
Rank,
PageFileName,
DataID,
DataTypeID,
PageTitle,
Static_Lineage,
DescriptionPublic,
Friendly_Lineage
)
SELECT
TT_M.Frequency * 10,
P.PageFileName,
' ',
10, -- Search spider
P.Title,
S.Static_Lineage,
P.DescriptionPublic,
dbo.GetFriendlyLineage( Static_Lineage )
FROM
@TT_Matches AS TT_M
INNER JOIN
Pages AS P ON
P.PageID = TT_M.PageID AND
P.IsSearchable = 1 -- Only return searchable pages
INNER JOIN
Sections AS S ON
S.SectionID = P.SectionID

-- Create temp table to order returned results
DECLARE
@TT_RankResults TABLE
(
OrderID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
SearchID INT,
Rank INT
)

INSERT INTO
@TT_RankResults
( SearchID, Rank )
SELECT
SearchID, Rank
FROM
@TT_SearchResults
ORDER BY
Rank DESC

-- Return results, in ranked order
SELECT DISTINCT
TT_RR.OrderID,
TT_SR.Rank,
TT_SR.PageFileName,
TT_SR.DataID,
TT_SR.DataTypeID,
TT_SR.PageTitle,
TT_SR.Static_Lineage,
TT_SR.Friendly_Lineage,
TT_SR.DescriptionPublic
FROM
@TT_SearchResults AS TT_SR
INNER JOIN
@TT_RankResults AS TT_RR ON
TT_RR.SearchID = TT_SR.SearchID
WHERE
TT_RR.OrderID > @PageSize * @CurrentPage AND
TT_RR.OrderID <= @PageSize * (@CurrentPage + 1 )
ORDER BY
TT_RR.OrderID ASC

-- Return total paging size
DECLARE
@PagingSizeTotal INT

SELECT
@PagingSizeTotal = COUNT(*)
FROM
@TT_SearchResults AS TT_SR

-- 5 September 2006 - Modified - Peter Bridger
-- Quick and dirty logging
INSERT INTO
SearchQuickLog
( SearchTerm, Matches )
VALUES
( @SearchTerm, @PagingSizeTotal )

RETURN @PagingSizeTotal

/* EOF */
GO

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-11-02 : 06:40:35
I couldnt find the code, which posted in your first post?

Are you doing that code out side this procedure, then it wont work for you, becuase scope of the table variable is only to this procedure.

you have to declare global temp table for that and then you can use it outside the stored procedure.

Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-02 : 07:05:55
The problem is that you do not use ALIAS.
Try this
DELETE	m
FROM @TT_Matches m
WHERE NOT EXISTS (
SELECT sr.PageID
FROM @TT_SearchResults sr WHERE sr.PageID = m.PageID
)


Peter Larsson
Helsingborg, Sweden

EDIT: I think you may benefit using LEFT JOIN.
Go to Top of Page

MaverickUK
Yak Posting Veteran

89 Posts

Posted - 2006-11-02 : 07:20:54
Peso you are a saviour!

So simple, I should have known.

Thank you and everyone else who spent time looking at this :)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-02 : 07:29:11
DELETE m
FROM @TT_Matches m
LEFT JOIN @TT_SearchResults sr ON sr.PageID = m.PageID
WHERE sr.PageID IS NULL


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

MaverickUK
Yak Posting Veteran

89 Posts

Posted - 2006-11-02 : 08:12:23
I was actually playing about with a join yesterday, but for some reason I did not think of using a IS NULL to exclude rows.

Jumping between C# and SQL is always difficult for me.
Go to Top of Page
   

- Advertisement -