 |
| SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 18th, 2010, 05:17 PM
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
T-SQL Array Search
Hello All,
I am hoping one of you guys or gals can help me out here cos I am kinda outta my depth. I took over a web project and I have a search engine to search for products, the udser can also select a category to search in. The user could possible type "Duty Call" and they are in fact searching for "Call of Duty".. I need the T-SQL script to look at each individual word in the string. My current script only finds "Call Of Duty". I understand that T-SQL cant do arrays, but there are ways of doing it. Heres my current code that only finds "Call of Duty" if you type it identically.
Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [sbuser].[tf_ProductSearch] (
@PG int,
@ROWCT numeric(18,2),
@COLCT numeric(18,2),
@StoreItemCatID bigint,
@SearchStr varchar(100) = null)
RETURNS @OUT TABLE (
StoreItemID bigint,
ProductName varchar(50),
ProductDesc varchar(1000),
ItemPrice float,
Active bit,
CreateDate datetime,
ShipWeight float,
TaxExempt bit,
ShipCost float,
Keywords varchar(1000),
PG int,
MAXPG INT,
TOTALRECS INT,
CoverImg varchar(50),
StoreItemCatID bigint )
AS
BEGIN
DECLARE @START numeric(18,2)
DECLARE @END numeric(18,2)
DECLARE @SIZE numeric(18,2)
DECLARE @MAXPG numeric(18,2)
DECLARE @TOTALRECS numeric(18,2)
DECLARE @TOTALRECS_INT int
DECLARE @MAXPG_INT int
DECLARE @TOTALRECS_REMAINDER numeric(18,2)
SET @SIZE = @ROWCT * @COLCT
SET @Start = (((@PG - 1) * @Size) + 1)
SET @END = (@START + @SIZE - 1)
DECLARE @TMP1 TABLE (
TmpID bigint identity(1,1) primary key,
StoreItemID bigint,
ProductName varchar(50),
ProductDesc varchar(1000),
ItemPrice float,
Active bit,
CreateDate datetime,
ShipWeight float,
TaxExempt bit,
ShipCost float,
Keywords varchar(1000),
CoverImg varchar(50),
StoreItemCatID bigint )
IF @StoreItemCatID IS NULL
BEGIN
INSERT INTO @TMP1
SELECT
a.StoreItemID,
a.ProductName,
a.ProductDesc,
a.ItemPrice,
a.Active,
a.CreateDate,
a.ShipWeight,
a.TaxExempt,
a.ShipCost,
a.Keywords,
sbuser.sf_StoreItemCoverImg(a.StoreItemID) AS CoverImg,
a.StoreItemCatID
FROM StoreItem a
WHERE
a.ProductName LIKE '%' + @SearchStr + '%'
AND a.Deleted = 0
AND a.Active = 1
ORDER BY a.ProductName
END
ELSE
BEGIN
INSERT INTO @TMP1
SELECT
a.StoreItemID,
a.ProductName,
a.ProductDesc,
a.ItemPrice,
a.Active,
a.CreateDate,
a.ShipWeight,
a.TaxExempt,
a.ShipCost,
a.Keywords,
sbuser.sf_StoreItemCoverImg(a.StoreItemID) AS CoverImg,
a.StoreItemCatID
FROM StoreItem a
WHERE
a.ProductName LIKE '%' + @SearchStr + '%'
AND a.StoreItemCatID = @StoreItemCatID
AND a.Deleted = 0
AND a.Active = 1
OR a.StoreItemCatID IN (SELECT StoreItemCatID FROM StoreItemCat WHERE ParentID = @StoreItemCatID)
AND a.Deleted = 0
AND a.Active = 1
ORDER BY a.ProductName
END
SELECT @TOTALRECS = MAX(TMPID) FROM @TMP1
SELECT @MAXPG = @TOTALRECS / @SIZE
SET @TOTALRECS_REMAINDER = @TOTALRECS % @SIZE
SET @MAXPG_INT = CAST(@MAXPG AS INT)
SET @TOTALRECS_INT = CAST(@TOTALRECS AS INT)
IF @TOTALRECS_REMAINDER > 0
BEGIN
SET @MAXPG_INT = @MAXPG_INT + 1
END
INSERT INTO @OUT
SELECT
StoreItemID,
ProductName,
ProductDesc,
ItemPrice,
Active,
CreateDate,
ShipWeight,
TaxExempt,
ShipCost,
Keywords,
@PG,
@MAXPG_INT,
@TOTALRECS_INT,
CoverImg,
StoreItemCatID
FROM @TMP1
WHERE (TmpID >= @Start) AND (TmpID <= @END)
RETURN
END
The @SearchStr is the obvious search string. If anyone can modify this code to search each word in the string so it will find "Call of Duty" if I type "Duty Call" then I will be forever in your debt.
I am using SQL Server 2005
Kind Regards,
Paul
|
|
 |