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 |
|
Skydolphin
Starting Member
43 Posts |
Posted - 2003-10-09 : 13:54:47
|
| Hi. I found a script on the net a while back that allows you to parse a users input by , or whatever and then builds the correct select statement. See code posted below.CREATE PROCEDURE dbo.GetLayerList@SearchString VARCHAR(1000)ASBEGINDECLARE @SqlStr VARCHAR(6000) DECLARE @SelectStr VARCHAR(1000)DECLARE @WhereStr VARCHAR(500)DECLARE @FromStr VARCHAR(1000)DECLARE @OrderByStr VARCHAR(500)DECLARE @keyword VARCHAR(100)DECLARE @separator char(1)DECLARE @separator_position int -- This is used to locate each separator characterSELECT @SearchString = Replace(@SearchString,' ',',')SELECT @separator = ','SELECT @SelectStr = "SELECT LayerName, LayerText, LayerTextType, LayerItemCount, ControlType, SubTopic "SELECT @FromStr = " FROM [Layer_List]"SELECT @OrderByStr = " order by LayerName, layerTextType"set nocount onset @SearchString = @SearchString + @separatorwhile patindex('%' + @separator + '%' , @SearchString) <> 0 beginselect @separator_position = patindex('%' + @separator + '%' , @SearchString)select @keyword = left(@SearchString, @separator_position - 1)If @WhereStr = "" SELECT @WhereStr = "( LayerName like '%" + @keyword + "%' or LayerText like '%" + @keyword + "%' or Keywords like '%" + @keyword + "%' )"ElseSELECT @WhereStr = @WhereStr + " And ( LayerName like '%" + @keyword + "%' or LayerText like '%" + @keyword + "%' or Keywords like '%" + @keyword + "%')"-- This replaces what we just processed with and empty stringselect @SearchString = stuff(@SearchString, 1, @separator_position, '')end -- end while loopset nocount offSELECT @SqlStr = @SelectStr + @FromStr + @WhereStr + @OrderByStrEXECUTE (@SqlStr) ENDGOAnyway, I am getting an error cannot use empty object or column names. Use a single space if necessary.Does anyone know what this means and where I should look. I am not a SQL pro so this is very frustrating.ThanksRhondaRhonda |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-10-09 : 14:06:53
|
| Query Analyzer will tell you the line number that is having the problem. When I copied your code into QA, it said error on line 24. You can then double click on the error to bring you to the line that is having the problem.Anyway, change line 24 to this:If @WhereStr = '' (two single quotes)Instead of this:If @WhereStr = "" (two double quotes)Tara |
 |
|
|
Skydolphin
Starting Member
43 Posts |
Posted - 2003-10-09 : 14:17:54
|
| Wow Tara thanks.I never knew you could run stored proc create code in QA. Still has other problems but at least I got passed that one.RhondaRhonda |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-10-09 : 14:22:08
|
| What are you using to create the stored procedures? Enterprise Manager? EM will cause problems. Use Query Analyzer for T-SQL code.Tara |
 |
|
|
Skydolphin
Starting Member
43 Posts |
Posted - 2003-10-09 : 14:29:35
|
| Yea I've always only used EM for sprocs.I've run into another hurdle. It giving me an invalid column name on the lines. SELECT @SelectStr = "SELECT LayerName, LayerText, LayerTextType, LayerItemCount, ControlType, SubTopic"SELECT @FromStr = " FROM dbo.Layer_List"SELECT @OrderByStr = " order by LayerName, layerTextType"and anywhere else the column names are listed but if I run the query directly it is fine. Any ideas?SELECT LayerName, LayerText, LayerTextType, LayerItemCount, ControlType, SubTopic FROM [Layer_List] WHERE LayerName like '%foo%' or LayerText like '%foo%' or Keywords like '%foo%' order by LayerName, layerTextTypeRhonda |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-10-09 : 14:37:27
|
| Put some PRINT statements in your code (use QA for this). Check to see if the output looks correct:CREATE PROCEDURE dbo.GetLayerList@SearchString VARCHAR(1000)ASBEGINDECLARE @SqlStr VARCHAR(6000) DECLARE @SelectStr VARCHAR(1000)DECLARE @WhereStr VARCHAR(500)DECLARE @FromStr VARCHAR(1000)DECLARE @OrderByStr VARCHAR(500)DECLARE @keyword VARCHAR(100)DECLARE @separator char(1)DECLARE @separator_position int -- This is used to locate each separator characterSELECT @SearchString = Replace(@SearchString,' ',',')SELECT @separator = ','SELECT @SelectStr = "SELECT LayerName, LayerText, LayerTextType, LayerItemCount, ControlType, SubTopic "SELECT @FromStr = " FROM [Layer_List]"SELECT @OrderByStr = " order by LayerName, layerTextType"set nocount onset @SearchString = @SearchString + @separatorwhile patindex('%' + @separator + '%' , @SearchString) <> 0 beginselect @separator_position = patindex('%' + @separator + '%' , @SearchString)select @keyword = left(@SearchString, @separator_position - 1)If @WhereStr = "" SELECT @WhereStr = "( LayerName like '%" + @keyword + "%' or LayerText like '%" + @keyword + "%' or Keywords like '%" + @keyword + "%' )"ElseSELECT @WhereStr = @WhereStr + " And ( LayerName like '%" + @keyword + "%' or LayerText like '%" + @keyword + "%' or Keywords like '%" + @keyword + "%')"-- This replaces what we just processed with and empty stringselect @SearchString = stuff(@SearchString, 1, @separator_position, '')end -- end while loopset nocount off--SELECT @SqlStr = @SelectStr + @FromStr + @WhereStr + @OrderByStrPRINT @SelectStrPRINT @FromStrPRINT @WhereStrPRINT @OrderByStr--EXECUTE (@SqlStr) ENDGOTara |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Skydolphin
Starting Member
43 Posts |
Posted - 2003-10-09 : 15:11:24
|
| Phew got that all figured out. Thanks everyone for you help.RhondaRhonda |
 |
|
|
|
|
|
|
|