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.
| 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 ALLwords 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))ASSET NOCOUNT OFFDECLARE @Params VARCHAR(1000)DECLARE @Drop VARCHAR(100)SET @Params = QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''')-- Show the resultsSELECT ElementText FROM @DropWHERE FREETEXT(ElementText, @Params)UNION ALL SELECT VideoMimeType FROM @Drop WHERE FREETEXT(VideoMimeType, @Params)UNION ALLSELECT SESummaryTextFROM @DropWHERE FREETEXT(SESummaryText, @Params)UNION ALLSELECT MetaTagTextFROM @DropWhere FREETEXT(MetaTagText,@Params)ERRORMsg 1087, Level 15, State 2, Procedure sp_ASearch, Line 21Must declare the table variable "@Drop".Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 27Must declare the table variable "@Drop".Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 33Must declare the table variable "@Drop".Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 39Must 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 ALLwords 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))ASSET NOCOUNT OFFDECLARE @Params VARCHAR(1000)DECLARE @Drop VARCHAR(100)SET @Params = QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''')-- Show the resultsSELECT ElementText FROM @DropWHERE FREETEXT(ElementText, @Params)UNION ALL SELECT VideoMimeType FROM @Drop WHERE FREETEXT(VideoMimeType, @Params)UNION ALLSELECT SESummaryTextFROM @DropWHERE FREETEXT(SESummaryText, @Params)UNION ALLSELECT MetaTagTextFROM @DropWhere FREETEXT(MetaTagText,@Params)ERRORMsg 1087, Level 15, State 2, Procedure sp_ASearch, Line 21Must declare the table variable "@Drop".Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 27Must declare the table variable "@Drop".Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 33Must declare the table variable "@Drop".Msg 1087, Level 15, State 2, Procedure sp_ASearch, Line 39Must declare the table variable "@Drop".
You've declared @Drop as a variable and using it like tableyou need to create @Drop as a table it seemsDECLARE @Drop table(fields...) |
 |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-07-29 : 11:07:59
|
| thanks for the replyOk so you mean something like this...ALTER PROCEDURE [dbo].[sp_ASearch]( @Text VARCHAR(300), @Text1 VARCHAR(300), @Text2 VARCHAR(300))ASSET NOCOUNT OFFDECLARE @Params VARCHAR(1000)DECLARE @Drop Table{ VideoElement, SystemElement, TextElement, MetaTagDictionary}SET @Params = QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''')-- Show the resultsSELECT ElementText FROM @DropWHERE FREETEXT(ElementText, @Params)UNION ALL SELECT VideoMimeType FROM @Drop WHERE FREETEXT(VideoMimeType, @Params)UNION ALLSELECT SESummaryTextFROM @DropWHERE FREETEXT(SESummaryText, @Params)UNION ALLSELECT MetaTagTextFROM @DropWhere FREETEXT(MetaTagText,@Params)I am getting error Msg 102, Level 15, State 1, Procedure sp_ASearch, Line 16Incorrect syntax near '{'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-29 : 11:11:52
|
quote: Originally posted by bluestar thanks for the replyOk so you mean something like this...ALTER PROCEDURE [dbo].[sp_ASearch]( @Text VARCHAR(300), @Text1 VARCHAR(300), @Text2 VARCHAR(300))ASSET NOCOUNT OFFDECLARE @Params VARCHAR(1000)DECLARE @Drop Table{( VideoElement, SystemElement, TextElement, MetaTagDictionary)}SET @Params = QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''')-- Show the resultsSELECT ElementText FROM @DropWHERE FREETEXT(ElementText, @Params)UNION ALL SELECT VideoMimeType FROM @Drop WHERE FREETEXT(VideoMimeType, @Params)UNION ALLSELECT SESummaryTextFROM @DropWHERE FREETEXT(SESummaryText, @Params)UNION ALLSELECT MetaTagTextFROM @DropWhere FREETEXT(MetaTagText,@Params)I am getting error Msg 102, Level 15, State 1, Procedure sp_ASearch, Line 16Incorrect syntax near '{'.
use common braces () |
 |
|
|
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 18The 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 |
 |
|
|
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 18The 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? |
 |
|
|
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 |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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 |
 |
|
|
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) |
 |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-07-29 : 12:10:54
|
| SURE here are the structure of different tables1.table SystemElementcolumns....SystemElementID intSEVersion intSETitleText varchar(500)SESummaryText varchar(500)2. table TextElementcolumnsSystemElementID int pk,fkElementText ntextElementURLText varchar(50)3. table VideoElementcolumnsSystemElementID int pk,fkVideoMimeType varchar(300)VideoLocationURL varchar(50)4 table MetatagDictionaryMetatagText varchar(400) pkCreatedby(int)datethanks |
 |
|
|
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 tables1.table SystemElementcolumns....SystemElementID intSEVersion intSETitleText varchar(500)SESummaryText varchar(500)2. table TextElementcolumnsSystemElementID int pk,fkElementText ntextElementURLText varchar(50)3. table VideoElementcolumnsSystemElementID int pk,fkVideoMimeType varchar(300)VideoLocationURL varchar(50)4 table MetatagDictionaryMetatagText varchar(400) pkCreatedby(int)datethanks
how is MetatagDictionary related to SystemElement? or is it independent?also wil be certain that data in ElementText will be less than 4000 chars? |
 |
|
|
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 togetheras 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 4Incorrect syntax near the keyword 'AND'.Msg 102, Level 15, State 1, Line 10Incorrect syntax near ','.Msg 156, Level 15, State 1, Line 16Incorrect 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)) ASSET NOCOUNT ON-- Show the resultsDECLARE @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 SESummaryTextFROM ' + @tblname + 'WHERE FREETEXT(SESummaryText, ' + QUOTENAME(@Text, '''') + ' AND ' + QUOTENAME(@Text1, '''') + ' OR ' + QUOTENAME(@Text2, '''') + ')'EXEC (@SQL)will be waiting for replyThank You |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
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 MetaTagMetaTagID pkSystemElementID fkMetaTagText fkMetaTagtypecode""also wil be certain that data in ElementText will be less than 4000 chars?"""Yes I think soThank You very much for your time.... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-29 : 13:14:24
|
| [code]SELECT se.*,t.StrValue,t.LocValueFROM SystemElement seLEFT JOIN (SELECT SystemElementID,VideoMimeType AS StrValue,VideoLocationURL AS LocValue,'Video' AS TypeValueFROM VideoElementUNION ALLSELECT SystemElementID,CAST(ElementText AS nvarchar(400)),ElementURLText,'Element'FROM TextElementUNION ALLSELECT mt.SystemElementID,mtd.MetaTagText,NULL,'MetaTag'FROM MetaTag mtINNER JOIN MetatagDictionary mtdON mt.MetaTagText=mtd.MetaTagText)tON t.SystemElementID=se.SystemElementIDWHERE TypeValue=@TypeValueAND (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. |
 |
|
|
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 thisSELECT SystemElementID,VideoMimeType AS StrValue,VideoLocationURL AS LocValue,'Video' AS TypeValuewhy SystemElementID,VideoMimeType AS StrValueand VideoLocationURL AS LocValue,'Video' AS TypeValueand 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 replyThank You |
 |
|
|
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 thisSELECT SystemElementID,VideoMimeType AS StrValue,VideoLocationURL AS LocValue,'Video' AS TypeValuewhy SystemElementID,VideoMimeType AS StrValueand VideoLocationURL AS LocValue,'Video' AS TypeValueand 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 replyThank 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. |
 |
|
|
|
|
|
|
|