Question : how do I write Stored Procedure where in SQL query 'FROM' clause is selected from dropdownlist!!!!!

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 very new to writing stored procedure.


Okay now I come to one conclusion that I have to use dynamic SQL statement here and the following code will not work.
something like this

REATE PROCEDURE general_select2 @tblname nvarchar(127),
                                 @key     varchar(10) AS
EXEC('SELECT col1, col2, col3
      FROM ' + @tblname + '
      WHERE keycol = ''' + @key + '''')


but now my problem is how will I write select statement,i mean "select  *  " will not work because different table have different # of columns,

ANY HELP WILL BE APPRICIATED


here is the structure of the 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





THANK YOU!!!!


Code Snippet:
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:
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 I am getting:
 
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".
Open in New Window Select All

Answer : how do I write Stored Procedure where in SQL query 'FROM' clause is selected from dropdownlist!!!!!

OK ...
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:
USE [charlotte_production]
GO
/****** Object:  StoredProcedure [dbo].[sp_AdvanceSearch1]    Script Date: 08/12/2008 09:33:00 ******/
 
ALTER PROCEDURE [dbo].[sp_AdvanceSearch1]
(
      @Text VARCHAR(300),
      @Text1 VARCHAR(300),
      @Text2 VARCHAR(300),
    @tblname VARCHAR(300)
)
 
AS
 
SET NOCOUNT ON
 
 
-- Show the results
--DECLARE @Params VARCHAR(1000)
Declare @SQL varchar(8000)
Declare @Element varchar(200)
--Declare @Params varchar(200)
 
--SET      @Params = QUOTENAME(@Text, '''') + 'AND' + QUOTENAME(@Text1, '''') + 'OR' + QUOTENAME(@Text2, '''')
 
Set @Element = case @tblname
       When 'SystemElement' then ' SESummaryText '
       When 'TextElement' then 'ElementText'
       When 'VideoElement' then 'VideoMimeType'
       When 'MetaTag' then 'MetatagText'
     END -- Ends the CASE statement
 
set @SQl = 'Select s.SystemElementID, TOC.DocumentID, S.SETitleText, '
   + @Element + ' as [SearchElement]'
 
 
SET      @SQL = @SQL + 'FROM  SystemElement Inner Join ' + 
	@tblname + ' t On s.SystemElementID=t.SystemElementID Inner Join 
	TOCMap on s.SystemElementID=TOCMap.SystemElementID Inner Join 
	TableOfContents TOC on TOCMap.TableOfContentsID = TOC.TableOfContentsID
WHERE   FREETEXT(' + @Element + ',''' +  @Text + ''') OR FREETEXT(' + @Element + ',''' +  @Text1 + ''') OR FREETEXT(' + @Element + ',''' +  @Text2 + ''') '
 
 
 --Print (@SQL)
EXEC      (@SQL)
Open in New Window Select All
Random Solutions  
 
programming4us programming4us