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
 General SQL Server Forums
 New to SQL Server Programming
 Stored procedure,,,please help!!!!!

Author  Topic 

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 10:41:04
Hello

I am trying to do advance search on my database on multiple tables.
The user will enter the search term and select whether they want ALL
words or ANY one words.And""" ALSO USER WILL SELECT FROM DROPDOWNLIST FROM WHICH SPECIFIC TABLE HE WANT TO SEARCH"""".And I dont how to implement dropdownlist in FROM clause in the sql query, and also I think here Union all is not correct as user is searching from specific table.please help me how to write correct stored procedure for this.

I am using SQL server 2005.
AND here is my code..
ALTER PROCEDURE [dbo].[sp_ASearch]
(
@Text VARCHAR(300),
@Text1 VARCHAR(300),
@Text2 VARCHAR(300)
)
AS

SET NOCOUNT OFF

DECLARE @Params VARCHAR(1000)
DECLARE @Drop VARCHAR(100)

SET @Params = QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''')

-- Show the results
SELECT ElementText
FROM @Drop
WHERE FREETEXT(ElementText, @Params)

UNION ALL

SELECT VideoMimeType
FROM @Drop
WHERE FREETEXT(VideoMimeType, @Params)

UNION ALL

SELECT SESummaryText
FROM @Drop
WHERE FREETEXT(SESummaryText, @Params)

UNION ALL

SELECT MetaTagText
FROM @Drop
Where FREETEXT(MetaTagText,@Params)

ERROR
Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 21
Must declare the table variable "@Drop".
Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 27
Must declare the table variable "@Drop".
Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 33
Must declare the table variable "@Drop".
Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 39
Must declare the table variable "@Drop".

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-29 : 10:52:42
quote:
Originally posted by bluestar

Hello

I am trying to do advance search on my database on multiple tables.
The user will enter the search term and select whether they want ALL
words or ANY one words.And""" ALSO USER WILL SELECT FROM DROPDOWNLIST FROM WHICH SPECIFIC TABLE HE WANT TO SEARCH"""".And I dont how to implement dropdownlist in FROM clause in the sql query, and also I think here Union all is not correct as user is searching from specific table.please help me how to write correct stored procedure for this.

I am using SQL server 2005.
AND here is my code..
ALTER PROCEDURE [dbo].[sp_ASearch]
(
@Text VARCHAR(300),
@Text1 VARCHAR(300),
@Text2 VARCHAR(300)
)
AS

SET NOCOUNT OFF

DECLARE @Params VARCHAR(1000)
DECLARE @Drop VARCHAR(100)

SET @Params = QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''')

-- Show the results
SELECT ElementText
FROM @Drop
WHERE FREETEXT(ElementText, @Params)

UNION ALL

SELECT VideoMimeType
FROM @Drop
WHERE FREETEXT(VideoMimeType, @Params)

UNION ALL

SELECT SESummaryText
FROM @Drop
WHERE FREETEXT(SESummaryText, @Params)

UNION ALL

SELECT MetaTagText
FROM @Drop
Where FREETEXT(MetaTagText,@Params)

ERROR
Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 21
Must declare the table variable "@Drop".
Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 27
Must declare the table variable "@Drop".
Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 33
Must declare the table variable "@Drop".
Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 39
Must declare the table variable "@Drop".




You've declared @Drop as a variable and using it like table

you need to create @Drop as a table it seems

DECLARE @Drop table
(
fields...
)
Go to Top of Page

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 11:07:59
thanks for the reply

Ok so you mean something like this...
ALTER PROCEDURE [dbo].[sp_ASearch]
(
@Text VARCHAR(300),
@Text1 VARCHAR(300),
@Text2 VARCHAR(300)
)
AS

SET NOCOUNT OFF

DECLARE @Params VARCHAR(1000)
DECLARE @Drop Table
{
VideoElement,
SystemElement,
TextElement,
MetaTagDictionary
}

SET @Params = QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''')

-- Show the results
SELECT ElementText
FROM @Drop
WHERE FREETEXT(ElementText, @Params)

UNION ALL

SELECT VideoMimeType
FROM @Drop
WHERE FREETEXT(VideoMimeType, @Params)

UNION ALL

SELECT SESummaryText
FROM @Drop
WHERE FREETEXT(SESummaryText, @Params)

UNION ALL

SELECT MetaTagText
FROM @Drop
Where FREETEXT(MetaTagText,@Params)


I am getting error

Msg 102, Level 15, State 1, Procedure sp_ASearch, Line 16
Incorrect syntax near '{'.


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-29 : 11:11:52
quote:
Originally posted by bluestar

thanks for the reply

Ok so you mean something like this...
ALTER PROCEDURE [dbo].[sp_ASearch]
(
@Text VARCHAR(300),
@Text1 VARCHAR(300),
@Text2 VARCHAR(300)
)
AS

SET NOCOUNT OFF

DECLARE @Params VARCHAR(1000)
DECLARE @Drop Table
{
(
VideoElement,
SystemElement,
TextElement,
MetaTagDictionary
)
}

SET @Params = QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''')

-- Show the results
SELECT ElementText
FROM @Drop
WHERE FREETEXT(ElementText, @Params)

UNION ALL

SELECT VideoMimeType
FROM @Drop
WHERE FREETEXT(VideoMimeType, @Params)

UNION ALL

SELECT SESummaryText
FROM @Drop
WHERE FREETEXT(SESummaryText, @Params)

UNION ALL

SELECT MetaTagText
FROM @Drop
Where FREETEXT(MetaTagText,@Params)


I am getting error

Msg 102, Level 15, State 1, Procedure sp_ASearch, Line 16
Incorrect syntax near '{'.





use common braces ()
Go to Top of Page

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 11:16:34
sorry sorry yes it was my mistake......

I corrected it, but then its

Msg 173, Level 15, State 1, Procedure sp_ASearch, Line 18
The definition for column 'VideoElement' must include a data type.

The videoElement,TextElement...are tables,it cannot have datatype,

so what to do now,

Please do reply


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-29 : 11:23:21
quote:
Originally posted by bluestar

sorry sorry yes it was my mistake......

I corrected it, but then its

Msg 173, Level 15, State 1, Procedure sp_ASearch, Line 18
The definition for column 'VideoElement' must include a data type.

The videoElement,TextElement...are tables,it cannot have datatype,

so what to do now,

Please do reply





you cant put tables inside a table table. i thought they are columns. can you tell what you're trying to do here?
Go to Top of Page

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 11:26:50

well I am doing advance search,where there will be textbox in which user will enter text to search and a dropdownlist,which will contain different tables,user will select from dropdownlist that from which particular table they want to search the text. And a button,which after submit will display data in datagrid.

Thank You
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-29 : 11:34:05
quote:
Originally posted by bluestar


well I am doing advance search,where there will be textbox in which user will enter text to search and a dropdownlist,which will contain different tables,user will select from dropdownlist that from which particular table they want to search the text. And a button,which after submit will display data in datagrid.

Thank You


as your table is determined dynamically you need dynamic sql to achieve this. Alternatively you could use if else to write seperate queries to handle each option if number of options is less. I dont personally think this is a good approach. why do you want to give users the facility to decide the table to query dynamically? is it as per the requirement? Some background info might also help.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-07-29 : 11:40:56
The point of your procedure is to search a run time specified table for a run time specified piece of text with some parameters to control search functionality (all / some etc) isn't it?

I think your orriginal idea was to pass the name of the table into your stored proc and then do a select from it?

so you'd pass in say 'employee' to @drop and then do SELECT x FROM @drop? -- unfortunately you can't do this. You need to use dynamic sql instead.

What you'd do is build up a string inside your stored proc and then execute that to generate the results.

Check out http://www.sommarskog.se/dynamic_sql.html -- this will tell you everything you need to know.

-------------
Charlie
Go to Top of Page

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 11:45:00
this search is performed on a document management system, which will have different elements,like text element,binary element,Video element.
and all these are different tables, which are link to one table called System element which will contain ID,Title text, summary and likes.
And in the advance search I am told to give user facility to do specific search,whether they want to search video,text or they want to search from metatagdicionary,or systemelemnt summary.

This what I have to do,please try to explain me with example,about the if else thing you are talking about.
Hope I am being able to explain you properly ,if not please let me know...

Thank You
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-29 : 11:50:15
quote:
Originally posted by bluestar

this search is performed on a document management system, which will have different elements,like text element,binary element,Video element.
and all these are different tables, which are link to one table called System element which will contain ID,Title text, summary and likes.
And in the advance search I am told to give user facility to do specific search,whether they want to search video,text or they want to search from metatagdicionary,or systemelemnt summary.

This what I have to do,please try to explain me with example,about the if else thing you are talking about.
Hope I am being able to explain you properly ,if not please let me know...

Thank You


I think your scenario can be fixed without dynamic sql seeing above explanation. lemme try. before that can i ask you to provide structures of tables involved. some sample data might also help (may be 5 related rows from each)
Go to Top of Page

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 12:10:54
SURE here are the structure of different tables
1.table SystemElement
columns....
SystemElementID int
SEVersion int
SETitleText varchar(500)
SESummaryText varchar(500)

2. table TextElement
columns
SystemElementID int pk,fk
ElementText ntext
ElementURLText varchar(50)

3. table VideoElement
columns
SystemElementID int pk,fk
VideoMimeType varchar(300)
VideoLocationURL varchar(50)

4 table MetatagDictionary
MetatagText varchar(400) pk
Createdby(int)
date


thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-29 : 12:41:50
quote:
Originally posted by bluestar

SURE here are the structure of different tables
1.table SystemElement
columns....
SystemElementID int
SEVersion int
SETitleText varchar(500)
SESummaryText varchar(500)

2. table TextElement
columns
SystemElementID int pk,fk
ElementText ntext
ElementURLText varchar(50)

3. table VideoElement
columns
SystemElementID int pk,fk
VideoMimeType varchar(300)
VideoLocationURL varchar(50)

4 table MetatagDictionary
MetatagText varchar(400) pk
Createdby(int)
date


thanks


how is MetatagDictionary related to SystemElement? or is it independent?also wil be certain that data in ElementText will be less than 4000 chars?
Go to Top of Page

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 12:44:20

I have tried to do this.....
My first question is can we have 3 different select statement together
as I have done below, ?????

2ndly this is giving me error only

""""""when I am executing the stored procedure""""

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'AND'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near ','.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'AND'.

(1 row(s) affected)


ALTER PROCEDURE [dbo].[sp_ASearch]
(
@Text VARCHAR(300),
@Text1 VARCHAR(300),
@Text2 VARCHAR(300),
@tblname VARCHAR(300)
)

AS

SET NOCOUNT ON


-- Show the results
DECLARE @SQL VARCHAR(8000)

SET @Sql = '
SELECT ElementText
FROM ' + @tblname + '
WHERE FREETEXT(ElementText, ' + QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''') + ')



SELECT VideoMimeType
FROM ' + @tblname + '
WHERE FREETEXT(VideoMimeType, , ' + QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''') + ')



SELECT SESummaryText
FROM ' + @tblname + '
WHERE FREETEXT(SESummaryText, ' + QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''') + ')
'

EXEC (@SQL)


will be waiting for reply

Thank You
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-29 : 12:47:01
You should do three separate EXEC(@SQL).

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 12:50:39
well metatagdictionary is independent,

there is a table called MetaTagwhich is connected to table SystemElement,and this MetaTag table is then connected to MetatagDictionary.

These are columns of table MetaTag
MetaTagID pk
SystemElementID fk
MetaTagText fk
MetaTagtypecode

""also wil be certain that data in ElementText will be less than 4000 chars?"""

Yes I think so

Thank You very much for your time....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-29 : 13:14:24
[code]SELECT se.*,t.StrValue,t.LocValue
FROM SystemElement se
LEFT JOIN (
SELECT SystemElementID,VideoMimeType AS StrValue,VideoLocationURL AS LocValue,'Video' AS TypeValue
FROM VideoElement
UNION ALL
SELECT SystemElementID,CAST(ElementText AS nvarchar(400)),
ElementURLText,'Element'
FROM TextElement
UNION ALL
SELECT mt.SystemElementID,mtd.MetaTagText,NULL,'MetaTag'
FROM MetaTag mt
INNER JOIN MetatagDictionary mtd
ON mt.MetaTagText=mtd.MetaTagText
)t
ON t.SystemElementID=se.SystemElementID
WHERE TypeValue=@TypeValue
AND (StrValue=@StrValue OR @StrValue IS NULL)
AND (LocValue=@LocValue OR @LocValue IS NULL)
....[/code]

where type value will be what you pass according as user selects type from dropdown. also include parameters which user uses to pass oher values to be searched for which will compared to corresponding field.
Go to Top of Page

bluestar
Posting Yak Master

133 Posts

Posted - 2008-07-29 : 15:35:59
thank you visakh16,

but sorry,I am not being able to understand your query,
1st of all do we require union all here,because here I am searching from one specific table,

I am feeling that your query is correct because inner join is required,
but if will be great if you can explain me bit why are you doing this
SELECT SystemElementID,VideoMimeType AS StrValue,VideoLocationURL AS LocValue,'Video' AS TypeValue

why SystemElementID,VideoMimeType AS StrValue
and VideoLocationURL AS LocValue,'Video' AS TypeValue
and also what is 'Video' here for.

I am sorry if my question is stupid,but I dont have enough experience in stored procedure.

please do reply

Thank You
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-30 : 00:14:48
quote:
Originally posted by bluestar

thank you visakh16,

but sorry,I am not being able to understand your query,
1st of all do we require union all here,because here I am searching from one specific table,

I am feeling that your query is correct because inner join is required,
but if will be great if you can explain me bit why are you doing this
SELECT SystemElementID,VideoMimeType AS StrValue,VideoLocationURL AS LocValue,'Video' AS TypeValue

why SystemElementID,VideoMimeType AS StrValue
and VideoLocationURL AS LocValue,'Video' AS TypeValue
and also what is 'Video' here for.

I am sorry if my question is stupid,but I dont have enough experience in stored procedure.

please do reply

Thank You


Thats for making your search static from dynamic. why you require dynamic sqql was because you wont be knowing which table to select until users select names from dropdown. what i've done is to take data from all tables and form a derived table out of them. this way we dont have to find out which table to be used while user selecting from dropdown and queries will always be against derived table. The last field ('Video','Element'..) was put to distinguish which tables data it originally was. This will be compared against value selected by user for searching against (i just put element,video... for example. you can use table name itself here instead of what i gave). So if user wants to select TextElement for particular value in a field he would be selecting value Element from first dropdown and enter value in next textbox. we pass 'Element' as value for @TypeValue param and the text value for coresponding field value param. this will then be used in searching as per query below.
Go to Top of Page
   

- Advertisement -