Tuesday, March 27, 2012
Easy SQL 7 Replication question
I have replication b/w 2 SQL 7.0 SP4 servers over a t1
connection. The type of replication is non-immediate
updating transactional replication pushing 1 way.
Sometimes the t1 connection will die. When the connection
comes back up the SQL Servers begin replicating again.
What happens to the transactions on the push server during
the connection loss? Are they stored in the distribution
DB until the SQL Servers can communicate again?
Please advise...
Thanks,
Louis
Louis,
they are never lost - they stay in MSrepl_commands on the distribution
database until all subscribers have read them or they have reached the
Transaction Retention Period (assuming non-anonymous subscribers).
Regards,
Paul Ibison
|||I believe these commands will reside in the MSreplication_queue table.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
"Paul Ibison" wrote:
> Louis,
> they are never lost - they stay in MSrepl_commands on the distribution
> database until all subscribers have read them or they have reached the
> Transaction Retention Period (assuming non-anonymous subscribers).
> Regards,
> Paul Ibison
>
>
|||Thanks for the help, Paul.
>--Original Message--
>Louis,
>they are never lost - they stay in MSrepl_commands on the
distribution
>database until all subscribers have read them or they
have reached the
>Transaction Retention Period (assuming non-anonymous
subscribers).
>Regards,
>Paul Ibison
>
>.
>
|||MSreplication_queue is probably also of interest, but Louis mentions he is
referring to transactions on the push server.
Cheers,
Paul
|||yikes, that's correct Paul.
Hilary Cotter
Looking for a book on SQL Server replication?
http://www.nwsu.com/0974973602.html
"Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
news:OKCEZ$FWEHA.3892@.TK2MSFTNGP10.phx.gbl...
> MSreplication_queue is probably also of interest, but Louis mentions he is
> referring to transactions on the push server.
> Cheers,
> Paul
>
Monday, March 26, 2012
Easy Newb Q? Cannot find data type xml?
CREATE TABLE T1(Col1 int primary key, Col2 xml)
gives me
Column or parameter #2: Cannot find data type xml.
I copied that straight out of the help. I must be missing something really simple here such as an "Enable XML" in a properties window somewhere? Thanks!
You are in SQL Management Studio 2005 but are you sure you are connected to a SQL Server 2005 instance ? Perhaps you have a SQL 2000 Server instance on your machine and connected to it ?|||
I have the same problem.
This machine has never had any other version of SQL other than SQL 2005.
Created database in Management Studio.
|||Greetings,
I have encountered the same problem "Cannot find data type xml.".
I see that you have encountered the same problem but I have not been able to find the answer.
In my case I have a newly loaded Windows 2000 Server with all of the service packs and the trail version of SQL 2005 Exterprise.
When I try to create the table from the example in the book, which is the same create table example as above I get the message "Cannot find data type xml"
If I go into object explorer, tables, new tables and try and create the table from their xml is not listed as one of the data types.
Thanks for your help.
Easy Newb Q? Cannot find data type xml?
in Microsoft SQL Server Management Studio 2005. I have all other
datatypes except XML... I thought it was supposed to be "built in".
CREATE TABLE T1(Col1 int primary key, Col2 xml)
gives me
Column or parameter #2: Cannot find data type xml.
I copied that straight out of the help. I must be missing something
really simple here such as an "Enable XML" in a properties window
somewhere? Thanks! You are in SQL Management Studio 2005 but are you sure you are connected to a SQL Server 2005 instance ? Perhaps you have a SQL 2000 Server instance on your machine and connected to it ?|||
I have the same problem.
This machine has never had any other version of SQL other than SQL 2005.
Created database in Management Studio.
|||Greetings,
I have encountered the same problem "Cannot find data type xml.".
I see that you have encountered the same problem but I have not been able to find the answer.
In my case I have a newly loaded Windows 2000 Server with all of the service packs and the trail version of SQL 2005 Exterprise.
When I try to create the table from the example in the book, which is the same create table example as above I get the message "Cannot find data type xml"
If I go into object explorer, tables, new tables and try and create the table from their xml is not listed as one of the data types.
Thanks for your help.
Easy Newb Q? Cannot find data type xml?
CREATE TABLE T1(Col1 int primary key, Col2 xml)
gives me
Column or parameter #2: Cannot find data type xml.
I copied that straight out of the help. I must be missing something really simple here such as an "Enable XML" in a properties window somewhere? Thanks!
You are in SQL Management Studio 2005 but are you sure you are connected to a SQL Server 2005 instance ? Perhaps you have a SQL 2000 Server instance on your machine and connected to it ?|||
I have the same problem.
This machine has never had any other version of SQL other than SQL 2005.
Created database in Management Studio.
|||Greetings,
I have encountered the same problem "Cannot find data type xml.".
I see that you have encountered the same problem but I have not been able to find the answer.
In my case I have a newly loaded Windows 2000 Server with all of the service packs and the trail version of SQL 2005 Exterprise.
When I try to create the table from the example in the book, which is the same create table example as above I get the message "Cannot find data type xml"
If I go into object explorer, tables, new tables and try and create the table from their xml is not listed as one of the data types.
Thanks for your help.
sql
Monday, March 19, 2012
Dynamically set chart type?
-mdbTo my knowledge, chart type is not available at run time, only design
time.
Andy Potter|||Howdy,
Being a complete newbie to reporting services I can think of 2 Q & D
ways to do this.
1. Add all your charts of various type to the report and set visibility
hidden=true on all but the one chart indicated by the parameter, or
2. Add and configure a chart with designer, then copy the XML in the
code view. Repeat for all chart types. Then paste all chart snippets
into the rdl with a case/switch statement for the parameter.
1. is probably quicker and dirtier, 2. is perhaps a little less
inelegant.
HTH,
Sean G.|||FYI,
YMMV, but I couldn't do much with suggestion 2 neither with the chart
<Type> nor the entire <Chart> element.
But no trouble at all with suggestion 1. Add all desired charts in
designer and set visibility based on your parameter.
Sean G.|||SeanGerman@.gmail.com wrote in news:1136925323.993091.53580
@.f14g2000cwb.googlegroups.com:
> But no trouble at all with suggestion 1. Add all desired charts in
> designer and set visibility based on your parameter.
Problem with that is that the data probably gets queried once for each
chart, no? In other words, the charts are being generated, you just can't
see them.
Its ok I found another way to do what I needed to do. Thanks!
-mdb
Sunday, March 11, 2012
dynamically delete data
I have the following situation.
Every month, I populate data from a source table.
This table has a field called process_date (char data type) and the
format is mmyy. So, 0406 means data for the month of April of 2006.
This source table always overlaps with old data. For example, for this
month it may have data for January, February or March of 2006, which I
already have processed.
What I do presently is I manually run a delete command and then insert
in the target table.
Such as:
delete Table1 where Process_Date<>'0406'
I want to make this automated so that I will not have to manually run
the above code.
I was wondering how could I achieve that?
I will highly appreciate your help.
Thanks a million in advance.
Best regards,
MamunHello Mamun,
You could create a SQL Server Agent job to run every month. This job
can execute the T-SQL statements you require to insert/delete the
required data and won't require any intervention by you (although you
should be checking that whenever the job executes it executes
successfully).
If you're new to creating SQL Server Agent jobs then SQL Server Books
Online should be able to run you through the process.
HTH,
Nate.
mamun wrote:
> Hi All,
> I have the following situation.
> Every month, I populate data from a source table.
> This table has a field called process_date (char data type) and the
> format is mmyy. So, 0406 means data for the month of April of 2006.
> This source table always overlaps with old data. For example, for this
> month it may have data for January, February or March of 2006, which I
> already have processed.
> What I do presently is I manually run a delete command and then insert
> in the target table.
> Such as:
> delete Table1 where Process_Date<>'0406'
> I want to make this automated so that I will not have to manually run
> the above code.
> I was wondering how could I achieve that?
> I will highly appreciate your help.
> Thanks a million in advance.
> Best regards,
> Mamun
Friday, March 9, 2012
Dynamically Change SSIS For Each Loop container
Hello,Use the Expressions property, and create an expression for FileSpec property that references the global variable.|||I would like to modify "Files" attribute of the Foreach Loop of type File
Enumerator. This attribute is used to set the mask (for example *.txt) to
specify which files to include in the selection. I need to be able to change
this mask dynamically depending on package global variable. Is this possible?
Thank you!
Michael
More details on How To get to the Extressions Property -
Open the ForEach Loop Editor by double clicking ForEach Loop Container.
Select Collection on left.
Click on the + sign on Expressions
Select FileSpec for Property and On Expression select the Global Variable Name. (which holds the file property such as *.txt)
Thanks,
Loonysan
Sunday, February 26, 2012
Dynamic updating of Parameters.
Hi I am using sql server reporting services 2000 and in a report I have more than 20 parameters of data type Boolean. I want another parameter drop down which have "select all" and "clear all" options. When user select “select all” option from the parameter list all 20 parameters value should be false.
I will be very thankful.
Regards,
Faisal Saleem
Hi Faisal,
try to use "cascading parameters" and "default values".
More info here:
http://msdn2.microsoft.com/en-us/library/ms170246.aspx
cheers
Markus
|||Firstly cascading/dependent parameters only work when they are populated from a dataset that uses another parameter. This is not the case here as these are boolean parameters.
Secondly, default values are only applied when the report first loads. So once the user changes any of the parameters, default values are no longer considered and hence trying to use use default values with deperndent parameters will not work.
Cascading parameters are used to modify/effect the list of available values rather than the one that's actually selected.
Although at first is seems like this should be possible, I don't think it is.
Instead, can I suggest that you change the "select all" parameter to contain 3 values:
All
None
Custom
|||
Adam Tappis - IMGroup wrote:
Secondly, default values are only applied when the report first loads. So once the user changes any of the parameters, default values are no longer considered and hence trying to use use default values with deperndent parameters will not work.
Cascading parameters are used to modify/effect the list of available values rather than the one that's actually selected.
I just ran into this and it seems like a functional bug to me. It's great that cascading parameters can be used to alter drop-down options for subsequent parameters, but that's only part of the story. If subsequent parameter choices can be altered by a previous parameter selection, it is completely misleading not to do the same for setting defaults. The users find it very confusing.
Anyone know if this has been logged to Connect?
|||
GregsListAcct wrote:
Anyone know if this has been logged to Connect?
Just answered my own question. It is logged in Connect:
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=268032
Microsoft is considering it "by design". :^(
Dynamic updating of Parameters.
Hi I am using sql server reporting services 2000 and in a report I have more than 20 parameters of data type Boolean. I want another parameter drop down which have "select all" and "clear all" options. When user select “select all” option from the parameter list all 20 parameters value should be false.
I will be very thankful.
Regards,
Faisal Saleem
Hi Faisal,
try to use "cascading parameters" and "default values".
More info here:
http://msdn2.microsoft.com/en-us/library/ms170246.aspx
cheers
Markus
|||Firstly cascading/dependent parameters only work when they are populated from a dataset that uses another parameter. This is not the case here as these are boolean parameters.
Secondly, default values are only applied when the report first loads. So once the user changes any of the parameters, default values are no longer considered and hence trying to use use default values with deperndent parameters will not work.
Cascading parameters are used to modify/effect the list of available values rather than the one that's actually selected.
Although at first is seems like this should be possible, I don't think it is.
Instead, can I suggest that you change the "select all" parameter to contain 3 values:
All
None
Custom
|||
Adam Tappis - IMGroup wrote:
Secondly, default values are only applied when the report first loads. So once the user changes any of the parameters, default values are no longer considered and hence trying to use use default values with deperndent parameters will not work.
Cascading parameters are used to modify/effect the list of available values rather than the one that's actually selected.
I just ran into this and it seems like a functional bug to me. It's great that cascading parameters can be used to alter drop-down options for subsequent parameters, but that's only part of the story. If subsequent parameter choices can be altered by a previous parameter selection, it is completely misleading not to do the same for setting defaults. The users find it very confusing.
Anyone know if this has been logged to Connect?
|||
GregsListAcct wrote:
Anyone know if this has been logged to Connect?
Just answered my own question. It is logged in Connect:
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=268032
Microsoft is considering it "by design". :^(
Dynamic updating of Parameters.
Hi I am using sql server reporting services 2000 and in a report I have more than 20 parameters of data type Boolean. I want another parameter drop down which have "select all" and "clear all" options. When user select “select all” option from the parameter list all 20 parameters value should be false.
I will be very thankful.
Regards,
Faisal Saleem
Hi Faisal,
try to use "cascading parameters" and "default values".
More info here:
http://msdn2.microsoft.com/en-us/library/ms170246.aspx
cheers
Markus
|||Firstly cascading/dependent parameters only work when they are populated from a dataset that uses another parameter. This is not the case here as these are boolean parameters.
Secondly, default values are only applied when the report first loads. So once the user changes any of the parameters, default values are no longer considered and hence trying to use use default values with deperndent parameters will not work.
Cascading parameters are used to modify/effect the list of available values rather than the one that's actually selected.
Although at first is seems like this should be possible, I don't think it is.
Instead, can I suggest that you change the "select all" parameter to contain 3 values:
All
None
Custom
|||
Adam Tappis - IMGroup wrote:
Secondly, default values are only applied when the report first loads. So once the user changes any of the parameters, default values are no longer considered and hence trying to use use default values with deperndent parameters will not work.
Cascading parameters are used to modify/effect the list of available values rather than the one that's actually selected.
I just ran into this and it seems like a functional bug to me. It's great that cascading parameters can be used to alter drop-down options for subsequent parameters, but that's only part of the story. If subsequent parameter choices can be altered by a previous parameter selection, it is completely misleading not to do the same for setting defaults. The users find it very confusing.
Anyone know if this has been logged to Connect?
|||
GregsListAcct wrote:
Anyone know if this has been logged to Connect?
Just answered my own question. It is logged in Connect:
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=268032
Microsoft is considering it "by design". :^(
Sunday, February 19, 2012
Dynamic SqlDataSource
Ok, here's my situation.
I have a dynamic page that accepts a "type" query string. This type query string is the name of a table I want to display on the page in the GridView. Creating a different SqlDataSource/Strongly typed class for every type isn't possible because I need to make this flexible for future updates. So therefor I basically need to be able to Sort, Page, Edit and Delete with the Grid View without knowing the table name at compile-time.
Any help is appreciated,
Thanks!
Displaying the table contents is relatively straightforward. In your code-behind, you just create a SelectCommand of SELECT * FROM " + Request.QueryString["table"]. Of course, you would have to verify that the contents of the querystring is a valid value. The bigger problem is with the update and delete commands. You could create stored procedures for all tables, then fetch the names of the appropriate ones from a database depending on the value of "table" in the querystring. But that isn't particularly flexible. An alternative is toprogrammatically examine the selected table's schema, and dynamically construct update and delete commands based on the column names.|||Thanks for your response. I actually found a way to dynamically get the data itself to enable paging/sorting. I did this just by creating a stored procedure called GetTable with a @.table parameter. So this works well. As for updating and deleting I'll look into that, thanks.|||At least you are using Stored Procedures. Kewl!dynamic SQL versus a huge IF structure
I have a stored proc intented to search a table on different fields,
depending on the search type.
Up until now, I have implemented 3 options, which is by kanji, kana and
english. I'll add many others, like strokes count, radical # and so on.
Here it is, with the 3 options (it may seem overwhelming but is only
because of the IF ELSE structure) :
... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
tinyint,
@.search2 nvarchar(200) = '', @.detailed bit AS
-- @.type : 1 = kanji
-- 2 = english
-- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
/ nanori)
-- @.search2 is there in case we have to search by pronunciation, we
must
-- be able to search in hiragana and katakana. In that case,
-- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
-- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
-- nanori (name) pronunciation is also in HIRAGANA ans therefore is
@.search2.
BEGIN
SET NOCOUNT ON
-- return fields for detailed listing
IF @.detailed = 1
BEGIN
IF @.type = 1
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #KanjiIdsByKanji
FROM dbo.Kanjis
WHERE kanji_kanji = @.search
-- select kanji fields
SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
kanji_nanori, kanji_meaning,
lk_filename, lk_idlesson,
lesson_idlevel, lesson_idlesson,
kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
.. [ * comment: I removed some other table select for the sake of
the simplicity post ]
END
ELSE IF @.type = 2
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #KanjiIdsByEn
FROM dbo.Kanjis
WHERE FREETEXT (kanji_meaning, @.search)
-- select kanji fields
SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
kanji_nanori, kanji_meaning,
lk_filename, lk_idlesson,
lesson_idlevel, lesson_idlesson,
kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
.. [ * comment: I removed some other table select for the sake of
the simplicity post ]
END
ELSE IF @.type = 3
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #KanjiIdsByKana
FROM dbo.Kanjis
WHERE FREETEXT (kanji_on, @.search)
UNION ALL
SELECT kanji_id as id
FROM dbo.Kanjis
WHERE FREETEXT (kanji_kun, @.search2)
UNION ALL
SELECT kanji_id as id
FROM dbo.Kanjis
WHERE FREETEXT (kanji_nanori, @.search2)
-- select kanji fields
SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
kanji_nanori, kanji_meaning,
lk_filename, lk_idlesson,
lesson_idlevel, lesson_idlesson,
kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id = id
LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
.. [ * comment: I removed some other table select for the sake of
the simplicity post ]
END
END
ELSE
-- return fields for basic listing
BEGIN
IF @.type = 1
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #BasicKanjiIdsByKanji
FROM dbo.Kanjis
WHERE kanji_kanji = @.search
SELECT kanji_kanji, kanji_meaning
FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
END
ELSE IF @.type = 2
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #BasicKanjiIdsByEn
FROM dbo.Kanjis
WHERE FREETEXT (kanji_meaning, @.search)
SELECT kanji_kanji, kanji_meaning
FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id = id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
END
ELSE IF @.type = 3
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #BasicKanjiIdsByKana
FROM dbo.Kanjis
WHERE FREETEXT (kanji_on, @.search)
UNION ALL
SELECT kanji_id as id
FROM dbo.Kanjis
WHERE FREETEXT (kanji_kun, @.search2)
UNION ALL
SELECT kanji_id as id
FROM dbo.Kanjis
WHERE FREETEXT (kanji_nanori, @.search2)
SELECT kanji_kanji, kanji_meaning, kanji_isjouyou
FROM Kanjis INNER JOIN #BasicKanjiIdsByKana ON kanji_id = id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
END
END
END
Okay, up until now, it's not too bad, I have a first IF to check if I
must return a lot of columns (detailed) or only a few (basic). Then, in
each, I have another IF for each search type, to search the correct
field.
I'd like to know on a performance point of view what is best? Continue
that way and add an IF for each new type (of course, in both detailed
and basic), or use dynamic SQL?
Also, if the answer is the IF ELSE structure, what would be a good way
to implement multiple search types (for example, by english meaning AND
strokes number). Because for now, I am limited to one search type per
query.
Thanks for all your comments and suggestionsHi
Check out http://www.sommarskog.se/dyn-search.html and
http://www.sommarskog.se/dyn-search.html.
It may be possible to rationalise the use of the different temporary tables
into one, in which case your second select might possibly be made the same o
r
more similar and not produce different result sets for each combination,
which may clean up the client code.
You may also want to look at splitting the different sections into their own
stored procedures.
John
"ibiza" wrote:
> Hi,
> I have a stored proc intented to search a table on different fields,
> depending on the search type.
> Up until now, I have implemented 3 options, which is by kanji, kana and
> english. I'll add many others, like strokes count, radical # and so on.
> Here it is, with the 3 options (it may seem overwhelming but is only
> because of the IF ELSE structure) :
> ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> tinyint,
> @.search2 nvarchar(200) = '', @.detailed bit AS
> -- @.type : 1 = kanji
> -- 2 = english
> -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> / nanori)
> -- @.search2 is there in case we have to search by pronunciation, we
> must
> -- be able to search in hiragana and katakana. In that case,
> -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> @.search2.
> BEGIN
> SET NOCOUNT ON
> -- return fields for detailed listing
> IF @.detailed = 1
> BEGIN
> IF @.type = 1
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #KanjiIdsByKanji
> FROM dbo.Kanjis
> WHERE kanji_kanji = @.search
> -- select kanji fields
> SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> kanji_nanori, kanji_meaning,
> lk_filename, lk_idlesson,
> lesson_idlevel, lesson_idlesson,
> kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> ... [ * comment: I removed some other table select for the sake of
> the simplicity post ]
> END
> ELSE IF @.type = 2
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #KanjiIdsByEn
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_meaning, @.search)
> -- select kanji fields
> SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> kanji_nanori, kanji_meaning,
> lk_filename, lk_idlesson,
> lesson_idlevel, lesson_idlesson,
> kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
> LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> ... [ * comment: I removed some other table select for the sake of
> the simplicity post ]
> END
> ELSE IF @.type = 3
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #KanjiIdsByKana
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_on, @.search)
> UNION ALL
> SELECT kanji_id as id
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_kun, @.search2)
> UNION ALL
> SELECT kanji_id as id
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_nanori, @.search2)
> -- select kanji fields
> SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> kanji_nanori, kanji_meaning,
> lk_filename, lk_idlesson,
> lesson_idlevel, lesson_idlesson,
> kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id = id
> LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> ... [ * comment: I removed some other table select for the sake of
> the simplicity post ]
> END
> END
> ELSE
> -- return fields for basic listing
> BEGIN
> IF @.type = 1
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #BasicKanjiIdsByKanji
> FROM dbo.Kanjis
> WHERE kanji_kanji = @.search
> SELECT kanji_kanji, kanji_meaning
> FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> END
> ELSE IF @.type = 2
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #BasicKanjiIdsByEn
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_meaning, @.search)
> SELECT kanji_kanji, kanji_meaning
> FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id = id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> END
> ELSE IF @.type = 3
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #BasicKanjiIdsByKana
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_on, @.search)
> UNION ALL
> SELECT kanji_id as id
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_kun, @.search2)
> UNION ALL
> SELECT kanji_id as id
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_nanori, @.search2)
> SELECT kanji_kanji, kanji_meaning, kanji_isjouyou
> FROM Kanjis INNER JOIN #BasicKanjiIdsByKana ON kanji_id = id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> END
> END
> END
>
> Okay, up until now, it's not too bad, I have a first IF to check if I
> must return a lot of columns (detailed) or only a few (basic). Then, in
> each, I have another IF for each search type, to search the correct
> field.
> I'd like to know on a performance point of view what is best? Continue
> that way and add an IF for each new type (of course, in both detailed
> and basic), or use dynamic SQL?
> Also, if the answer is the IF ELSE structure, what would be a good way
> to implement multiple search types (for example, by english meaning AND
> strokes number). Because for now, I am limited to one search type per
> query.
> Thanks for all your comments and suggestions
>|||Hi, thanks for your reply.
By the way, you gave me the two same links, is that a typo?
> It may be possible to rationalise the use of the different temporary table
s
> into one, in which case your second select might possibly be made the same
or
> more similar and not produce different result sets for each combination,
> which may clean up the client code.
How would you do that? I have trouble to get it :S
thanks for your time
John Bell wrote:[vbcol=seagreen]
> Hi
> Check out http://www.sommarskog.se/dyn-search.html and
> http://www.sommarskog.se/dyn-search.html.
> It may be possible to rationalise the use of the different temporary table
s
> into one, in which case your second select might possibly be made the same
or
> more similar and not produce different result sets for each combination,
> which may clean up the client code.
> You may also want to look at splitting the different sections into their o
wn
> stored procedures.
> John
> "ibiza" wrote:
>|||Hi
"ibiza" wrote:
> Hi, thanks for your reply.
> By the way, you gave me the two same links, is that a typo?
Yes it was a cut and paste issue! Try
http://www.sommarskog.se/dynamic_sql.html
>
> How would you do that? I have trouble to get it :S
Looking at the code you seem to have very similar temporary tables with
different name. Without looking in depth it struck me that if you just
created a single table at the begining you may be able to see more
rationalisation. I would have expected the current client to have similar
code to the stored procedure as you seem to be producing unique output for
each one. This could probably be cleaned up if you had the same column names
for everything. If you took this further and always returned the same column
s
then you may be able to clean up the client coding further, the downside may
be that you produce a wider result set, you may need to judge the performanc
e
impact of that.
John
> thanks for your time
> John Bell wrote:
>|||Hi,
yes, in fact each of my temporary tables strictly contains an id. Each
IF branch (now splitted into sub-procedures) fills that temp table with
the ids related to the search, then do many select queries based on
those ids.
I've change the SELECT INTO and splitted each IF into sub-procedures,
here it is now (with some commented example search at the beginning) :
--EXECUTE [DicKanjiSearch] N'=E5=8F=8B', 1, N'', 0
--EXECUTE [DicKanjiSearch] N'=E5=8B=9D', 1, N'', 1
--EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
--EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
--EXECUTE [DicKanjiSearch] N'=E3=82=B8=E3=83=A5=E3=82=A6', 3, N'=E3=81=9
8=
=E3=82=85=E3=81=86', 0
--EXECUTE [DicKanjiSearch] N'=E3=82=B3=E3=82=B3=E3=83=AD', 3, N'=E3=81=9
3=
=E3=81=93=E3=82=8D', 1
--EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
--EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
tinyint,
@.search2 nvarchar(200) =3D '', @.detailed bit AS
-- @.type : 1 =3D kanji
-- 2 =3D english
-- 3 =3D hiragana or katakana : pronunciation (on'yomi / kun'yomi
/ nanori)
-- @.search2 is there in case we have to search by pronunciation, we
must
-- be able to search in hiragana and katakana. In that case,
-- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
-- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
-- nanori (name) pronunciation is also in HIRAGANA ans therefore is
@.search2.
BEGIN
SET NOCOUNT ON
-- temp table to hold ids
CREATE TABLE #TempTable (id int PRIMARY KEY)
-- return fields for detailed listing
IF @.detailed =3D 1
BEGIN
IF @.type =3D 1
EXEC DictKanjiGetDetailedByKanji @.search
ELSE IF @.type =3D 2
EXEC DictKanjiGetDetailedByMeaning @.search
ELSE IF @.type =3D 3
EXEC DictKanjiGetDetailedByPron @.search,
@.search2
ELSE IF @.type =3D 4
This will be a new type : by stroke count
EXEC DictKanjiGetDetailedByStrokesCount
CAST(@.search as tinyint)
END
ELSE
-- return fields for basic listing
BEGIN
IF @.type =3D 1
EXEC DictKanjiGetBasicByKanji @.search
ELSE IF @.type =3D 2
EXEC DictKanjiGetBasicByMeaning @.search
ELSE IF @.type =3D 3
EXEC DictKanjiGetBasicByPron @.search, @.search2
ELSE IF @.type =3D 4
This will be a new type : by stroke count
EXEC DictKanjiGetBasicByStrokesCount CAST(@.search
as tinyint)
END
END
Now, after reading your two excellent articles (many thanks for that),
it seems that, I quote, "dynamic SQL is often the best solution, both
for performance and maintainability"...Now I have a concern with the
method I am using, because it's static SQL :| what do you think about
that?
Finally, is there a way to 'merge' queries together with the method I
am currently using? That's because I added the search type 4, which
is by kanji strokes count, I'd like to be able to search for by more
than one search type. (e.g., strokes count AND english term).
I see two ways of passing multiple parameters here. Either
1) have a @.parameter_name for each type possible (e.g.: @.meaning =3D
NULL, @.kanji =3D NULL, @.pronH =3D NULL, @.pronK =3D NULL, @.strokescount =3D
NULL, ...), then check which ones are assigned, then execute
corresponding queries.
2) use my @.search paramater as a container, like "7&name", and have my
@.type parameter something like "strokecount&meaning" (or the more
shorter "sc&en") and parse then to execute corresponding queries.
Which one would perfrom faster?
Then I still need a way to merge SELECT tables together...Any ideas?
Thank you very much!
John Bell wrote:
> Hi
> "ibiza" wrote:
>
> Yes it was a cut and paste issue! Try
> http://www.sommarskog.se/dynamic_sql.html
>
tables[vbcol=seagreen]
same or[vbcol=seagreen]
on,[vbcol=seagreen]
> Looking at the code you seem to have very similar temporary tables with
> different name. Without looking in depth it struck me that if you just
> created a single table at the begining you may be able to see more
> rationalisation. I would have expected the current client to have similar
> code to the stored procedure as you seem to be producing unique output for
> each one. This could probably be cleaned up if you had the same column na=
mes
> for everything. If you took this further and always returned the same col=
umns
> then you may be able to clean up the client coding further, the downside =
may
> be that you produce a wider result set, you may need to judge the perform=
ance[vbcol=seagreen]
> impact of that.
>
> John
tables[vbcol=seagreen]
same or[vbcol=seagreen]
on,[vbcol=seagreen]
eir own[vbcol=seagreen]
and[vbcol=seagreen]
on.[vbcol=seagreen]
n'yomi[vbcol=seagreen]
of[vbcol=seagreen]
of[vbcol=seagreen]
of[vbcol=seagreen]
I[vbcol=seagreen]
, in[vbcol=seagreen]
nue[vbcol=seagreen]
ed[vbcol=seagreen]
way[vbcol=seagreen]
AND[vbcol=seagreen]
er[vbcol=seagreen]|||Hi
The only way to see if dynamic SQL is quicker would be to implement it. In
general I would only do that if you think your current solution is too slow.
To implement multiple criteria you could separate your procedures into two
bring the second select into DicKanjiSearch, you can then call the various
procedures to add new ids to the temporary table multiple times.
John
"ibiza" wrote:
[vbcol=seagreen]
> Hi,
> yes, in fact each of my temporary tables strictly contains an id. Each
> IF branch (now splitted into sub-procedures) fills that temp table with
> the ids related to the search, then do many select queries based on
> those ids.
> I've change the SELECT INTO and splitted each IF into sub-procedures,
> here it is now (with some commented example search at the beginning) :
> --EXECUTE [DicKanjiSearch] N'友', 1, N'', 0
> --EXECUTE [DicKanjiSearch] N'勝', 1, N'', 1
> --EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> --EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
> --EXECUTE [DicKanjiSearch] N'ジュウ', 3, N'じゅう', 0
> --EXECUTE [DicKanjiSearch] N'ココ_', 3, N'こころ', 1
> --EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
> --EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
> ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.typ
e
> tinyint,
> @.search2 nvarchar(200) = '', @.detailed bit AS
> -- @.type : 1 = kanji
> -- 2 = english
> -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> / nanori)
> -- @.search2 is there in case we have to search by pronunciation, we
> must
> -- be able to search in hiragana and katakana. In that case,
> -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> @.search2.
> BEGIN
> SET NOCOUNT ON
> -- temp table to hold ids
> CREATE TABLE #TempTable (id int PRIMARY KEY)
> -- return fields for detailed listing
> IF @.detailed = 1
> BEGIN
> IF @.type = 1
> EXEC DictKanjiGetDetailedByKanji @.search
> ELSE IF @.type = 2
> EXEC DictKanjiGetDetailedByMeaning @.search
> ELSE IF @.type = 3
> EXEC DictKanjiGetDetailedByPron @.search,
> @.search2
> ELSE IF @.type = 4
> This will be a new type : by stroke count
> EXEC DictKanjiGetDetailedByStrokesCount
> CAST(@.search as tinyint)
> END
> ELSE
> -- return fields for basic listing
> BEGIN
> IF @.type = 1
> EXEC DictKanjiGetBasicByKanji @.search
> ELSE IF @.type = 2
> EXEC DictKanjiGetBasicByMeaning @.search
> ELSE IF @.type = 3
> EXEC DictKanjiGetBasicByPron @.search, @.search2
> ELSE IF @.type = 4
> This will be a new type : by stroke count
> EXEC DictKanjiGetBasicByStrokesCount CAST(@.search
> as tinyint)
> END
> END
> Now, after reading your two excellent articles (many thanks for that),
> it seems that, I quote, "dynamic SQL is often the best solution, both
> for performance and maintainability"...Now I have a concern with the
> method I am using, because it's static SQL :| what do you think about
> that?
> Finally, is there a way to 'merge' queries together with the method I
> am currently using? That's because I added the search type 4, which
> is by kanji strokes count, I'd like to be able to search for by more
> than one search type. (e.g., strokes count AND english term).
> I see two ways of passing multiple parameters here. Either
> 1) have a @.parameter_name for each type possible (e.g.: @.meaning =
> NULL, @.kanji = NULL, @.pronH = NULL, @.pronK = NULL, @.strokescount =
> NULL, ...), then check which ones are assigned, then execute
> corresponding queries.
> 2) use my @.search paramater as a container, like "7&name", and have my
> @.type parameter something like "strokecount&meaning" (or the more
> shorter "sc&en") and parse then to execute corresponding queries.
> Which one would perfrom faster?
> Then I still need a way to merge SELECT tables together...Any ideas?
> Thank you very much!
> John Bell wrote:|||Hello,
> To implement multiple criteria you could separate your procedures into two
> bring the second select into DicKanjiSearch, you can then call the various
> procedures to add new ids to the temporary table multiple times.
I don't really understand. Separating which procedures into two?
Also, multiple criteria would mean a sort of AND implemantation, so
that does not mean "add new ids to the temporary table multiple times"
(this would be an OR, am I correct?)
thanks for your time
John Bell wrote:
> Hi
> The only way to see if dynamic SQL is quicker would be to implement it. In
> general I would only do that if you think your current solution is too sl=
ow.[vbcol=seagreen]
> To implement multiple criteria you could separate your procedures into two
> bring the second select into DicKanjiSearch, you can then call the various
> procedures to add new ids to the temporary table multiple times.
> John
> "ibiza" wrote:
>
=98=E3=82=85=E3=81=86', 0[vbcol=seagreen]
=93=E3=81=93=E3=82=8D', 1[vbcol=seagreen]
mi[vbcol=seagreen]
=3D[vbcol=seagreen]
ary tables[vbcol=seagreen]
the same or[vbcol=seagreen]
nation,[vbcol=seagreen]
th[vbcol=seagreen]
ilar[vbcol=seagreen]
t for[vbcol=seagreen]
n names[vbcol=seagreen]
columns[vbcol=seagreen]
ide may[vbcol=seagreen]
formance[vbcol=seagreen]
ary tables[vbcol=seagreen]
the same or[vbcol=seagreen]
nation,[vbcol=seagreen]
o their own[vbcol=seagreen]
elds,[vbcol=seagreen]
kana and[vbcol=seagreen]
d so on.[vbcol=seagreen]
only[vbcol=seagreen]
ype[vbcol=seagreen]
/ kun'yomi[vbcol=seagreen]
, we[vbcol=seagreen]
h=2E[vbcol=seagreen]
rch2.[vbcol=seagreen]
e is[vbcol=seagreen]
un,[vbcol=seagreen]
eq[vbcol=seagreen]
201)[vbcol=seagreen]
ake of[vbcol=seagreen]
un,[vbcol=seagreen]
eq[vbcol=seagreen]
201)[vbcol=seagreen]
ake of[vbcol=seagreen]
un,[vbcol=seagreen]
eq[vbcol=seagreen]
201)[vbcol=seagreen]
ake of[vbcol=seagreen]
id[vbcol=seagreen]
201)[vbcol=seagreen]
201)[vbcol=seagreen]|||Hi
The second select statement seems to be in two flavours based on Kanji and
LessonKanji! If these are always exclusive then you may want to separate
these into two different procedures.
To get an AND condition you can select the ids where there is more than one
occurence using a group by id and a having clause, providing each time you
add the id you only insert distinct ones.
John
"ibiza" wrote:
[vbcol=seagreen]
> Hello,
>
> I don't really understand. Separating which procedures into two?
> Also, multiple criteria would mean a sort of AND implemantation, so
> that does not mean "add new ids to the temporary table multiple times"
> (this would be an OR, am I correct?)
> thanks for your time
> John Bell wrote:|||:S sorry I'd need examples to understand better what you mean.
I suppose 'the second select statement' you refer to are any select
after the
SELECT kanji_id as id
INTO #TempTable
FROM dbo.Kanjis
WHERE (condition)
into each sub-procedure? How come you say that it seems to be in two
flavours?
thanks,
Bruno
John Bell wrote:
> Hi
> The second select statement seems to be in two flavours based on Kanji and
> LessonKanji! If these are always exclusive then you may want to separate
> these into two different procedures.
> To get an AND condition you can select the ids where there is more than o=
ne
> occurence using a group by id and a having clause, providing each time y=
ou[vbcol=seagreen]
> add the id you only insert distinct ones.
> John
> "ibiza" wrote:
>
o two[vbcol=seagreen]
rious[vbcol=seagreen]
t=2E In[vbcol=seagreen]
o slow.[vbcol=seagreen]
o two[vbcol=seagreen]
rious[vbcol=seagreen]
ach[vbcol=seagreen]
with[vbcol=seagreen]
s,[vbcol=seagreen]
) :[vbcol=seagreen]
=81=98=E3=82=85=E3=81=86', 0[vbcol=seagreen]
=81=93=E3=81=93=E3=82=8D', 1[vbcol=seagreen]
n'yomi[vbcol=seagreen]
ch2[vbcol=seagreen]
arch[vbcol=seagreen]
t),[vbcol=seagreen]
th[vbcol=seagreen]
ut[vbcol=seagreen]
I[vbcol=seagreen]
=3D[vbcol=seagreen]
unt =3D[vbcol=seagreen]
my[vbcol=seagreen]
mporary tables[vbcol=seagreen]
made the same or[vbcol=seagreen]
ombination,[vbcol=seagreen]
s with[vbcol=seagreen]
just[vbcol=seagreen]
re[vbcol=seagreen]
similar[vbcol=seagreen]
utput for[vbcol=seagreen]
olumn names[vbcol=seagreen]
same columns[vbcol=seagreen]
ownside may[vbcol=seagreen]
performance[vbcol=seagreen]
mporary tables[vbcol=seagreen]
made the same or[vbcol=seagreen]
ombination,[vbcol=seagreen]
into their own[vbcol=seagreen]
t fields,[vbcol=seagreen]
ji, kana and[vbcol=seagreen]
# and so on.[vbcol=seagreen]
t is only[vbcol=seagreen]
, @.type[vbcol=seagreen]
omi / kun'yomi[vbcol=seagreen]
tion, we[vbcol=seagreen]
earch.[vbcol=seagreen]
@.search2.[vbcol=seagreen]
efore is[vbcol=seagreen]
ji_kun,[vbcol=seagreen]
i_freq[vbcol=seagreen]
id[vbcol=seagreen]
ASC,[vbcol=seagreen]
ng), 201)[vbcol=seagreen]
he sake of[vbcol=seagreen]
ji_kun,[vbcol=seagreen]
i_freq[vbcol=seagreen]
ASC,[vbcol=seagreen]
ng), 201)[vbcol=seagreen]
he sake of[vbcol=seagreen]
ji_kun,[vbcol=seagreen]
i_freq[vbcol=seagreen]
ASC,[vbcol=seagreen]
ng), 201)[vbcol=seagreen]
he sake of[vbcol=seagreen]
=3D id[vbcol=seagreen]
ASC,[vbcol=seagreen]
ng), 201)[vbcol=seagreen]|||Hi
The second select statement seems to be very similar to each other of which
there is the basic information and full information:
SELECT kanji_kanji, kanji_meaning
FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
or
SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
kanji_nanori, kanji_meaning,
lk_filename, lk_idlesson,
lesson_idlevel, lesson_idlesson,
kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
Rather than repeating this in multiple procedures they could be put at a
level where the code would only be written once (helping maintainance). If
you do this then the procedures that populate the temporary tables can just
contain single select statement and you can use INSERT.. EXEC e.g.
INSERT #Tmptable (id)
EXECUTE [DicKanjiSearch] N'友', 1, N'', 0
As the basic listing is a subset of the full listing, then you may want to
return the full listing result set each time and throw away the columns not
used to try and simplify the client code (although normally you would not
want to return excessively wide result sets!).
To get the 'AND' result you can do something like:
CREATE TABLE #Tmptable (id int)
INSERT #Tmptable (id)
EXECUTE [DicKanjiSearch] N'友', 1, N'', 0
INSERT #Tmptable (id)
EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
-- And results will be have two entried one from each insert
SELECT id
FROM #Tmptable
GROUP BY id
HAVING COUNT(*) > 1
I hope that is clearer?
John
"ibiza" wrote:
[vbcol=seagreen]
> :S sorry I'd need examples to understand better what you mean.
> I suppose 'the second select statement' you refer to are any select
> after the
> SELECT kanji_id as id
> INTO #TempTable
> FROM dbo.Kanjis
> WHERE (condition)
> into each sub-procedure? How come you say that it seems to be in two
> flavours?
> thanks,
> Bruno
> John Bell wrote:
dynamic SQL versus a huge IF structure
I have a stored proc intented to search a table on different fields,
depending on the search type.
Up until now, I have implemented 3 options, which is by kanji, kana and
english. I'll add many others, like strokes count, radical # and so on.
Here it is, with the 3 options (it may seem overwhelming but is only
because of the IF ELSE structure) :
... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
tinyint,
@.search2 nvarchar(200) = '', @.detailed bit AS
-- @.type : 1 = kanji
-- 2 = english
-- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
/ nanori)
-- @.search2 is there in case we have to search by pronunciation, we
must
-- be able to search in hiragana and katakana. In that case,
-- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
-- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
-- nanori (name) pronunciation is also in HIRAGANA ans therefore is
@.search2.
BEGIN
SET NOCOUNT ON
-- return fields for detailed listing
IF @.detailed = 1
BEGIN
IF @.type = 1
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #KanjiIdsByKanji
FROM dbo.Kanjis
WHERE kanji_kanji = @.search
-- select kanji fields
SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
kanji_nanori, kanji_meaning,
lk_filename, lk_idlesson,
lesson_idlevel, lesson_idlesson,
kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
... [ * comment: I removed some other table select for the sake of
the simplicity post ]
END
ELSE IF @.type = 2
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #KanjiIdsByEn
FROM dbo.Kanjis
WHERE FREETEXT (kanji_meaning, @.search)
-- select kanji fields
SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
kanji_nanori, kanji_meaning,
lk_filename, lk_idlesson,
lesson_idlevel, lesson_idlesson,
kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
... [ * comment: I removed some other table select for the sake of
the simplicity post ]
END
ELSE IF @.type = 3
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #KanjiIdsByKana
FROM dbo.Kanjis
WHERE FREETEXT (kanji_on, @.search)
UNION ALL
SELECT kanji_id as id
FROM dbo.Kanjis
WHERE FREETEXT (kanji_kun, @.search2)
UNION ALL
SELECT kanji_id as id
FROM dbo.Kanjis
WHERE FREETEXT (kanji_nanori, @.search2)
-- select kanji fields
SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
kanji_nanori, kanji_meaning,
lk_filename, lk_idlesson,
lesson_idlevel, lesson_idlesson,
kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id = id
LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
... [ * comment: I removed some other table select for the sake of
the simplicity post ]
END
END
ELSE
-- return fields for basic listing
BEGIN
IF @.type = 1
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #BasicKanjiIdsByKanji
FROM dbo.Kanjis
WHERE kanji_kanji = @.search
SELECT kanji_kanji, kanji_meaning
FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
END
ELSE IF @.type = 2
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #BasicKanjiIdsByEn
FROM dbo.Kanjis
WHERE FREETEXT (kanji_meaning, @.search)
SELECT kanji_kanji, kanji_meaning
FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id = id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
END
ELSE IF @.type = 3
BEGIN
-- select kanji ids corresponding to the search string
SELECT kanji_id as id
INTO #BasicKanjiIdsByKana
FROM dbo.Kanjis
WHERE FREETEXT (kanji_on, @.search)
UNION ALL
SELECT kanji_id as id
FROM dbo.Kanjis
WHERE FREETEXT (kanji_kun, @.search2)
UNION ALL
SELECT kanji_id as id
FROM dbo.Kanjis
WHERE FREETEXT (kanji_nanori, @.search2)
SELECT kanji_kanji, kanji_meaning, kanji_isjouyou
FROM Kanjis INNER JOIN #BasicKanjiIdsByKana ON kanji_id = id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
ASC
END
END
END
Okay, up until now, it's not too bad, I have a first IF to check if I
must return a lot of columns (detailed) or only a few (basic). Then, in
each, I have another IF for each search type, to search the correct
field.
I'd like to know on a performance point of view what is best? Continue
that way and add an IF for each new type (of course, in both detailed
and basic), or use dynamic SQL?
Also, if the answer is the IF ELSE structure, what would be a good way
to implement multiple search types (for example, by english meaning AND
strokes number). Because for now, I am limited to one search type per
query.
Thanks for all your comments and suggestionsHi
Check out http://www.sommarskog.se/dyn-search.html and
http://www.sommarskog.se/dyn-search.html.
It may be possible to rationalise the use of the different temporary tables
into one, in which case your second select might possibly be made the same or
more similar and not produce different result sets for each combination,
which may clean up the client code.
You may also want to look at splitting the different sections into their own
stored procedures.
John
"ibiza" wrote:
> Hi,
> I have a stored proc intented to search a table on different fields,
> depending on the search type.
> Up until now, I have implemented 3 options, which is by kanji, kana and
> english. I'll add many others, like strokes count, radical # and so on.
> Here it is, with the 3 options (it may seem overwhelming but is only
> because of the IF ELSE structure) :
> ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> tinyint,
> @.search2 nvarchar(200) = '', @.detailed bit AS
> -- @.type : 1 = kanji
> -- 2 = english
> -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> / nanori)
> -- @.search2 is there in case we have to search by pronunciation, we
> must
> -- be able to search in hiragana and katakana. In that case,
> -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> @.search2.
> BEGIN
> SET NOCOUNT ON
> -- return fields for detailed listing
> IF @.detailed = 1
> BEGIN
> IF @.type = 1
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #KanjiIdsByKanji
> FROM dbo.Kanjis
> WHERE kanji_kanji = @.search
> -- select kanji fields
> SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> kanji_nanori, kanji_meaning,
> lk_filename, lk_idlesson,
> lesson_idlevel, lesson_idlesson,
> kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> ... [ * comment: I removed some other table select for the sake of
> the simplicity post ]
> END
> ELSE IF @.type = 2
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #KanjiIdsByEn
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_meaning, @.search)
> -- select kanji fields
> SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> kanji_nanori, kanji_meaning,
> lk_filename, lk_idlesson,
> lesson_idlevel, lesson_idlesson,
> kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
> LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> ... [ * comment: I removed some other table select for the sake of
> the simplicity post ]
> END
> ELSE IF @.type = 3
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #KanjiIdsByKana
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_on, @.search)
> UNION ALL
> SELECT kanji_id as id
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_kun, @.search2)
> UNION ALL
> SELECT kanji_id as id
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_nanori, @.search2)
> -- select kanji fields
> SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> kanji_nanori, kanji_meaning,
> lk_filename, lk_idlesson,
> lesson_idlevel, lesson_idlesson,
> kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id = id
> LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> ... [ * comment: I removed some other table select for the sake of
> the simplicity post ]
> END
> END
> ELSE
> -- return fields for basic listing
> BEGIN
> IF @.type = 1
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #BasicKanjiIdsByKanji
> FROM dbo.Kanjis
> WHERE kanji_kanji = @.search
> SELECT kanji_kanji, kanji_meaning
> FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> END
> ELSE IF @.type = 2
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #BasicKanjiIdsByEn
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_meaning, @.search)
> SELECT kanji_kanji, kanji_meaning
> FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id = id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> END
> ELSE IF @.type = 3
> BEGIN
> -- select kanji ids corresponding to the search string
> SELECT kanji_id as id
> INTO #BasicKanjiIdsByKana
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_on, @.search)
> UNION ALL
> SELECT kanji_id as id
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_kun, @.search2)
> UNION ALL
> SELECT kanji_id as id
> FROM dbo.Kanjis
> WHERE FREETEXT (kanji_nanori, @.search2)
> SELECT kanji_kanji, kanji_meaning, kanji_isjouyou
> FROM Kanjis INNER JOIN #BasicKanjiIdsByKana ON kanji_id = id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> ASC
> END
> END
> END
>
> Okay, up until now, it's not too bad, I have a first IF to check if I
> must return a lot of columns (detailed) or only a few (basic). Then, in
> each, I have another IF for each search type, to search the correct
> field.
> I'd like to know on a performance point of view what is best? Continue
> that way and add an IF for each new type (of course, in both detailed
> and basic), or use dynamic SQL?
> Also, if the answer is the IF ELSE structure, what would be a good way
> to implement multiple search types (for example, by english meaning AND
> strokes number). Because for now, I am limited to one search type per
> query.
> Thanks for all your comments and suggestions
>|||Hi, thanks for your reply.
By the way, you gave me the two same links, is that a typo?
> It may be possible to rationalise the use of the different temporary tables
> into one, in which case your second select might possibly be made the same or
> more similar and not produce different result sets for each combination,
> which may clean up the client code.
How would you do that? I have trouble to get it :S
thanks for your time
John Bell wrote:
> Hi
> Check out http://www.sommarskog.se/dyn-search.html and
> http://www.sommarskog.se/dyn-search.html.
> It may be possible to rationalise the use of the different temporary tables
> into one, in which case your second select might possibly be made the same or
> more similar and not produce different result sets for each combination,
> which may clean up the client code.
> You may also want to look at splitting the different sections into their own
> stored procedures.
> John
> "ibiza" wrote:
> > Hi,
> >
> > I have a stored proc intented to search a table on different fields,
> > depending on the search type.
> >
> > Up until now, I have implemented 3 options, which is by kanji, kana and
> > english. I'll add many others, like strokes count, radical # and so on.
> >
> > Here it is, with the 3 options (it may seem overwhelming but is only
> > because of the IF ELSE structure) :
> >
> > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > tinyint,
> > @.search2 nvarchar(200) = '', @.detailed bit AS
> > -- @.type : 1 = kanji
> > -- 2 = english
> > -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> > / nanori)
> > -- @.search2 is there in case we have to search by pronunciation, we
> > must
> > -- be able to search in hiragana and katakana. In that case,
> > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > @.search2.
> > BEGIN
> > SET NOCOUNT ON
> >
> > -- return fields for detailed listing
> > IF @.detailed = 1
> > BEGIN
> > IF @.type = 1
> > BEGIN
> > -- select kanji ids corresponding to the search string
> > SELECT kanji_id as id
> > INTO #KanjiIdsByKanji
> > FROM dbo.Kanjis
> > WHERE kanji_kanji = @.search
> >
> > -- select kanji fields
> > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > kanji_nanori, kanji_meaning,
> > lk_filename, lk_idlesson,
> > lesson_idlevel, lesson_idlesson,
> > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > ASC
> >
> > ... [ * comment: I removed some other table select for the sake of
> > the simplicity post ]
> > END
> > ELSE IF @.type = 2
> > BEGIN
> > -- select kanji ids corresponding to the search string
> > SELECT kanji_id as id
> > INTO #KanjiIdsByEn
> > FROM dbo.Kanjis
> > WHERE FREETEXT (kanji_meaning, @.search)
> >
> > -- select kanji fields
> > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > kanji_nanori, kanji_meaning,
> > lk_filename, lk_idlesson,
> > lesson_idlevel, lesson_idlesson,
> > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
> > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > ASC
> >
> > ... [ * comment: I removed some other table select for the sake of
> > the simplicity post ]
> > END
> > ELSE IF @.type = 3
> > BEGIN
> > -- select kanji ids corresponding to the search string
> > SELECT kanji_id as id
> > INTO #KanjiIdsByKana
> > FROM dbo.Kanjis
> > WHERE FREETEXT (kanji_on, @.search)
> > UNION ALL
> > SELECT kanji_id as id
> > FROM dbo.Kanjis
> > WHERE FREETEXT (kanji_kun, @.search2)
> > UNION ALL
> > SELECT kanji_id as id
> > FROM dbo.Kanjis
> > WHERE FREETEXT (kanji_nanori, @.search2)
> >
> > -- select kanji fields
> > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > kanji_nanori, kanji_meaning,
> > lk_filename, lk_idlesson,
> > lesson_idlevel, lesson_idlesson,
> > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id = id
> > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > ASC
> >
> > ... [ * comment: I removed some other table select for the sake of
> > the simplicity post ]
> > END
> > END
> > ELSE
> > -- return fields for basic listing
> > BEGIN
> > IF @.type = 1
> > BEGIN
> > -- select kanji ids corresponding to the search string
> > SELECT kanji_id as id
> > INTO #BasicKanjiIdsByKanji
> > FROM dbo.Kanjis
> > WHERE kanji_kanji = @.search
> >
> > SELECT kanji_kanji, kanji_meaning
> > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > ASC
> > END
> > ELSE IF @.type = 2
> > BEGIN
> > -- select kanji ids corresponding to the search string
> > SELECT kanji_id as id
> > INTO #BasicKanjiIdsByEn
> > FROM dbo.Kanjis
> > WHERE FREETEXT (kanji_meaning, @.search)
> >
> > SELECT kanji_kanji, kanji_meaning
> > FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id = id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > ASC
> > END
> > ELSE IF @.type = 3
> > BEGIN
> > -- select kanji ids corresponding to the search string
> > SELECT kanji_id as id
> > INTO #BasicKanjiIdsByKana
> > FROM dbo.Kanjis
> > WHERE FREETEXT (kanji_on, @.search)
> > UNION ALL
> > SELECT kanji_id as id
> > FROM dbo.Kanjis
> > WHERE FREETEXT (kanji_kun, @.search2)
> > UNION ALL
> > SELECT kanji_id as id
> > FROM dbo.Kanjis
> > WHERE FREETEXT (kanji_nanori, @.search2)
> >
> > SELECT kanji_kanji, kanji_meaning, kanji_isjouyou
> > FROM Kanjis INNER JOIN #BasicKanjiIdsByKana ON kanji_id = id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > ASC
> > END
> > END
> > END
> >
> >
> > Okay, up until now, it's not too bad, I have a first IF to check if I
> > must return a lot of columns (detailed) or only a few (basic). Then, in
> > each, I have another IF for each search type, to search the correct
> > field.
> >
> > I'd like to know on a performance point of view what is best? Continue
> > that way and add an IF for each new type (of course, in both detailed
> > and basic), or use dynamic SQL?
> >
> > Also, if the answer is the IF ELSE structure, what would be a good way
> > to implement multiple search types (for example, by english meaning AND
> > strokes number). Because for now, I am limited to one search type per
> > query.
> >
> > Thanks for all your comments and suggestions
> >
> >|||Hi
"ibiza" wrote:
> Hi, thanks for your reply.
> By the way, you gave me the two same links, is that a typo?
Yes it was a cut and paste issue! Try
http://www.sommarskog.se/dynamic_sql.html
> > It may be possible to rationalise the use of the different temporary tables
> > into one, in which case your second select might possibly be made the same or
> > more similar and not produce different result sets for each combination,
> > which may clean up the client code.
> How would you do that? I have trouble to get it :S
Looking at the code you seem to have very similar temporary tables with
different name. Without looking in depth it struck me that if you just
created a single table at the begining you may be able to see more
rationalisation. I would have expected the current client to have similar
code to the stored procedure as you seem to be producing unique output for
each one. This could probably be cleaned up if you had the same column names
for everything. If you took this further and always returned the same columns
then you may be able to clean up the client coding further, the downside may
be that you produce a wider result set, you may need to judge the performance
impact of that.
John
> thanks for your time
> John Bell wrote:
> > Hi
> >
> > Check out http://www.sommarskog.se/dyn-search.html and
> > http://www.sommarskog.se/dyn-search.html.
> >
> > It may be possible to rationalise the use of the different temporary tables
> > into one, in which case your second select might possibly be made the same or
> > more similar and not produce different result sets for each combination,
> > which may clean up the client code.
> >
> > You may also want to look at splitting the different sections into their own
> > stored procedures.
> >
> > John
> >
> > "ibiza" wrote:
> >
> > > Hi,
> > >
> > > I have a stored proc intented to search a table on different fields,
> > > depending on the search type.
> > >
> > > Up until now, I have implemented 3 options, which is by kanji, kana and
> > > english. I'll add many others, like strokes count, radical # and so on.
> > >
> > > Here it is, with the 3 options (it may seem overwhelming but is only
> > > because of the IF ELSE structure) :
> > >
> > > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > tinyint,
> > > @.search2 nvarchar(200) = '', @.detailed bit AS
> > > -- @.type : 1 = kanji
> > > -- 2 = english
> > > -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> > > / nanori)
> > > -- @.search2 is there in case we have to search by pronunciation, we
> > > must
> > > -- be able to search in hiragana and katakana. In that case,
> > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > @.search2.
> > > BEGIN
> > > SET NOCOUNT ON
> > >
> > > -- return fields for detailed listing
> > > IF @.detailed = 1
> > > BEGIN
> > > IF @.type = 1
> > > BEGIN
> > > -- select kanji ids corresponding to the search string
> > > SELECT kanji_id as id
> > > INTO #KanjiIdsByKanji
> > > FROM dbo.Kanjis
> > > WHERE kanji_kanji = @.search
> > >
> > > -- select kanji fields
> > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > kanji_nanori, kanji_meaning,
> > > lk_filename, lk_idlesson,
> > > lesson_idlevel, lesson_idlesson,
> > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > ASC
> > >
> > > ... [ * comment: I removed some other table select for the sake of
> > > the simplicity post ]
> > > END
> > > ELSE IF @.type = 2
> > > BEGIN
> > > -- select kanji ids corresponding to the search string
> > > SELECT kanji_id as id
> > > INTO #KanjiIdsByEn
> > > FROM dbo.Kanjis
> > > WHERE FREETEXT (kanji_meaning, @.search)
> > >
> > > -- select kanji fields
> > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > kanji_nanori, kanji_meaning,
> > > lk_filename, lk_idlesson,
> > > lesson_idlevel, lesson_idlesson,
> > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
> > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > ASC
> > >
> > > ... [ * comment: I removed some other table select for the sake of
> > > the simplicity post ]
> > > END
> > > ELSE IF @.type = 3
> > > BEGIN
> > > -- select kanji ids corresponding to the search string
> > > SELECT kanji_id as id
> > > INTO #KanjiIdsByKana
> > > FROM dbo.Kanjis
> > > WHERE FREETEXT (kanji_on, @.search)
> > > UNION ALL
> > > SELECT kanji_id as id
> > > FROM dbo.Kanjis
> > > WHERE FREETEXT (kanji_kun, @.search2)
> > > UNION ALL
> > > SELECT kanji_id as id
> > > FROM dbo.Kanjis
> > > WHERE FREETEXT (kanji_nanori, @.search2)
> > >
> > > -- select kanji fields
> > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > kanji_nanori, kanji_meaning,
> > > lk_filename, lk_idlesson,
> > > lesson_idlevel, lesson_idlesson,
> > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id = id
> > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > ASC
> > >
> > > ... [ * comment: I removed some other table select for the sake of
> > > the simplicity post ]
> > > END
> > > END
> > > ELSE
> > > -- return fields for basic listing
> > > BEGIN
> > > IF @.type = 1
> > > BEGIN
> > > -- select kanji ids corresponding to the search string
> > > SELECT kanji_id as id
> > > INTO #BasicKanjiIdsByKanji
> > > FROM dbo.Kanjis
> > > WHERE kanji_kanji = @.search
> > >
> > > SELECT kanji_kanji, kanji_meaning
> > > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
> > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > ASC
> > > END
> > > ELSE IF @.type = 2
> > > BEGIN
> > > -- select kanji ids corresponding to the search string
> > > SELECT kanji_id as id
> > > INTO #BasicKanjiIdsByEn
> > > FROM dbo.Kanjis
> > > WHERE FREETEXT (kanji_meaning, @.search)
> > >
> > > SELECT kanji_kanji, kanji_meaning
> > > FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id = id
> > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > ASC
> > > END
> > > ELSE IF @.type = 3
> > > BEGIN
> > > -- select kanji ids corresponding to the search string
> > > SELECT kanji_id as id
> > > INTO #BasicKanjiIdsByKana
> > > FROM dbo.Kanjis
> > > WHERE FREETEXT (kanji_on, @.search)
> > > UNION ALL
> > > SELECT kanji_id as id
> > > FROM dbo.Kanjis
> > > WHERE FREETEXT (kanji_kun, @.search2)
> > > UNION ALL
> > > SELECT kanji_id as id
> > > FROM dbo.Kanjis
> > > WHERE FREETEXT (kanji_nanori, @.search2)
> > >
> > > SELECT kanji_kanji, kanji_meaning, kanji_isjouyou
> > > FROM Kanjis INNER JOIN #BasicKanjiIdsByKana ON kanji_id = id
> > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > ASC
> > > END
> > > END
> > > END
> > >
> > >
> > > Okay, up until now, it's not too bad, I have a first IF to check if I
> > > must return a lot of columns (detailed) or only a few (basic). Then, in
> > > each, I have another IF for each search type, to search the correct
> > > field.
> > >
> > > I'd like to know on a performance point of view what is best? Continue
> > > that way and add an IF for each new type (of course, in both detailed
> > > and basic), or use dynamic SQL?
> > >
> > > Also, if the answer is the IF ELSE structure, what would be a good way
> > > to implement multiple search types (for example, by english meaning AND
> > > strokes number). Because for now, I am limited to one search type per
> > > query.
> > >
> > > Thanks for all your comments and suggestions
> > >
> > >
>|||Hi,
yes, in fact each of my temporary tables strictly contains an id. Each
IF branch (now splitted into sub-procedures) fills that temp table with
the ids related to the search, then do many select queries based on
those ids.
I've change the SELECT INTO and splitted each IF into sub-procedures,
here it is now (with some commented example search at the beginning) :
--EXECUTE [DicKanjiSearch] N'=E5=8F=8B', 1, N'', 0
--EXECUTE [DicKanjiSearch] N'=E5=8B=9D', 1, N'', 1
--EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
--EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
--EXECUTE [DicKanjiSearch] N'=E3=82=B8=E3=83=A5=E3=82=A6', 3, N'=E3=81=98==E3=82=85=E3=81=86', 0
--EXECUTE [DicKanjiSearch] N'=E3=82=B3=E3=82=B3=E3=83=AD', 3, N'=E3=81=93==E3=81=93=E3=82=8D', 1
--EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
--EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
tinyint,
@.search2 nvarchar(200) =3D '', @.detailed bit AS
-- @.type : 1 =3D kanji
-- 2 =3D english
-- 3 =3D hiragana or katakana : pronunciation (on'yomi / kun'yomi
/ nanori)
-- @.search2 is there in case we have to search by pronunciation, we
must
-- be able to search in hiragana and katakana. In that case,
-- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
-- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
-- nanori (name) pronunciation is also in HIRAGANA ans therefore is
@.search2.
BEGIN
SET NOCOUNT ON
-- temp table to hold ids
CREATE TABLE #TempTable (id int PRIMARY KEY)
-- return fields for detailed listing
IF @.detailed =3D 1
BEGIN
IF @.type =3D 1
EXEC DictKanjiGetDetailedByKanji @.search
ELSE IF @.type =3D 2
EXEC DictKanjiGetDetailedByMeaning @.search
ELSE IF @.type =3D 3
EXEC DictKanjiGetDetailedByPron @.search,
@.search2
ELSE IF @.type =3D 4
This will be a new type : by stroke count
EXEC DictKanjiGetDetailedByStrokesCount
CAST(@.search as tinyint)
END
ELSE
-- return fields for basic listing
BEGIN
IF @.type =3D 1
EXEC DictKanjiGetBasicByKanji @.search
ELSE IF @.type =3D 2
EXEC DictKanjiGetBasicByMeaning @.search
ELSE IF @.type =3D 3
EXEC DictKanjiGetBasicByPron @.search, @.search2
ELSE IF @.type =3D 4
This will be a new type : by stroke count
EXEC DictKanjiGetBasicByStrokesCount CAST(@.search
as tinyint)
END
END
Now, after reading your two excellent articles (many thanks for that),
it seems that, I quote, "dynamic SQL is often the best solution, both
for performance and maintainability"...Now I have a concern with the
method I am using, because it's static SQL :| what do you think about
that?
Finally, is there a way to 'merge' queries together with the method I
am currently using? That's because I added the search type 4, which
is by kanji strokes count, I'd like to be able to search for by more
than one search type. (e.g., strokes count AND english term).
I see two ways of passing multiple parameters here. Either
1) have a @.parameter_name for each type possible (e.g.: @.meaning =3D
NULL, @.kanji =3D NULL, @.pronH =3D NULL, @.pronK =3D NULL, @.strokescount =3D
NULL, ...), then check which ones are assigned, then execute
corresponding queries.
2) use my @.search paramater as a container, like "7&name", and have my
@.type parameter something like "strokecount&meaning" (or the more
shorter "sc&en") and parse then to execute corresponding queries.
Which one would perfrom faster?
Then I still need a way to merge SELECT tables together...Any ideas?
Thank you very much!
John Bell wrote:
> Hi
> "ibiza" wrote:
> > Hi, thanks for your reply.
> >
> > By the way, you gave me the two same links, is that a typo?
> Yes it was a cut and paste issue! Try
> http://www.sommarskog.se/dynamic_sql.html
> >
> > > It may be possible to rationalise the use of the different temporary =tables
> > > into one, in which case your second select might possibly be made the= same or
> > > more similar and not produce different result sets for each combinati=on,
> > > which may clean up the client code.
> >
> > How would you do that? I have trouble to get it :S
> Looking at the code you seem to have very similar temporary tables with
> different name. Without looking in depth it struck me that if you just
> created a single table at the begining you may be able to see more
> rationalisation. I would have expected the current client to have similar
> code to the stored procedure as you seem to be producing unique output for
> each one. This could probably be cleaned up if you had the same column na=mes
> for everything. If you took this further and always returned the same col=umns
> then you may be able to clean up the client coding further, the downside =may
> be that you produce a wider result set, you may need to judge the perform=ance
> impact of that.
>
> John
> > thanks for your time
> >
> > John Bell wrote:
> > > Hi
> > >
> > > Check out http://www.sommarskog.se/dyn-search.html and
> > > http://www.sommarskog.se/dyn-search.html.
> > >
> > > It may be possible to rationalise the use of the different temporary =tables
> > > into one, in which case your second select might possibly be made the= same or
> > > more similar and not produce different result sets for each combinati=on,
> > > which may clean up the client code.
> > >
> > > You may also want to look at splitting the different sections into th=eir own
> > > stored procedures.
> > >
> > > John
> > >
> > > "ibiza" wrote:
> > >
> > > > Hi,
> > > >
> > > > I have a stored proc intented to search a table on different fields,
> > > > depending on the search type.
> > > >
> > > > Up until now, I have implemented 3 options, which is by kanji, kana= and
> > > > english. I'll add many others, like strokes count, radical # and so= on.
> > > >
> > > > Here it is, with the 3 options (it may seem overwhelming but is only
> > > > because of the IF ELSE structure) :
> > > >
> > > > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > > tinyint,
> > > > @.search2 nvarchar(200) =3D '', @.detailed bit AS
> > > > -- @.type : 1 =3D kanji
> > > > -- 2 =3D english
> > > > -- 3 =3D hiragana or katakana : pronunciation (on'yomi / ku=n'yomi
> > > > / nanori)
> > > > -- @.search2 is there in case we have to search by pronunciation, we
> > > > must
> > > > -- be able to search in hiragana and katakana. In that case,
> > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > > @.search2.
> > > > BEGIN
> > > > SET NOCOUNT ON
> > > >
> > > > -- return fields for detailed listing
> > > > IF @.detailed =3D 1
> > > > BEGIN
> > > > IF @.type =3D 1
> > > > BEGIN
> > > > -- select kanji ids corresponding to the search string
> > > > SELECT kanji_id as id
> > > > INTO #KanjiIdsByKanji
> > > > FROM dbo.Kanjis
> > > > WHERE kanji_kanji =3D @.search
> > > >
> > > > -- select kanji fields
> > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > kanji_nanori, kanji_meaning,
> > > > lk_filename, lk_idlesson,
> > > > lesson_idlevel, lesson_idlesson,
> > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id =3D id
> > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > ASC
> > > >
> > > > ... [ * comment: I removed some other table select for the sake =of
> > > > the simplicity post ]
> > > > END
> > > > ELSE IF @.type =3D 2
> > > > BEGIN
> > > > -- select kanji ids corresponding to the search string
> > > > SELECT kanji_id as id
> > > > INTO #KanjiIdsByEn
> > > > FROM dbo.Kanjis
> > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > >
> > > > -- select kanji fields
> > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > kanji_nanori, kanji_meaning,
> > > > lk_filename, lk_idlesson,
> > > > lesson_idlevel, lesson_idlesson,
> > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id =3D id
> > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > ASC
> > > >
> > > > ... [ * comment: I removed some other table select for the sake =of
> > > > the simplicity post ]
> > > > END
> > > > ELSE IF @.type =3D 3
> > > > BEGIN
> > > > -- select kanji ids corresponding to the search string
> > > > SELECT kanji_id as id
> > > > INTO #KanjiIdsByKana
> > > > FROM dbo.Kanjis
> > > > WHERE FREETEXT (kanji_on, @.search)
> > > > UNION ALL
> > > > SELECT kanji_id as id
> > > > FROM dbo.Kanjis
> > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > UNION ALL
> > > > SELECT kanji_id as id
> > > > FROM dbo.Kanjis
> > > > WHERE FREETEXT (kanji_nanori, @.search2)
> > > >
> > > > -- select kanji fields
> > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > kanji_nanori, kanji_meaning,
> > > > lk_filename, lk_idlesson,
> > > > lesson_idlevel, lesson_idlesson,
> > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id =3D id
> > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > ASC
> > > >
> > > > ... [ * comment: I removed some other table select for the sake =of
> > > > the simplicity post ]
> > > > END
> > > > END
> > > > ELSE
> > > > -- return fields for basic listing
> > > > BEGIN
> > > > IF @.type =3D 1
> > > > BEGIN
> > > > -- select kanji ids corresponding to the search string
> > > > SELECT kanji_id as id
> > > > INTO #BasicKanjiIdsByKanji
> > > > FROM dbo.Kanjis
> > > > WHERE kanji_kanji =3D @.search
> > > >
> > > > SELECT kanji_kanji, kanji_meaning
> > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id =3D id
> > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > ASC
> > > > END
> > > > ELSE IF @.type =3D 2
> > > > BEGIN
> > > > -- select kanji ids corresponding to the search string
> > > > SELECT kanji_id as id
> > > > INTO #BasicKanjiIdsByEn
> > > > FROM dbo.Kanjis
> > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > >
> > > > SELECT kanji_kanji, kanji_meaning
> > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id =3D id
> > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > ASC
> > > > END
> > > > ELSE IF @.type =3D 3
> > > > BEGIN
> > > > -- select kanji ids corresponding to the search string
> > > > SELECT kanji_id as id
> > > > INTO #BasicKanjiIdsByKana
> > > > FROM dbo.Kanjis
> > > > WHERE FREETEXT (kanji_on, @.search)
> > > > UNION ALL
> > > > SELECT kanji_id as id
> > > > FROM dbo.Kanjis
> > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > UNION ALL
> > > > SELECT kanji_id as id
> > > > FROM dbo.Kanjis
> > > > WHERE FREETEXT (kanji_nanori, @.search2)
> > > >
> > > > SELECT kanji_kanji, kanji_meaning, kanji_isjouyou
> > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByKana ON kanji_id =3D id
> > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > ASC
> > > > END
> > > > END
> > > > END
> > > >
> > > >
> > > > Okay, up until now, it's not too bad, I have a first IF to check if= I
> > > > must return a lot of columns (detailed) or only a few (basic). Then=, in
> > > > each, I have another IF for each search type, to search the correct
> > > > field.
> > > >
> > > > I'd like to know on a performance point of view what is best? Conti=nue
> > > > that way and add an IF for each new type (of course, in both detail=ed
> > > > and basic), or use dynamic SQL?
> > > >
> > > > Also, if the answer is the IF ELSE structure, what would be a good =way
> > > > to implement multiple search types (for example, by english meaning= AND
> > > > strokes number). Because for now, I am limited to one search type p=er
> > > > query.
> > > >
> > > > Thanks for all your comments and suggestions
> > > > > > > >
> > > >|||Hi
The only way to see if dynamic SQL is quicker would be to implement it. In
general I would only do that if you think your current solution is too slow.
To implement multiple criteria you could separate your procedures into two
bring the second select into DicKanjiSearch, you can then call the various
procedures to add new ids to the temporary table multiple times.
John
"ibiza" wrote:
> Hi,
> yes, in fact each of my temporary tables strictly contains an id. Each
> IF branch (now splitted into sub-procedures) fills that temp table with
> the ids related to the search, then do many select queries based on
> those ids.
> I've change the SELECT INTO and splitted each IF into sub-procedures,
> here it is now (with some commented example search at the beginning) :
> --EXECUTE [DicKanjiSearch] N'å'', 1, N'', 0
> --EXECUTE [DicKanjiSearch] N'å'', 1, N'', 1
> --EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> --EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
> --EXECUTE [DicKanjiSearch] N'ã?¸ã?¥ã?¦', 3, N'ã'ã'ã'', 0
> --EXECUTE [DicKanjiSearch] N'ã?³ã?³ã?', 3, N'ã'ã'ã'', 1
> --EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
> --EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
> ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> tinyint,
> @.search2 nvarchar(200) = '', @.detailed bit AS
> -- @.type : 1 = kanji
> -- 2 = english
> -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> / nanori)
> -- @.search2 is there in case we have to search by pronunciation, we
> must
> -- be able to search in hiragana and katakana. In that case,
> -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> @.search2.
> BEGIN
> SET NOCOUNT ON
> -- temp table to hold ids
> CREATE TABLE #TempTable (id int PRIMARY KEY)
> -- return fields for detailed listing
> IF @.detailed = 1
> BEGIN
> IF @.type = 1
> EXEC DictKanjiGetDetailedByKanji @.search
> ELSE IF @.type = 2
> EXEC DictKanjiGetDetailedByMeaning @.search
> ELSE IF @.type = 3
> EXEC DictKanjiGetDetailedByPron @.search,
> @.search2
> ELSE IF @.type = 4
> This will be a new type : by stroke count
> EXEC DictKanjiGetDetailedByStrokesCount
> CAST(@.search as tinyint)
> END
> ELSE
> -- return fields for basic listing
> BEGIN
> IF @.type = 1
> EXEC DictKanjiGetBasicByKanji @.search
> ELSE IF @.type = 2
> EXEC DictKanjiGetBasicByMeaning @.search
> ELSE IF @.type = 3
> EXEC DictKanjiGetBasicByPron @.search, @.search2
> ELSE IF @.type = 4
> This will be a new type : by stroke count
> EXEC DictKanjiGetBasicByStrokesCount CAST(@.search
> as tinyint)
> END
> END
> Now, after reading your two excellent articles (many thanks for that),
> it seems that, I quote, "dynamic SQL is often the best solution, both
> for performance and maintainability"...Now I have a concern with the
> method I am using, because it's static SQL :| what do you think about
> that?
> Finally, is there a way to 'merge' queries together with the method I
> am currently using? That's because I added the search type 4, which
> is by kanji strokes count, I'd like to be able to search for by more
> than one search type. (e.g., strokes count AND english term).
> I see two ways of passing multiple parameters here. Either
> 1) have a @.parameter_name for each type possible (e.g.: @.meaning => NULL, @.kanji = NULL, @.pronH = NULL, @.pronK = NULL, @.strokescount => NULL, ...), then check which ones are assigned, then execute
> corresponding queries.
> 2) use my @.search paramater as a container, like "7&name", and have my
> @.type parameter something like "strokecount&meaning" (or the more
> shorter "sc&en") and parse then to execute corresponding queries.
> Which one would perfrom faster?
> Then I still need a way to merge SELECT tables together...Any ideas?
> Thank you very much!
> John Bell wrote:
> > Hi
> >
> > "ibiza" wrote:
> >
> > > Hi, thanks for your reply.
> > >
> > > By the way, you gave me the two same links, is that a typo?
> > Yes it was a cut and paste issue! Try
> > http://www.sommarskog.se/dynamic_sql.html
> >
> > >
> > > > It may be possible to rationalise the use of the different temporary tables
> > > > into one, in which case your second select might possibly be made the same or
> > > > more similar and not produce different result sets for each combination,
> > > > which may clean up the client code.
> > >
> > > How would you do that? I have trouble to get it :S
> > Looking at the code you seem to have very similar temporary tables with
> > different name. Without looking in depth it struck me that if you just
> > created a single table at the begining you may be able to see more
> > rationalisation. I would have expected the current client to have similar
> > code to the stored procedure as you seem to be producing unique output for
> > each one. This could probably be cleaned up if you had the same column names
> > for everything. If you took this further and always returned the same columns
> > then you may be able to clean up the client coding further, the downside may
> > be that you produce a wider result set, you may need to judge the performance
> > impact of that.
> >
> >
> > John
> > > thanks for your time
> > >
> > > John Bell wrote:
> > > > Hi
> > > >
> > > > Check out http://www.sommarskog.se/dyn-search.html and
> > > > http://www.sommarskog.se/dyn-search.html.
> > > >
> > > > It may be possible to rationalise the use of the different temporary tables
> > > > into one, in which case your second select might possibly be made the same or
> > > > more similar and not produce different result sets for each combination,
> > > > which may clean up the client code.
> > > >
> > > > You may also want to look at splitting the different sections into their own
> > > > stored procedures.
> > > >
> > > > John
> > > >
> > > > "ibiza" wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > I have a stored proc intented to search a table on different fields,
> > > > > depending on the search type.
> > > > >
> > > > > Up until now, I have implemented 3 options, which is by kanji, kana and
> > > > > english. I'll add many others, like strokes count, radical # and so on.
> > > > >
> > > > > Here it is, with the 3 options (it may seem overwhelming but is only
> > > > > because of the IF ELSE structure) :
> > > > >
> > > > > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > > > tinyint,
> > > > > @.search2 nvarchar(200) = '', @.detailed bit AS
> > > > > -- @.type : 1 = kanji
> > > > > -- 2 = english
> > > > > -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> > > > > / nanori)
> > > > > -- @.search2 is there in case we have to search by pronunciation, we
> > > > > must
> > > > > -- be able to search in hiragana and katakana. In that case,
> > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > > > @.search2.
> > > > > BEGIN
> > > > > SET NOCOUNT ON
> > > > >
> > > > > -- return fields for detailed listing
> > > > > IF @.detailed = 1
> > > > > BEGIN
> > > > > IF @.type = 1
> > > > > BEGIN
> > > > > -- select kanji ids corresponding to the search string
> > > > > SELECT kanji_id as id
> > > > > INTO #KanjiIdsByKanji
> > > > > FROM dbo.Kanjis
> > > > > WHERE kanji_kanji = @.search
> > > > >
> > > > > -- select kanji fields
> > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > kanji_nanori, kanji_meaning,
> > > > > lk_filename, lk_idlesson,
> > > > > lesson_idlevel, lesson_idlesson,
> > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > ASC
> > > > >
> > > > > ... [ * comment: I removed some other table select for the sake of
> > > > > the simplicity post ]
> > > > > END
> > > > > ELSE IF @.type = 2
> > > > > BEGIN
> > > > > -- select kanji ids corresponding to the search string
> > > > > SELECT kanji_id as id
> > > > > INTO #KanjiIdsByEn
> > > > > FROM dbo.Kanjis
> > > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > > >
> > > > > -- select kanji fields
> > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > kanji_nanori, kanji_meaning,
> > > > > lk_filename, lk_idlesson,
> > > > > lesson_idlevel, lesson_idlesson,
> > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
> > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > ASC
> > > > >
> > > > > ... [ * comment: I removed some other table select for the sake of
> > > > > the simplicity post ]
> > > > > END
> > > > > ELSE IF @.type = 3
> > > > > BEGIN
> > > > > -- select kanji ids corresponding to the search string
> > > > > SELECT kanji_id as id
> > > > > INTO #KanjiIdsByKana
> > > > > FROM dbo.Kanjis
> > > > > WHERE FREETEXT (kanji_on, @.search)
> > > > > UNION ALL
> > > > > SELECT kanji_id as id
> > > > > FROM dbo.Kanjis
> > > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > > UNION ALL
> > > > > SELECT kanji_id as id
> > > > > FROM dbo.Kanjis
> > > > > WHERE FREETEXT (kanji_nanori, @.search2)
> > > > >
> > > > > -- select kanji fields
> > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > kanji_nanori, kanji_meaning,
> > > > > lk_filename, lk_idlesson,
> > > > > lesson_idlevel, lesson_idlesson,
> > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > > FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id = id
> > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > ASC
> > > > >
> > > > > ... [ * comment: I removed some other table select for the sake of
> > > > > the simplicity post ]
> > > > > END
> > > > > END
> > > > > ELSE
> > > > > -- return fields for basic listing
> > > > > BEGIN
> > > > > IF @.type = 1
> > > > > BEGIN
> > > > > -- select kanji ids corresponding to the search string
> > > > > SELECT kanji_id as id
> > > > > INTO #BasicKanjiIdsByKanji
> > > > > FROM dbo.Kanjis
> > > > > WHERE kanji_kanji = @.search
> > > > >
> > > > > SELECT kanji_kanji, kanji_meaning
> > > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
> > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > ASC
> > > > > END
> > > > > ELSE IF @.type = 2
> > > > > BEGIN
> > > > > -- select kanji ids corresponding to the search string
> > > > > SELECT kanji_id as id
> > > > > INTO #BasicKanjiIdsByEn
> > > > > FROM dbo.Kanjis
> > > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > > >
> > > > > SELECT kanji_kanji, kanji_meaning
> > > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id = id
> > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > ASC
> > > > > END
> > > > > ELSE IF @.type = 3
> > > > > BEGIN
> > > > > -- select kanji ids corresponding to the search string
> > > > > SELECT kanji_id as id
> > > > > INTO #BasicKanjiIdsByKana
> > > > > FROM dbo.Kanjis
> > > > > WHERE FREETEXT (kanji_on, @.search)
> > > > > UNION ALL
> > > > > SELECT kanji_id as id
> > > > > FROM dbo.Kanjis
> > > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > > UNION ALL|||Hello,
> To implement multiple criteria you could separate your procedures into two
> bring the second select into DicKanjiSearch, you can then call the various
> procedures to add new ids to the temporary table multiple times.
I don't really understand. Separating which procedures into two?
Also, multiple criteria would mean a sort of AND implemantation, so
that does not mean "add new ids to the temporary table multiple times"
(this would be an OR, am I correct?)
thanks for your time
John Bell wrote:
> Hi
> The only way to see if dynamic SQL is quicker would be to implement it. In
> general I would only do that if you think your current solution is too sl=ow.
> To implement multiple criteria you could separate your procedures into two
> bring the second select into DicKanjiSearch, you can then call the various
> procedures to add new ids to the temporary table multiple times.
> John
> "ibiza" wrote:
> > Hi,
> >
> > yes, in fact each of my temporary tables strictly contains an id. Each
> > IF branch (now splitted into sub-procedures) fills that temp table with
> > the ids related to the search, then do many select queries based on
> > those ids.
> >
> > I've change the SELECT INTO and splitted each IF into sub-procedures,
> > here it is now (with some commented example search at the beginning) :
> >
> > --EXECUTE [DicKanjiSearch] N'=E5=8F=8B', 1, N'', 0
> > --EXECUTE [DicKanjiSearch] N'=E5=8B=9D', 1, N'', 1
> > --EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> > --EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
> > --EXECUTE [DicKanjiSearch] N'=E3=82=B8=E3=83=A5=E3=82=A6', 3, N'=E3=81==98=E3=82=85=E3=81=86', 0
> > --EXECUTE [DicKanjiSearch] N'=E3=82=B3=E3=82=B3=E3=83=AD', 3, N'=E3=81==93=E3=81=93=E3=82=8D', 1
> > --EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
> > --EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
> > ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > tinyint,
> >
> > @.search2 nvarchar(200) =3D '', @.detailed bit AS
> > -- @.type : 1 =3D kanji
> > -- 2 =3D english
> > -- 3 =3D hiragana or katakana : pronunciation (on'yomi / kun'yo=mi
> > / nanori)
> > -- @.search2 is there in case we have to search by pronunciation, we
> > must
> > -- be able to search in hiragana and katakana. In that case,
> > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > @.search2.
> > BEGIN
> > SET NOCOUNT ON
> >
> > -- temp table to hold ids
> > CREATE TABLE #TempTable (id int PRIMARY KEY)
> >
> > -- return fields for detailed listing
> > IF @.detailed =3D 1
> > BEGIN
> > IF @.type =3D 1
> > EXEC DictKanjiGetDetailedByKanji @.search
> > ELSE IF @.type =3D 2
> > EXEC DictKanjiGetDetailedByMeaning @.search
> > ELSE IF @.type =3D 3
> > EXEC DictKanjiGetDetailedByPron @.search,
> > @.search2
> > ELSE IF @.type =3D 4
> > This will be a new type : by stroke count
> > EXEC DictKanjiGetDetailedByStrokesCount
> > CAST(@.search as tinyint)
> > END
> > ELSE
> > -- return fields for basic listing
> > BEGIN
> > IF @.type =3D 1
> > EXEC DictKanjiGetBasicByKanji @.search
> > ELSE IF @.type =3D 2
> > EXEC DictKanjiGetBasicByMeaning @.search
> > ELSE IF @.type =3D 3
> > EXEC DictKanjiGetBasicByPron @.search, @.search2
> > ELSE IF @.type =3D 4
> > This will be a new type : by stroke count
> > EXEC DictKanjiGetBasicByStrokesCount CAST(@.search
> > as tinyint)
> > END
> > END
> >
> > Now, after reading your two excellent articles (many thanks for that),
> > it seems that, I quote, "dynamic SQL is often the best solution, both
> > for performance and maintainability"...Now I have a concern with the
> > method I am using, because it's static SQL :| what do you think about
> > that?
> >
> > Finally, is there a way to 'merge' queries together with the method I
> > am currently using? That's because I added the search type 4, which
> > is by kanji strokes count, I'd like to be able to search for by more
> > than one search type. (e.g., strokes count AND english term).
> > I see two ways of passing multiple parameters here. Either
> > 1) have a @.parameter_name for each type possible (e.g.: @.meaning =3D
> > NULL, @.kanji =3D NULL, @.pronH =3D NULL, @.pronK =3D NULL, @.strokescount ==3D
> > NULL, ...), then check which ones are assigned, then execute
> > corresponding queries.
> > 2) use my @.search paramater as a container, like "7&name", and have my
> > @.type parameter something like "strokecount&meaning" (or the more
> > shorter "sc&en") and parse then to execute corresponding queries.
> > Which one would perfrom faster?
> >
> > Then I still need a way to merge SELECT tables together...Any ideas?
> >
> > Thank you very much!
> >
> > John Bell wrote:
> > > Hi
> > >
> > > "ibiza" wrote:
> > >
> > > > Hi, thanks for your reply.
> > > >
> > > > By the way, you gave me the two same links, is that a typo?
> > > Yes it was a cut and paste issue! Try
> > > http://www.sommarskog.se/dynamic_sql.html
> > >
> > > >
> > > > > It may be possible to rationalise the use of the different tempor=ary tables
> > > > > into one, in which case your second select might possibly be made= the same or
> > > > > more similar and not produce different result sets for each combi=nation,
> > > > > which may clean up the client code.
> > > >
> > > > How would you do that? I have trouble to get it :S
> > > Looking at the code you seem to have very similar temporary tables wi=th
> > > different name. Without looking in depth it struck me that if you just
> > > created a single table at the begining you may be able to see more
> > > rationalisation. I would have expected the current client to have sim=ilar
> > > code to the stored procedure as you seem to be producing unique outpu=t for
> > > each one. This could probably be cleaned up if you had the same colum=n names
> > > for everything. If you took this further and always returned the same= columns
> > > then you may be able to clean up the client coding further, the downs=ide may
> > > be that you produce a wider result set, you may need to judge the per=formance
> > > impact of that.
> > >
> > >
> > > John
> > > > thanks for your time
> > > >
> > > > John Bell wrote:
> > > > > Hi
> > > > >
> > > > > Check out http://www.sommarskog.se/dyn-search.html and
> > > > > http://www.sommarskog.se/dyn-search.html.
> > > > >
> > > > > It may be possible to rationalise the use of the different tempor=ary tables
> > > > > into one, in which case your second select might possibly be made= the same or
> > > > > more similar and not produce different result sets for each combi=nation,
> > > > > which may clean up the client code.
> > > > >
> > > > > You may also want to look at splitting the different sections int=o their own
> > > > > stored procedures.
> > > > >
> > > > > John
> > > > >
> > > > > "ibiza" wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > I have a stored proc intented to search a table on different fi=elds,
> > > > > > depending on the search type.
> > > > > >
> > > > > > Up until now, I have implemented 3 options, which is by kanji, =kana and
> > > > > > english. I'll add many others, like strokes count, radical # an=d so on.
> > > > > >
> > > > > > Here it is, with the 3 options (it may seem overwhelming but is= only
> > > > > > because of the IF ELSE structure) :
> > > > > >
> > > > > > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.t=ype
> > > > > > tinyint,
> > > > > > @.search2 nvarchar(200) =3D '', @.detailed bit AS
> > > > > > -- @.type : 1 =3D kanji
> > > > > > -- 2 =3D english
> > > > > > -- 3 =3D hiragana or katakana : pronunciation (on'yomi =/ kun'yomi
> > > > > > / nanori)
> > > > > > -- @.search2 is there in case we have to search by pronunciation=, we
> > > > > > must
> > > > > > -- be able to search in hiragana and katakana. In that case,
> > > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.searc=h=2E
> > > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.sea=rch2.
> > > > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefor=e is
> > > > > > @.search2.
> > > > > > BEGIN
> > > > > > SET NOCOUNT ON
> > > > > >
> > > > > > -- return fields for detailed listing
> > > > > > IF @.detailed =3D 1
> > > > > > BEGIN
> > > > > > IF @.type =3D 1
> > > > > > BEGIN
> > > > > > -- select kanji ids corresponding to the search string
> > > > > > SELECT kanji_id as id
> > > > > > INTO #KanjiIdsByKanji
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE kanji_kanji =3D @.search
> > > > > >
> > > > > > -- select kanji fields
> > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_k=un,
> > > > > > kanji_nanori, kanji_meaning,
> > > > > > lk_filename, lk_idlesson,
> > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_fr=eq
> > > > > > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id =3D id
> > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning),= 201)
> > > > > > ASC
> > > > > >
> > > > > > ... [ * comment: I removed some other table select for the s=ake of
> > > > > > the simplicity post ]
> > > > > > END
> > > > > > ELSE IF @.type =3D 2
> > > > > > BEGIN
> > > > > > -- select kanji ids corresponding to the search string
> > > > > > SELECT kanji_id as id
> > > > > > INTO #KanjiIdsByEn
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > > > >
> > > > > > -- select kanji fields
> > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_k=un,
> > > > > > kanji_nanori, kanji_meaning,
> > > > > > lk_filename, lk_idlesson,
> > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_fr=eq
> > > > > > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id =3D id
> > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning),= 201)
> > > > > > ASC
> > > > > >
> > > > > > ... [ * comment: I removed some other table select for the s=ake of
> > > > > > the simplicity post ]
> > > > > > END
> > > > > > ELSE IF @.type =3D 3
> > > > > > BEGIN
> > > > > > -- select kanji ids corresponding to the search string
> > > > > > SELECT kanji_id as id
> > > > > > INTO #KanjiIdsByKana
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE FREETEXT (kanji_on, @.search)
> > > > > > UNION ALL
> > > > > > SELECT kanji_id as id
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > > > UNION ALL
> > > > > > SELECT kanji_id as id
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE FREETEXT (kanji_nanori, @.search2)
> > > > > >
> > > > > > -- select kanji fields
> > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_k=un,
> > > > > > kanji_nanori, kanji_meaning,
> > > > > > lk_filename, lk_idlesson,
> > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_fr=eq
> > > > > > FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id =3D id
> > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning),= 201)
> > > > > > ASC
> > > > > >
> > > > > > ... [ * comment: I removed some other table select for the s=ake of
> > > > > > the simplicity post ]
> > > > > > END
> > > > > > END
> > > > > > ELSE
> > > > > > -- return fields for basic listing
> > > > > > BEGIN
> > > > > > IF @.type =3D 1
> > > > > > BEGIN
> > > > > > -- select kanji ids corresponding to the search string
> > > > > > SELECT kanji_id as id
> > > > > > INTO #BasicKanjiIdsByKanji
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE kanji_kanji =3D @.search
> > > > > >
> > > > > > SELECT kanji_kanji, kanji_meaning
> > > > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id =3D= id
> > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning),= 201)
> > > > > > ASC
> > > > > > END
> > > > > > ELSE IF @.type =3D 2
> > > > > > BEGIN
> > > > > > -- select kanji ids corresponding to the search string
> > > > > > SELECT kanji_id as id
> > > > > > INTO #BasicKanjiIdsByEn
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > > > >
> > > > > > SELECT kanji_kanji, kanji_meaning
> > > > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByEn ON kanji_id =3D id
> > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning),= 201)
> > > > > > ASC
> > > > > > END
> > > > > > ELSE IF @.type =3D 3
> > > > > > BEGIN
> > > > > > -- select kanji ids corresponding to the search string
> > > > > > SELECT kanji_id as id
> > > > > > INTO #BasicKanjiIdsByKana
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE FREETEXT (kanji_on, @.search)
> > > > > > UNION ALL
> > > > > > SELECT kanji_id as id
> > > > > > FROM dbo.Kanjis
> > > > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > > > UNION ALL|||Hi
The second select statement seems to be in two flavours based on Kanji and
LessonKanji! If these are always exclusive then you may want to separate
these into two different procedures.
To get an AND condition you can select the ids where there is more than one
occurence using a group by id and a having clause, providing each time you
add the id you only insert distinct ones.
John
"ibiza" wrote:
> Hello,
> > To implement multiple criteria you could separate your procedures into two
> > bring the second select into DicKanjiSearch, you can then call the various
> > procedures to add new ids to the temporary table multiple times.
> I don't really understand. Separating which procedures into two?
> Also, multiple criteria would mean a sort of AND implemantation, so
> that does not mean "add new ids to the temporary table multiple times"
> (this would be an OR, am I correct?)
> thanks for your time
> John Bell wrote:
> > Hi
> >
> > The only way to see if dynamic SQL is quicker would be to implement it. In
> > general I would only do that if you think your current solution is too slow.
> >
> > To implement multiple criteria you could separate your procedures into two
> > bring the second select into DicKanjiSearch, you can then call the various
> > procedures to add new ids to the temporary table multiple times.
> >
> > John
> >
> > "ibiza" wrote:
> >
> > > Hi,
> > >
> > > yes, in fact each of my temporary tables strictly contains an id. Each
> > > IF branch (now splitted into sub-procedures) fills that temp table with
> > > the ids related to the search, then do many select queries based on
> > > those ids.
> > >
> > > I've change the SELECT INTO and splitted each IF into sub-procedures,
> > > here it is now (with some commented example search at the beginning) :
> > >
> > > --EXECUTE [DicKanjiSearch] N'å'', 1, N'', 0
> > > --EXECUTE [DicKanjiSearch] N'å'', 1, N'', 1
> > > --EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> > > --EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
> > > --EXECUTE [DicKanjiSearch] N'ã?¸ã?¥ã?¦', 3, N'ã'ã'ã'', 0
> > > --EXECUTE [DicKanjiSearch] N'ã?³ã?³ã?', 3, N'ã'ã'ã'', 1
> > > --EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
> > > --EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
> > > ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > tinyint,
> > >
> > > @.search2 nvarchar(200) = '', @.detailed bit AS
> > > -- @.type : 1 = kanji
> > > -- 2 = english
> > > -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> > > / nanori)
> > > -- @.search2 is there in case we have to search by pronunciation, we
> > > must
> > > -- be able to search in hiragana and katakana. In that case,
> > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > @.search2.
> > > BEGIN
> > > SET NOCOUNT ON
> > >
> > > -- temp table to hold ids
> > > CREATE TABLE #TempTable (id int PRIMARY KEY)
> > >
> > > -- return fields for detailed listing
> > > IF @.detailed = 1
> > > BEGIN
> > > IF @.type = 1
> > > EXEC DictKanjiGetDetailedByKanji @.search
> > > ELSE IF @.type = 2
> > > EXEC DictKanjiGetDetailedByMeaning @.search
> > > ELSE IF @.type = 3
> > > EXEC DictKanjiGetDetailedByPron @.search,
> > > @.search2
> > > ELSE IF @.type = 4
> > > This will be a new type : by stroke count
> > > EXEC DictKanjiGetDetailedByStrokesCount
> > > CAST(@.search as tinyint)
> > > END
> > > ELSE
> > > -- return fields for basic listing
> > > BEGIN
> > > IF @.type = 1
> > > EXEC DictKanjiGetBasicByKanji @.search
> > > ELSE IF @.type = 2
> > > EXEC DictKanjiGetBasicByMeaning @.search
> > > ELSE IF @.type = 3
> > > EXEC DictKanjiGetBasicByPron @.search, @.search2
> > > ELSE IF @.type = 4
> > > This will be a new type : by stroke count
> > > EXEC DictKanjiGetBasicByStrokesCount CAST(@.search
> > > as tinyint)
> > > END
> > > END
> > >
> > > Now, after reading your two excellent articles (many thanks for that),
> > > it seems that, I quote, "dynamic SQL is often the best solution, both
> > > for performance and maintainability"...Now I have a concern with the
> > > method I am using, because it's static SQL :| what do you think about
> > > that?
> > >
> > > Finally, is there a way to 'merge' queries together with the method I
> > > am currently using? That's because I added the search type 4, which
> > > is by kanji strokes count, I'd like to be able to search for by more
> > > than one search type. (e.g., strokes count AND english term).
> > > I see two ways of passing multiple parameters here. Either
> > > 1) have a @.parameter_name for each type possible (e.g.: @.meaning => > > NULL, @.kanji = NULL, @.pronH = NULL, @.pronK = NULL, @.strokescount => > > NULL, ...), then check which ones are assigned, then execute
> > > corresponding queries.
> > > 2) use my @.search paramater as a container, like "7&name", and have my
> > > @.type parameter something like "strokecount&meaning" (or the more
> > > shorter "sc&en") and parse then to execute corresponding queries.
> > > Which one would perfrom faster?
> > >
> > > Then I still need a way to merge SELECT tables together...Any ideas?
> > >
> > > Thank you very much!
> > >
> > > John Bell wrote:
> > > > Hi
> > > >
> > > > "ibiza" wrote:
> > > >
> > > > > Hi, thanks for your reply.
> > > > >
> > > > > By the way, you gave me the two same links, is that a typo?
> > > > Yes it was a cut and paste issue! Try
> > > > http://www.sommarskog.se/dynamic_sql.html
> > > >
> > > > >
> > > > > > It may be possible to rationalise the use of the different temporary tables
> > > > > > into one, in which case your second select might possibly be made the same or
> > > > > > more similar and not produce different result sets for each combination,
> > > > > > which may clean up the client code.
> > > > >
> > > > > How would you do that? I have trouble to get it :S
> > > > Looking at the code you seem to have very similar temporary tables with
> > > > different name. Without looking in depth it struck me that if you just
> > > > created a single table at the begining you may be able to see more
> > > > rationalisation. I would have expected the current client to have similar
> > > > code to the stored procedure as you seem to be producing unique output for
> > > > each one. This could probably be cleaned up if you had the same column names
> > > > for everything. If you took this further and always returned the same columns
> > > > then you may be able to clean up the client coding further, the downside may
> > > > be that you produce a wider result set, you may need to judge the performance
> > > > impact of that.
> > > >
> > > >
> > > > John
> > > > > thanks for your time
> > > > >
> > > > > John Bell wrote:
> > > > > > Hi
> > > > > >
> > > > > > Check out http://www.sommarskog.se/dyn-search.html and
> > > > > > http://www.sommarskog.se/dyn-search.html.
> > > > > >
> > > > > > It may be possible to rationalise the use of the different temporary tables
> > > > > > into one, in which case your second select might possibly be made the same or
> > > > > > more similar and not produce different result sets for each combination,
> > > > > > which may clean up the client code.
> > > > > >
> > > > > > You may also want to look at splitting the different sections into their own
> > > > > > stored procedures.
> > > > > >
> > > > > > John
> > > > > >
> > > > > > "ibiza" wrote:
> > > > > >
> > > > > > > Hi,
> > > > > > >
> > > > > > > I have a stored proc intented to search a table on different fields,
> > > > > > > depending on the search type.
> > > > > > >
> > > > > > > Up until now, I have implemented 3 options, which is by kanji, kana and
> > > > > > > english. I'll add many others, like strokes count, radical # and so on.
> > > > > > >
> > > > > > > Here it is, with the 3 options (it may seem overwhelming but is only
> > > > > > > because of the IF ELSE structure) :
> > > > > > >
> > > > > > > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > > > > > tinyint,
> > > > > > > @.search2 nvarchar(200) = '', @.detailed bit AS
> > > > > > > -- @.type : 1 = kanji
> > > > > > > -- 2 = english
> > > > > > > -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> > > > > > > / nanori)
> > > > > > > -- @.search2 is there in case we have to search by pronunciation, we
> > > > > > > must
> > > > > > > -- be able to search in hiragana and katakana. In that case,
> > > > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > > > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > > > > > @.search2.
> > > > > > > BEGIN
> > > > > > > SET NOCOUNT ON
> > > > > > >
> > > > > > > -- return fields for detailed listing
> > > > > > > IF @.detailed = 1
> > > > > > > BEGIN
> > > > > > > IF @.type = 1
> > > > > > > BEGIN
> > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > SELECT kanji_id as id
> > > > > > > INTO #KanjiIdsByKanji
> > > > > > > FROM dbo.Kanjis
> > > > > > > WHERE kanji_kanji = @.search
> > > > > > >
> > > > > > > -- select kanji fields
> > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > lk_filename, lk_idlesson,
> > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > > > > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > > > ASC
> > > > > > >
> > > > > > > ... [ * comment: I removed some other table select for the sake of
> > > > > > > the simplicity post ]
> > > > > > > END
> > > > > > > ELSE IF @.type = 2
> > > > > > > BEGIN
> > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > SELECT kanji_id as id
> > > > > > > INTO #KanjiIdsByEn
> > > > > > > FROM dbo.Kanjis
> > > > > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > > > > >
> > > > > > > -- select kanji fields
> > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > lk_filename, lk_idlesson,
> > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > > > > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
> > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > > > ASC
> > > > > > >
> > > > > > > ... [ * comment: I removed some other table select for the sake of
> > > > > > > the simplicity post ]
> > > > > > > END
> > > > > > > ELSE IF @.type = 3
> > > > > > > BEGIN
> > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > SELECT kanji_id as id
> > > > > > > INTO #KanjiIdsByKana
> > > > > > > FROM dbo.Kanjis
> > > > > > > WHERE FREETEXT (kanji_on, @.search)
> > > > > > > UNION ALL
> > > > > > > SELECT kanji_id as id
> > > > > > > FROM dbo.Kanjis
> > > > > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > > > > UNION ALL
> > > > > > > SELECT kanji_id as id
> > > > > > > FROM dbo.Kanjis
> > > > > > > WHERE FREETEXT (kanji_nanori, @.search2)
> > > > > > >
> > > > > > > -- select kanji fields
> > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > lk_filename, lk_idlesson,
> > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > > > > FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id = id
> > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > > > ASC
> > > > > > >
> > > > > > > ... [ * comment: I removed some other table select for the sake of
> > > > > > > the simplicity post ]
> > > > > > > END
> > > > > > > END
> > > > > > > ELSE
> > > > > > > -- return fields for basic listing
> > > > > > > BEGIN
> > > > > > > IF @.type = 1
> > > > > > > BEGIN
> > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > SELECT kanji_id as id
> > > > > > > INTO #BasicKanjiIdsByKanji
> > > > > > > FROM dbo.Kanjis
> > > > > > > WHERE kanji_kanji = @.search
> > > > > > >
> > > > > > > SELECT kanji_kanji, kanji_meaning
> > > > > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
> > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > > > ASC|||:S sorry I'd need examples to understand better what you mean.
I suppose 'the second select statement' you refer to are any select
after the
SELECT kanji_id as id
INTO #TempTable
FROM dbo.Kanjis
WHERE (condition)
into each sub-procedure? How come you say that it seems to be in two
flavours?
thanks,
Bruno
John Bell wrote:
> Hi
> The second select statement seems to be in two flavours based on Kanji and
> LessonKanji! If these are always exclusive then you may want to separate
> these into two different procedures.
> To get an AND condition you can select the ids where there is more than o=ne
> occurence using a group by id and a having clause, providing each time y=ou
> add the id you only insert distinct ones.
> John
> "ibiza" wrote:
> > Hello,
> >
> > > To implement multiple criteria you could separate your procedures int=o two
> > > bring the second select into DicKanjiSearch, you can then call the va=rious
> > > procedures to add new ids to the temporary table multiple times.
> >
> > I don't really understand. Separating which procedures into two?
> > Also, multiple criteria would mean a sort of AND implemantation, so
> > that does not mean "add new ids to the temporary table multiple times"
> > (this would be an OR, am I correct?)
> >
> > thanks for your time
> >
> > John Bell wrote:
> > > Hi
> > >
> > > The only way to see if dynamic SQL is quicker would be to implement i=t=2E In
> > > general I would only do that if you think your current solution is to=o slow.
> > >
> > > To implement multiple criteria you could separate your procedures int=o two
> > > bring the second select into DicKanjiSearch, you can then call the va=rious
> > > procedures to add new ids to the temporary table multiple times.
> > >
> > > John
> > >
> > > "ibiza" wrote:
> > >
> > > > Hi,
> > > >
> > > > yes, in fact each of my temporary tables strictly contains an id. E=ach
> > > > IF branch (now splitted into sub-procedures) fills that temp table =with
> > > > the ids related to the search, then do many select queries based on
> > > > those ids.
> > > >
> > > > I've change the SELECT INTO and splitted each IF into sub-procedure=s,
> > > > here it is now (with some commented example search at the beginning=) :
> > > >
> > > > --EXECUTE [DicKanjiSearch] N'=E5=8F=8B', 1, N'', 0
> > > > --EXECUTE [DicKanjiSearch] N'=E5=8B=9D', 1, N'', 1
> > > > --EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> > > > --EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
> > > > --EXECUTE [DicKanjiSearch] N'=E3=82=B8=E3=83=A5=E3=82=A6', 3, N'=E3==81=98=E3=82=85=E3=81=86', 0
> > > > --EXECUTE [DicKanjiSearch] N'=E3=82=B3=E3=82=B3=E3=83=AD', 3, N'=E3==81=93=E3=81=93=E3=82=8D', 1
> > > > --EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
> > > > --EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
> > > > ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > > tinyint,
> > > >
> > > > @.search2 nvarchar(200) =3D '', @.detailed bit AS
> > > > -- @.type : 1 =3D kanji
> > > > -- 2 =3D english
> > > > -- 3 =3D hiragana or katakana : pronunciation (on'yomi / ku=n'yomi
> > > > / nanori)
> > > > -- @.search2 is there in case we have to search by pronunciation, we
> > > > must
> > > > -- be able to search in hiragana and katakana. In that case,
> > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > > @.search2.
> > > > BEGIN
> > > > SET NOCOUNT ON
> > > >
> > > > -- temp table to hold ids
> > > > CREATE TABLE #TempTable (id int PRIMARY KEY)
> > > >
> > > > -- return fields for detailed listing
> > > > IF @.detailed =3D 1
> > > > BEGIN
> > > > IF @.type =3D 1
> > > > EXEC DictKanjiGetDetailedByKanji @.search
> > > > ELSE IF @.type =3D 2
> > > > EXEC DictKanjiGetDetailedByMeaning @.search
> > > > ELSE IF @.type =3D 3
> > > > EXEC DictKanjiGetDetailedByPron @.search,
> > > > @.search2
> > > > ELSE IF @.type =3D 4
> > > > This will be a new type : by stroke count
> > > > EXEC DictKanjiGetDetailedByStrokesCount
> > > > CAST(@.search as tinyint)
> > > > END
> > > > ELSE
> > > > -- return fields for basic listing
> > > > BEGIN
> > > > IF @.type =3D 1
> > > > EXEC DictKanjiGetBasicByKanji @.search
> > > > ELSE IF @.type =3D 2
> > > > EXEC DictKanjiGetBasicByMeaning @.search
> > > > ELSE IF @.type =3D 3
> > > > EXEC DictKanjiGetBasicByPron @.search, @.sear=ch2
> > > > ELSE IF @.type =3D 4
> > > > This will be a new type : by stroke count
> > > > EXEC DictKanjiGetBasicByStrokesCount CAST(@.se=arch
> > > > as tinyint)
> > > > END
> > > > END
> > > >
> > > > Now, after reading your two excellent articles (many thanks for tha=t),
> > > > it seems that, I quote, "dynamic SQL is often the best solution, bo=th
> > > > for performance and maintainability"...Now I have a concern with the
> > > > method I am using, because it's static SQL :| what do you think abo=ut
> > > > that?
> > > >
> > > > Finally, is there a way to 'merge' queries together with the method= I
> > > > am currently using? That's because I added the search type 4, which
> > > > is by kanji strokes count, I'd like to be able to search for by more
> > > > than one search type. (e.g., strokes count AND english term).
> > > > I see two ways of passing multiple parameters here. Either
> > > > 1) have a @.parameter_name for each type possible (e.g.: @.meaning ==3D
> > > > NULL, @.kanji =3D NULL, @.pronH =3D NULL, @.pronK =3D NULL, @.strokesco=unt =3D
> > > > NULL, ...), then check which ones are assigned, then execute
> > > > corresponding queries.
> > > > 2) use my @.search paramater as a container, like "7&name", and have= my
> > > > @.type parameter something like "strokecount&meaning" (or the more
> > > > shorter "sc&en") and parse then to execute corresponding queries.
> > > > Which one would perfrom faster?
> > > >
> > > > Then I still need a way to merge SELECT tables together...Any ideas?
> > > >
> > > > Thank you very much!
> > > >
> > > > John Bell wrote:
> > > > > Hi
> > > > >
> > > > > "ibiza" wrote:
> > > > >
> > > > > > Hi, thanks for your reply.
> > > > > >
> > > > > > By the way, you gave me the two same links, is that a typo?
> > > > > Yes it was a cut and paste issue! Try
> > > > > http://www.sommarskog.se/dynamic_sql.html
> > > > >
> > > > > >
> > > > > > > It may be possible to rationalise the use of the different te=mporary tables
> > > > > > > into one, in which case your second select might possibly be =made the same or
> > > > > > > more similar and not produce different result sets for each c=ombination,
> > > > > > > which may clean up the client code.
> > > > > >
> > > > > > How would you do that? I have trouble to get it :S
> > > > > Looking at the code you seem to have very similar temporary table=s with
> > > > > different name. Without looking in depth it struck me that if you= just
> > > > > created a single table at the begining you may be able to see mo=re
> > > > > rationalisation. I would have expected the current client to have= similar
> > > > > code to the stored procedure as you seem to be producing unique o=utput for
> > > > > each one. This could probably be cleaned up if you had the same c=olumn names
> > > > > for everything. If you took this further and always returned the =same columns
> > > > > then you may be able to clean up the client coding further, the d=ownside may
> > > > > be that you produce a wider result set, you may need to judge the= performance
> > > > > impact of that.
> > > > >
> > > > >
> > > > > John
> > > > > > thanks for your time
> > > > > >
> > > > > > John Bell wrote:
> > > > > > > Hi
> > > > > > >
> > > > > > > Check out http://www.sommarskog.se/dyn-search.html and
> > > > > > > http://www.sommarskog.se/dyn-search.html.
> > > > > > >
> > > > > > > It may be possible to rationalise the use of the different te=mporary tables
> > > > > > > into one, in which case your second select might possibly be =made the same or
> > > > > > > more similar and not produce different result sets for each c=ombination,
> > > > > > > which may clean up the client code.
> > > > > > >
> > > > > > > You may also want to look at splitting the different sections= into their own
> > > > > > > stored procedures.
> > > > > > >
> > > > > > > John
> > > > > > >
> > > > > > > "ibiza" wrote:
> > > > > > >
> > > > > > > > Hi,
> > > > > > > >
> > > > > > > > I have a stored proc intented to search a table on differen=t fields,
> > > > > > > > depending on the search type.
> > > > > > > >
> > > > > > > > Up until now, I have implemented 3 options, which is by kan=ji, kana and
> > > > > > > > english. I'll add many others, like strokes count, radical =# and so on.
> > > > > > > >
> > > > > > > > Here it is, with the 3 options (it may seem overwhelming bu=t is only
> > > > > > > > because of the IF ELSE structure) :
> > > > > > > >
> > > > > > > > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200)=, @.type
> > > > > > > > tinyint,
> > > > > > > > @.search2 nvarchar(200) =3D '', @.detailed bit AS
> > > > > > > > -- @.type : 1 =3D kanji
> > > > > > > > -- 2 =3D english
> > > > > > > > -- 3 =3D hiragana or katakana : pronunciation (on'y=omi / kun'yomi
> > > > > > > > / nanori)
> > > > > > > > -- @.search2 is there in case we have to search by pronuncia=tion, we
> > > > > > > > must
> > > > > > > > -- be able to search in hiragana and katakana. In that case,
> > > > > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.s=earch.
> > > > > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is =@.search2.
> > > > > > > > -- nanori (name) pronunciation is also in HIRAGANA ans ther=efore is
> > > > > > > > @.search2.
> > > > > > > > BEGIN
> > > > > > > > SET NOCOUNT ON
> > > > > > > >
> > > > > > > > -- return fields for detailed listing
> > > > > > > > IF @.detailed =3D 1
> > > > > > > > BEGIN
> > > > > > > > IF @.type =3D 1
> > > > > > > > BEGIN
> > > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > > SELECT kanji_id as id
> > > > > > > > INTO #KanjiIdsByKanji
> > > > > > > > FROM dbo.Kanjis
> > > > > > > > WHERE kanji_kanji =3D @.search
> > > > > > > >
> > > > > > > > -- select kanji fields
> > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kan=ji_kun,
> > > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > > lk_filename, lk_idlesson,
> > > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanj=i_freq
> > > > > > > > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id =3D =id
> > > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999)= ASC,
> > > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meani=ng), 201)
> > > > > > > > ASC
> > > > > > > >
> > > > > > > > ... [ * comment: I removed some other table select for t=he sake of
> > > > > > > > the simplicity post ]
> > > > > > > > END
> > > > > > > > ELSE IF @.type =3D 2
> > > > > > > > BEGIN
> > > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > > SELECT kanji_id as id
> > > > > > > > INTO #KanjiIdsByEn
> > > > > > > > FROM dbo.Kanjis
> > > > > > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > > > > > >
> > > > > > > > -- select kanji fields
> > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kan=ji_kun,
> > > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > > lk_filename, lk_idlesson,
> > > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanj=i_freq
> > > > > > > > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id =3D id
> > > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999)= ASC,
> > > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meani=ng), 201)
> > > > > > > > ASC
> > > > > > > >
> > > > > > > > ... [ * comment: I removed some other table select for t=he sake of
> > > > > > > > the simplicity post ]
> > > > > > > > END
> > > > > > > > ELSE IF @.type =3D 3
> > > > > > > > BEGIN
> > > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > > SELECT kanji_id as id
> > > > > > > > INTO #KanjiIdsByKana
> > > > > > > > FROM dbo.Kanjis
> > > > > > > > WHERE FREETEXT (kanji_on, @.search)
> > > > > > > > UNION ALL
> > > > > > > > SELECT kanji_id as id
> > > > > > > > FROM dbo.Kanjis
> > > > > > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > > > > > UNION ALL
> > > > > > > > SELECT kanji_id as id
> > > > > > > > FROM dbo.Kanjis
> > > > > > > > WHERE FREETEXT (kanji_nanori, @.search2)
> > > > > > > >
> > > > > > > > -- select kanji fields
> > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kan=ji_kun,
> > > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > > lk_filename, lk_idlesson,
> > > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanj=i_freq
> > > > > > > > FROM #KanjiIdsByKana INNER JOIN Kanjis ON kanji_id =3D id
> > > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> > > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999)= ASC,
> > > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meani=ng), 201)
> > > > > > > > ASC
> > > > > > > >
> > > > > > > > ... [ * comment: I removed some other table select for t=he sake of
> > > > > > > > the simplicity post ]
> > > > > > > > END
> > > > > > > > END
> > > > > > > > ELSE
> > > > > > > > -- return fields for basic listing
> > > > > > > > BEGIN
> > > > > > > > IF @.type =3D 1
> > > > > > > > BEGIN
> > > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > > SELECT kanji_id as id
> > > > > > > > INTO #BasicKanjiIdsByKanji
> > > > > > > > FROM dbo.Kanjis
> > > > > > > > WHERE kanji_kanji =3D @.search
> > > > > > > >
> > > > > > > > SELECT kanji_kanji, kanji_meaning
> > > > > > > > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id= =3D id
> > > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999)= ASC,
> > > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meani=ng), 201)
> > > > > > > > ASC|||Hi
The second select statement seems to be very similar to each other of which
there is the basic information and full information:
SELECT kanji_kanji, kanji_meaning
FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
or
SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
kanji_nanori, kanji_meaning,
lk_filename, lk_idlesson,
lesson_idlevel, lesson_idlesson,
kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
Rather than repeating this in multiple procedures they could be put at a
level where the code would only be written once (helping maintainance). If
you do this then the procedures that populate the temporary tables can just
contain single select statement and you can use INSERT.. EXEC e.g.
INSERT #Tmptable (id)
EXECUTE [DicKanjiSearch] N'å'', 1, N'', 0
As the basic listing is a subset of the full listing, then you may want to
return the full listing result set each time and throw away the columns not
used to try and simplify the client code (although normally you would not
want to return excessively wide result sets!).
To get the 'AND' result you can do something like:
CREATE TABLE #Tmptable (id int)
INSERT #Tmptable (id)
EXECUTE [DicKanjiSearch] N'å'', 1, N'', 0
INSERT #Tmptable (id)
EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
-- And results will be have two entried one from each insert
SELECT id
FROM #Tmptable
GROUP BY id
HAVING COUNT(*) > 1
I hope that is clearer?
John
"ibiza" wrote:
> :S sorry I'd need examples to understand better what you mean.
> I suppose 'the second select statement' you refer to are any select
> after the
> SELECT kanji_id as id
> INTO #TempTable
> FROM dbo.Kanjis
> WHERE (condition)
> into each sub-procedure? How come you say that it seems to be in two
> flavours?
> thanks,
> Bruno
> John Bell wrote:
> > Hi
> >
> > The second select statement seems to be in two flavours based on Kanji and
> > LessonKanji! If these are always exclusive then you may want to separate
> > these into two different procedures.
> >
> > To get an AND condition you can select the ids where there is more than one
> > occurence using a group by id and a having clause, providing each time you
> > add the id you only insert distinct ones.
> >
> > John
> >
> > "ibiza" wrote:
> >
> > > Hello,
> > >
> > > > To implement multiple criteria you could separate your procedures into two
> > > > bring the second select into DicKanjiSearch, you can then call the various
> > > > procedures to add new ids to the temporary table multiple times.
> > >
> > > I don't really understand. Separating which procedures into two?
> > > Also, multiple criteria would mean a sort of AND implemantation, so
> > > that does not mean "add new ids to the temporary table multiple times"
> > > (this would be an OR, am I correct?)
> > >
> > > thanks for your time
> > >
> > > John Bell wrote:
> > > > Hi
> > > >
> > > > The only way to see if dynamic SQL is quicker would be to implement it. In
> > > > general I would only do that if you think your current solution is too slow.
> > > >
> > > > To implement multiple criteria you could separate your procedures into two
> > > > bring the second select into DicKanjiSearch, you can then call the various
> > > > procedures to add new ids to the temporary table multiple times.
> > > >
> > > > John
> > > >
> > > > "ibiza" wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > yes, in fact each of my temporary tables strictly contains an id. Each
> > > > > IF branch (now splitted into sub-procedures) fills that temp table with
> > > > > the ids related to the search, then do many select queries based on
> > > > > those ids.
> > > > >
> > > > > I've change the SELECT INTO and splitted each IF into sub-procedures,
> > > > > here it is now (with some commented example search at the beginning) :
> > > > >
> > > > > --EXECUTE [DicKanjiSearch] N'å'', 1, N'', 0
> > > > > --EXECUTE [DicKanjiSearch] N'å'', 1, N'', 1
> > > > > --EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> > > > > --EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
> > > > > --EXECUTE [DicKanjiSearch] N'ã?¸ã?¥ã?¦', 3, N'ã'ã'ã'', 0
> > > > > --EXECUTE [DicKanjiSearch] N'ã?³ã?³ã?', 3, N'ã'ã'ã'', 1
> > > > > --EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
> > > > > --EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
> > > > > ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > > > tinyint,
> > > > >
> > > > > @.search2 nvarchar(200) = '', @.detailed bit AS
> > > > > -- @.type : 1 = kanji
> > > > > -- 2 = english
> > > > > -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> > > > > / nanori)
> > > > > -- @.search2 is there in case we have to search by pronunciation, we
> > > > > must
> > > > > -- be able to search in hiragana and katakana. In that case,
> > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > > > @.search2.
> > > > > BEGIN
> > > > > SET NOCOUNT ON
> > > > >
> > > > > -- temp table to hold ids
> > > > > CREATE TABLE #TempTable (id int PRIMARY KEY)
> > > > >
> > > > > -- return fields for detailed listing
> > > > > IF @.detailed = 1
> > > > > BEGIN
> > > > > IF @.type = 1
> > > > > EXEC DictKanjiGetDetailedByKanji @.search
> > > > > ELSE IF @.type = 2
> > > > > EXEC DictKanjiGetDetailedByMeaning @.search
> > > > > ELSE IF @.type = 3
> > > > > EXEC DictKanjiGetDetailedByPron @.search,
> > > > > @.search2
> > > > > ELSE IF @.type = 4
> > > > > This will be a new type : by stroke count
> > > > > EXEC DictKanjiGetDetailedByStrokesCount
> > > > > CAST(@.search as tinyint)
> > > > > END
> > > > > ELSE
> > > > > -- return fields for basic listing
> > > > > BEGIN
> > > > > IF @.type = 1
> > > > > EXEC DictKanjiGetBasicByKanji @.search
> > > > > ELSE IF @.type = 2
> > > > > EXEC DictKanjiGetBasicByMeaning @.search
> > > > > ELSE IF @.type = 3
> > > > > EXEC DictKanjiGetBasicByPron @.search, @.search2
> > > > > ELSE IF @.type = 4
> > > > > This will be a new type : by stroke count
> > > > > EXEC DictKanjiGetBasicByStrokesCount CAST(@.search
> > > > > as tinyint)
> > > > > END
> > > > > END
> > > > >
> > > > > Now, after reading your two excellent articles (many thanks for that),
> > > > > it seems that, I quote, "dynamic SQL is often the best solution, both
> > > > > for performance and maintainability"...Now I have a concern with the
> > > > > method I am using, because it's static SQL :| what do you think about
> > > > > that?
> > > > >
> > > > > Finally, is there a way to 'merge' queries together with the method I
> > > > > am currently using? That's because I added the search type 4, which
> > > > > is by kanji strokes count, I'd like to be able to search for by more
> > > > > than one search type. (e.g., strokes count AND english term).
> > > > > I see two ways of passing multiple parameters here. Either
> > > > > 1) have a @.parameter_name for each type possible (e.g.: @.meaning => > > > > NULL, @.kanji = NULL, @.pronH = NULL, @.pronK = NULL, @.strokescount => > > > > NULL, ...), then check which ones are assigned, then execute
> > > > > corresponding queries.
> > > > > 2) use my @.search paramater as a container, like "7&name", and have my
> > > > > @.type parameter something like "strokecount&meaning" (or the more
> > > > > shorter "sc&en") and parse then to execute corresponding queries.
> > > > > Which one would perfrom faster?
> > > > >
> > > > > Then I still need a way to merge SELECT tables together...Any ideas?
> > > > >
> > > > > Thank you very much!
> > > > >
> > > > > John Bell wrote:
> > > > > > Hi
> > > > > >
> > > > > > "ibiza" wrote:
> > > > > >
> > > > > > > Hi, thanks for your reply.
> > > > > > >
> > > > > > > By the way, you gave me the two same links, is that a typo?
> > > > > > Yes it was a cut and paste issue! Try
> > > > > > http://www.sommarskog.se/dynamic_sql.html
> > > > > >
> > > > > > >
> > > > > > > > It may be possible to rationalise the use of the different temporary tables
> > > > > > > > into one, in which case your second select might possibly be made the same or
> > > > > > > > more similar and not produce different result sets for each combination,
> > > > > > > > which may clean up the client code.
> > > > > > >
> > > > > > > How would you do that? I have trouble to get it :S
> > > > > > Looking at the code you seem to have very similar temporary tables with
> > > > > > different name. Without looking in depth it struck me that if you just
> > > > > > created a single table at the begining you may be able to see more
> > > > > > rationalisation. I would have expected the current client to have similar
> > > > > > code to the stored procedure as you seem to be producing unique output for
> > > > > > each one. This could probably be cleaned up if you had the same column names
> > > > > > for everything. If you took this further and always returned the same columns
> > > > > > then you may be able to clean up the client coding further, the downside may
> > > > > > be that you produce a wider result set, you may need to judge the performance
> > > > > > impact of that.
> > > > > >
> > > > > >
> > > > > > John
> > > > > > > thanks for your time
> > > > > > >
> > > > > > > John Bell wrote:
> > > > > > > > Hi
> > > > > > > >
> > > > > > > > Check out http://www.sommarskog.se/dyn-search.html and
> > > > > > > > http://www.sommarskog.se/dyn-search.html.
> > > > > > > >
> > > > > > > > It may be possible to rationalise the use of the different temporary tables
> > > > > > > > into one, in which case your second select might possibly be made the same or
> > > > > > > > more similar and not produce different result sets for each combination,
> > > > > > > > which may clean up the client code.
> > > > > > > >
> > > > > > > > You may also want to look at splitting the different sections into their own
> > > > > > > > stored procedures.
> > > > > > > >
> > > > > > > > John
> > > > > > > >
> > > > > > > > "ibiza" wrote:
> > > > > > > >
> > > > > > > > > Hi,
> > > > > > > > >
> > > > > > > > > I have a stored proc intented to search a table on different fields,
> > > > > > > > > depending on the search type.
> > > > > > > > >
> > > > > > > > > Up until now, I have implemented 3 options, which is by kanji, kana and
> > > > > > > > > english. I'll add many others, like strokes count, radical # and so on.
> > > > > > > > >
> > > > > > > > > Here it is, with the 3 options (it may seem overwhelming but is only
> > > > > > > > > because of the IF ELSE structure) :
> > > > > > > > >
> > > > > > > > > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > > > > > > > tinyint,
> > > > > > > > > @.search2 nvarchar(200) = '', @.detailed bit AS
> > > > > > > > > -- @.type : 1 = kanji
> > > > > > > > > -- 2 = english
> > > > > > > > > -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> > > > > > > > > / nanori)
> > > > > > > > > -- @.search2 is there in case we have to search by pronunciation, we
> > > > > > > > > must
> > > > > > > > > -- be able to search in hiragana and katakana. In that case,
> > > > > > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > > > > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > > > > > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > > > > > > > @.search2.
> > > > > > > > > BEGIN
> > > > > > > > > SET NOCOUNT ON
> > > > > > > > >
> > > > > > > > > -- return fields for detailed listing
> > > > > > > > > IF @.detailed = 1
> > > > > > > > > BEGIN
> > > > > > > > > IF @.type = 1
> > > > > > > > > BEGIN
> > > > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > > > SELECT kanji_id as id
> > > > > > > > > INTO #KanjiIdsByKanji
> > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > WHERE kanji_kanji = @.search
> > > > > > > > >
> > > > > > > > > -- select kanji fields
> > > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > > > lk_filename, lk_idlesson,
> > > > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > > > > > > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> > > > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > > > > > ASC
> > > > > > > > >
> > > > > > > > > ... [ * comment: I removed some other table select for the sake of
> > > > > > > > > the simplicity post ]
> > > > > > > > > END
> > > > > > > > > ELSE IF @.type = 2
> > > > > > > > > BEGIN
> > > > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > > > SELECT kanji_id as id
> > > > > > > > > INTO #KanjiIdsByEn
> > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > > > > > > >
> > > > > > > > > -- select kanji fields
> > > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > > > lk_filename, lk_idlesson,
> > > > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > > > > > > > > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id = id
> > > > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > > > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > > > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > > > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201)
> > > > > > > > > ASC
> > > > > > > > >
> > > > > > > > > ... [ * comment: I removed some other table select for the sake of
> > > > > > > > > the simplicity post ]
> > > > > > > > > END
> > > > > > > > > ELSE IF @.type = 3
> > > > > > > > > BEGIN
> > > > > > > > > -- select kanji ids corresponding to the search string
> > > > > > > > > SELECT kanji_id as id
> > > > > > > > > INTO #KanjiIdsByKana
> > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > WHERE FREETEXT (kanji_on, @.search)
> > > > > > > > > UNION ALL
> > > > > > > > > SELECT kanji_id as id
> > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > > > > > > UNION ALL
> > > > > > > > > SELECT kanji_id as id
> > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > WHERE FREETEXT (kanji_nanori, @.search2)
> > > > > > > > >
> > > > > > > > > -- select kanji fields
> > > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > > > > > > > > kanji_nanori, kanji_meaning,|||Hello again!
Thanks for the detailed explanations, now it is all clear, and I
appreciate it :)
> The second select statement seems to be very similar to each other of whi=ch
> there is the basic information and full information:
> SELECT kanji_kanji, kanji_meaning
> FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id =3D id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
> or
> SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> kanji_nanori, kanji_meaning,
> lk_filename, lk_idlesson,
> lesson_idlevel, lesson_idlesson,
> kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id =3D id
> LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
Yes, you were right, I wonder why I didn't realize and understand what
you meant sooner, I'm sorry! Now, my new query takes that in account by
putting these SELECT at the dispatcher level (and the detailed one now
has its own proc).
I still separate the two resultsets (basic and detailed) however,
because as you noted it, it is not a good idea to return more fields
than needed. Is it still good practice?
Here is my new query that allows multiple search conditions (and
works!). Can you give me some comments about it? I also chose dynamic
SQL over static for this one, as I wrote the same procedure, but with
static code, which was much slower (maybe it's the way I wrote it...I
can post code if you'd like)
ALTER PROCEDURE [dbo].[DicKanjiSearch3] (@.detailed bit, @.type tinyint,
@.search nvarchar(200) =3D '',
@.search2 nvarchar(200) =3D '', @.sc tinyint =3D 0, @.rad tinyint =3D 0, @.gra=de
tinyint =3D 0)
WITH RECOMPILE
AS
-- EXAMPLE SEARCH
-- Detailed resultset about kanji that has
-- a pronunciation of '=E3=82=A2' (or '=E3=81=82')
-- 11 strokes
-- a radical # of 31
--EXECUTE [DicKanjiSearch3] 1, 3, N'=E3=82=A2', N'=E3=81=82',11,31
-- EXAMPLE SEARCH
-- Basic resultset about kanji that has
-- 15 strokes
-- a radical # of 115
-- is learned in grade 8
--EXECUTE [DicKanjiSearch3] 0, 0, N'', N'',15,115,8
-- @.type determines the kind of text @.search contains:
-- 0 =3D no search by text
-- 1 =3D kanji
-- 2 =3D english
-- 3 =3D pronunciation (on'yomi / kun'yomi / nanori)
-- @.search : main text search query
-- @.search2 is there in case we have to search by pronunciation, we
must
-- be able to search in hiragana and katakana. In that case,
-- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
-- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
-- nanori (name) pronunciation is also in HIRAGANA ans therefore is
@.search2.
-- @.sc : if not 0, search by strokes count =3D @.sc
-- @.rad : if not 0, search by radical number =3D @.rad
-- @.grade : if not 0, search by grade level =3D @.grade
BEGIN
SET NOCOUNT ON
DECLARE @.debug bit
SET @.debug =3D 1
DECLARE @.sql nvarchar(4000)
DECLARE @.tables nvarchar(4000)
SET @.tables =3D N''
DECLARE @.where nvarchar(4000)
SET @.where =3D N'1 =3D 1'
DECLARE @.params nvarchar(4000)
SET @.params =3D N'@.search nvarchar(200), @.search2 nvarchar(200), @.sc
tinyint, @.rad tinyint, @.grade tinyint'
-- temp table to hold ids of the multiple searches
CREATE TABLE #Ids (id int PRIMARY KEY)
-- query by kanji
IF @.type =3D 1
BEGIN
SET @.where =3D @.where + N' AND kanji_kanji =3D @.search'
END
-- query by english
ELSE IF @.type =3D 2
SET @.where =3D @.where + N' AND FREETEXT(kanji_meaning, @.search)'
-- query by pronunciation
ELSE IF @.type =3D 3
SET @.where =3D @.where + N' AND (FREETEXT(kanji_on, @.search) OR
FREETEXT(kanji_kun, @.search2) OR
FREETEXT(kanji_nanori, @.search2))'
-- query by strokes count
IF @.sc <> 0
SET @.where =3D @.where + N' AND kanji_strokecount =3D @.sc'
-- query by radical
IF @.rad <> 0
BEGIN
SET @.where =3D @.where + N' AND krad_idradical =3D @.rad'
SET @.tables =3D ' INNER JOIN KanjiRads ON krad_idkanji =3D kanji_id'
END
-- query by grade
IF @.grade <> 0
SET @.where =3D @.where + N' AND kanji_grade =3D @.grade'
-- select kanji ids that satisfy all queries
SET @.sql =3D N'
INSERT INTO #Ids (id)
SELECT kanji_id
FROM Kanjis' + @.tables + '
WHERE ' + @.where
EXEC sp_executesql @.sql, @.params, @.search, @.search2, @.sc, @.rad, @.grade
IF @.debug =3D 1 PRINT @.sql
-- return fields for detailed listing
IF @.detailed =3D 1
EXEC DictKanjiGetDetailed
ELSE
-- select basic info
SELECT kanji_kanji, kanji_meaning
FROM Kanjis INNER JOIN #Ids ON kanji_id =3D id
ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
END
thanks again!
Bruno
John Bell wrote:
> Hi
> The second select statement seems to be very similar to each other of whi=ch
> there is the basic information and full information:
> SELECT kanji_kanji, kanji_meaning
> FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id =3D id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
> or
> SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> kanji_nanori, kanji_meaning,
> lk_filename, lk_idlesson,
> lesson_idlevel, lesson_idlesson,
> kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id =3D id
> LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji_id
> LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
> Rather than repeating this in multiple procedures they could be put at a
> level where the code would only be written once (helping maintainance). =If
> you do this then the procedures that populate the temporary tables can ju=st
> contain single select statement and you can use INSERT.. EXEC e.g.
> INSERT #Tmptable (id)
> EXECUTE [DicKanjiSearch] N'=E5=8F=8B', 1, N'', 0
> As the basic listing is a subset of the full listing, then you may want =to
> return the full listing result set each time and throw away the columns n=ot
> used to try and simplify the client code (although normally you would not
> want to return excessively wide result sets!).
> To get the 'AND' result you can do something like:
> CREATE TABLE #Tmptable (id int)
> INSERT #Tmptable (id)
> EXECUTE [DicKanjiSearch] N'=E5=8F=8B', 1, N'', 0
> INSERT #Tmptable (id)
> EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> -- And results will be have two entried one from each insert
> SELECT id
> FROM #Tmptable
> GROUP BY id
> HAVING COUNT(*) > 1
> I hope that is clearer?
> John
>
> "ibiza" wrote:
> > :S sorry I'd need examples to understand better what you mean.
> > I suppose 'the second select statement' you refer to are any select
> > after the
> >
> > SELECT kanji_id as id
> > INTO #TempTable
> > FROM dbo.Kanjis
> > WHERE (condition)
> >
> > into each sub-procedure? How come you say that it seems to be in two
> > flavours?
> >
> > thanks,
> > Bruno
> >
> > John Bell wrote:
> > > Hi
> > >
> > > The second select statement seems to be in two flavours based on Kanj=i and
> > > LessonKanji! If these are always exclusive then you may want to separ=ate
> > > these into two different procedures.
> > >
> > > To get an AND condition you can select the ids where there is more th=an one
> > > occurence using a group by id and a having clause, providing each ti=me you
> > > add the id you only insert distinct ones.
> > >
> > > John
> > >
> > > "ibiza" wrote:
> > >
> > > > Hello,
> > > >
> > > > > To implement multiple criteria you could separate your procedures= into two
> > > > > bring the second select into DicKanjiSearch, you can then call th=e various
> > > > > procedures to add new ids to the temporary table multiple times.
> > > >
> > > > I don't really understand. Separating which procedures into two?
> > > > Also, multiple criteria would mean a sort of AND implemantation, so
> > > > that does not mean "add new ids to the temporary table multiple tim=es"
> > > > (this would be an OR, am I correct?)
> > > >
> > > > thanks for your time
> > > >
> > > > John Bell wrote:
> > > > > Hi
> > > > >
> > > > > The only way to see if dynamic SQL is quicker would be to impleme=nt it. In
> > > > > general I would only do that if you think your current solution i=s too slow.
> > > > >
> > > > > To implement multiple criteria you could separate your procedures= into two
> > > > > bring the second select into DicKanjiSearch, you can then call th=e various
> > > > > procedures to add new ids to the temporary table multiple times.
> > > > >
> > > > > John
> > > > >
> > > > > "ibiza" wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > yes, in fact each of my temporary tables strictly contains an i=d=2E Each
> > > > > > IF branch (now splitted into sub-procedures) fills that temp ta=ble with
> > > > > > the ids related to the search, then do many select queries base=d on
> > > > > > those ids.
> > > > > >
> > > > > > I've change the SELECT INTO and splitted each IF into sub-proce=dures,
> > > > > > here it is now (with some commented example search at the begin=ning) :
> > > > > >
> > > > > > --EXECUTE [DicKanjiSearch] N'=E5=8F=8B', 1, N'', 0
> > > > > > --EXECUTE [DicKanjiSearch] N'=E5=8B=9D', 1, N'', 1
> > > > > > --EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> > > > > > --EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
> > > > > > --EXECUTE [DicKanjiSearch] N'=E3=82=B8=E3=83=A5=E3=82=A6', 3, N='=E3=81=98=E3=82=85=E3=81=86', 0
> > > > > > --EXECUTE [DicKanjiSearch] N'=E3=82=B3=E3=82=B3=E3=83=AD', 3, N='=E3=81=93=E3=81=93=E3=82=8D', 1
> > > > > > --EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
> > > > > > --EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
> > > > > > ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.=type
> > > > > > tinyint,
> > > > > >
> > > > > > @.search2 nvarchar(200) =3D '', @.detailed bit AS
> > > > > > -- @.type : 1 =3D kanji
> > > > > > -- 2 =3D english
> > > > > > -- 3 =3D hiragana or katakana : pronunciation (on'yomi =/ kun'yomi
> > > > > > / nanori)
> > > > > > -- @.search2 is there in case we have to search by pronunciation=, we
> > > > > > must
> > > > > > -- be able to search in hiragana and katakana. In that case,
> > > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.searc=h=2E
> > > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.sea=rch2.
> > > > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefor=e is
> > > > > > @.search2.
> > > > > > BEGIN
> > > > > > SET NOCOUNT ON
> > > > > >
> > > > > > -- temp table to hold ids
> > > > > > CREATE TABLE #TempTable (id int PRIMARY KEY)
> > > > > >
> > > > > > -- return fields for detailed listing
> > > > > > IF @.detailed =3D 1
> > > > > > BEGIN
> > > > > > IF @.type =3D 1
> > > > > > EXEC DictKanjiGetDetailedByKanji @.search
> > > > > > ELSE IF @.type =3D 2
> > > > > > EXEC DictKanjiGetDetailedByMeaning @.sea=rch
> > > > > > ELSE IF @.type =3D 3
> > > > > > EXEC DictKanjiGetDetailedByPron @.search,
> > > > > > @.search2
> > > > > > ELSE IF @.type =3D 4
> > > > > > This will be a new type : by stroke count
> > > > > > EXEC DictKanjiGetDetailedByStrokesCount
> > > > > > CAST(@.search as tinyint)
> > > > > > END
> > > > > > ELSE
> > > > > > -- return fields for basic listing
> > > > > > BEGIN
> > > > > > IF @.type =3D 1
> > > > > > EXEC DictKanjiGetBasicByKanji @.search
> > > > > > ELSE IF @.type =3D 2
> > > > > > EXEC DictKanjiGetBasicByMeaning @.search
> > > > > > ELSE IF @.type =3D 3
> > > > > > EXEC DictKanjiGetBasicByPron @.search, @.=search2
> > > > > > ELSE IF @.type =3D 4
> > > > > > This will be a new type : by stroke count
> > > > > > EXEC DictKanjiGetBasicByStrokesCount CAST=(@.search
> > > > > > as tinyint)
> > > > > > END
> > > > > > END
> > > > > >
> > > > > > Now, after reading your two excellent articles (many thanks for= that),
> > > > > > it seems that, I quote, "dynamic SQL is often the best solution=, both
> > > > > > for performance and maintainability"...Now I have a concern wit=h the
> > > > > > method I am using, because it's static SQL :| what do you think= about
> > > > > > that?
> > > > > >
> > > > > > Finally, is there a way to 'merge' queries together with the me=thod I
> > > > > > am currently using? That's because I added the search type 4, w=hich
> > > > > > is by kanji strokes count, I'd like to be able to search for by= more
> > > > > > than one search type. (e.g., strokes count AND english term).
> > > > > > I see two ways of passing multiple parameters here. Either
> > > > > > 1) have a @.parameter_name for each type possible (e.g.: @.meanin=g =3D
> > > > > > NULL, @.kanji =3D NULL, @.pronH =3D NULL, @.pronK =3D NULL, @.strok=escount =3D
> > > > > > NULL, ...), then check which ones are assigned, then execute
> > > > > > corresponding queries.
> > > > > > 2) use my @.search paramater as a container, like "7&name", and =have my
> > > > > > @.type parameter something like "strokecount&meaning" (or the mo=re
> > > > > > shorter "sc&en") and parse then to execute corresponding querie=s=2E
> > > > > > Which one would perfrom faster?
> > > > > >
> > > > > > Then I still need a way to merge SELECT tables together...Any i=deas?
> > > > > >
> > > > > > Thank you very much!
> > > > > >
> > > > > > John Bell wrote:
> > > > > > > Hi
> > > > > > >
> > > > > > > "ibiza" wrote:
> > > > > > >
> > > > > > > > Hi, thanks for your reply.
> > > > > > > >
> > > > > > > > By the way, you gave me the two same links, is that a typo?
> > > > > > > Yes it was a cut and paste issue! Try
> > > > > > > http://www.sommarskog.se/dynamic_sql.html
> > > > > > >
> > > > > > > >
> > > > > > > > > It may be possible to rationalise the use of the differen=t temporary tables
> > > > > > > > > into one, in which case your second select might possibly= be made the same or
> > > > > > > > > more similar and not produce different result sets for ea=ch combination,
> > > > > > > > > which may clean up the client code.
> > > > > > > >
> > > > > > > > How would you do that? I have trouble to get it :S
> > > > > > > Looking at the code you seem to have very similar temporary t=ables with
> > > > > > > different name. Without looking in depth it struck me that if= you just
> > > > > > > created a single table at the begining you may be able to se=e more
> > > > > > > rationalisation. I would have expected the current client to =have similar
> > > > > > > code to the stored procedure as you seem to be producing uniq=ue output for
> > > > > > > each one. This could probably be cleaned up if you had the sa=me column names
> > > > > > > for everything. If you took this further and always returned =the same columns
> > > > > > > then you may be able to clean up the client coding further, t=he downside may
> > > > > > > be that you produce a wider result set, you may need to judge= the performance
> > > > > > > impact of that.
> > > > > > >
> > > > > > >
> > > > > > > John
> > > > > > > > thanks for your time
> > > > > > > >
> > > > > > > > John Bell wrote:
> > > > > > > > > Hi
> > > > > > > > >
> > > > > > > > > Check out http://www.sommarskog.se/dyn-search.html and
> > > > > > > > > http://www.sommarskog.se/dyn-search.html.
> > > > > > > > >
> > > > > > > > > It may be possible to rationalise the use of the differen=t temporary tables
> > > > > > > > > into one, in which case your second select might possibly= be made the same or
> > > > > > > > > more similar and not produce different result sets for ea=ch combination,
> > > > > > > > > which may clean up the client code.
> > > > > > > > >
> > > > > > > > > You may also want to look at splitting the different sect=ions into their own
> > > > > > > > > stored procedures.
> > > > > > > > >
> > > > > > > > > John
> > > > > > > > >
> > > > > > > > > "ibiza" wrote:
> > > > > > > > >
> > > > > > > > > > Hi,
> > > > > > > > > >
> > > > > > > > > > I have a stored proc intented to search a table on diff=erent fields,
> > > > > > > > > > depending on the search type.
> > > > > > > > > >
> > > > > > > > > > Up until now, I have implemented 3 options, which is by= kanji, kana and
> > > > > > > > > > english. I'll add many others, like strokes count, radi=cal # and so on.
> > > > > > > > > >
> > > > > > > > > > Here it is, with the 3 options (it may seem overwhelmin=g but is only
> > > > > > > > > > because of the IF ELSE structure) :
> > > > > > > > > >
> > > > > > > > > > ... PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(=200), @.type
> > > > > > > > > > tinyint,
> > > > > > > > > > @.search2 nvarchar(200) =3D '', @.detailed bit =AS
> > > > > > > > > > -- @.type : 1 =3D kanji
> > > > > > > > > > -- 2 =3D english
> > > > > > > > > > -- 3 =3D hiragana or katakana : pronunciation (=on'yomi / kun'yomi
> > > > > > > > > > / nanori)
> > > > > > > > > > -- @.search2 is there in case we have to search by pronu=nciation, we
> > > > > > > > > > must
> > > > > > > > > > -- be able to search in hiragana and katakana. In that =case,
> > > > > > > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans i=s @.search.
> > > > > > > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans= is @.search2.
> > > > > > > > > > -- nanori (name) pronunciation is also in HIRAGANA ans =therefore is
> > > > > > > > > > @.search2.
> > > > > > > > > > BEGIN
> > > > > > > > > > SET NOCOUNT ON
> > > > > > > > > >
> > > > > > > > > > -- return fields for detailed listing
> > > > > > > > > > IF @.detailed =3D 1
> > > > > > > > > > BEGIN
> > > > > > > > > > IF @.type =3D 1
> > > > > > > > > > BEGIN
> > > > > > > > > > -- select kanji ids corresponding to the search stri=ng
> > > > > > > > > > SELECT kanji_id as id
> > > > > > > > > > INTO #KanjiIdsByKanji
> > > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > > WHERE kanji_kanji =3D @.search
> > > > > > > > > >
> > > > > > > > > > -- select kanji fields
> > > > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on,= kanji_kun,
> > > > > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > > > > lk_filename, lk_idlesson,
> > > > > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, =kanji_freq
> > > > > > > > > > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id ==3D id
> > > > > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji=_id
> > > > > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9=999) ASC,
> > > > > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_m=eaning), 201)
> > > > > > > > > > ASC
> > > > > > > > > >
> > > > > > > > > > ... [ * comment: I removed some other table select f=or the sake of
> > > > > > > > > > the simplicity post ]
> > > > > > > > > > END
> > > > > > > > > > ELSE IF @.type =3D 2
> > > > > > > > > > BEGIN
> > > > > > > > > > -- select kanji ids corresponding to the search stri=ng
> > > > > > > > > > SELECT kanji_id as id
> > > > > > > > > > INTO #KanjiIdsByEn
> > > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > > WHERE FREETEXT (kanji_meaning, @.search)
> > > > > > > > > >
> > > > > > > > > > -- select kanji fields
> > > > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on,= kanji_kun,
> > > > > > > > > > kanji_nanori, kanji_meaning,
> > > > > > > > > > lk_filename, lk_idlesson,
> > > > > > > > > > lesson_idlevel, lesson_idlesson,
> > > > > > > > > > kanji_unicode, kanji_grade, kanji_strokemiscounts, =kanji_freq
> > > > > > > > > > FROM #KanjiIdsByEn INNER JOIN Kanjis ON kanji_id =3D= id
> > > > > > > > > > LEFT OUTER JOIN LessonKanji ON lk_idkanji =3D kanji=_id
> > > > > > > > > > LEFT OUTER JOIN Lessons ON lk_idlesson =3D lesson_id
> > > > > > > > > > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9=999) ASC,
> > > > > > > > > > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_m=eaning), 201)
> > > > > > > > > > ASC
> > > > > > > > > >
> > > > > > > > > > ... [ * comment: I removed some other table select f=or the sake of
> > > > > > > > > > the simplicity post ]
> > > > > > > > > > END
> > > > > > > > > > ELSE IF @.type =3D 3
> > > > > > > > > > BEGIN
> > > > > > > > > > -- select kanji ids corresponding to the search stri=ng
> > > > > > > > > > SELECT kanji_id as id
> > > > > > > > > > INTO #KanjiIdsByKana
> > > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > > WHERE FREETEXT (kanji_on, @.search)
> > > > > > > > > > UNION ALL
> > > > > > > > > > SELECT kanji_id as id
> > > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > > WHERE FREETEXT (kanji_kun, @.search2)
> > > > > > > > > > UNION ALL
> > > > > > > > > > SELECT kanji_id as id
> > > > > > > > > > FROM dbo.Kanjis
> > > > > > > > > > WHERE FREETEXT (kanji_nanori, @.search2)
> > > > > > > > > >
> > > > > > > > > > -- select kanji fields
> > > > > > > > > > SELECT id, kanji_kanji, kanji_strokecount, kanji_on,= kanji_kun,
> > > > > > > > > > kanji_nanori, kanji_meaning,|||Hi
You should add owner/schema prefixes to the tables and stored procedures e.g
SET @.tables = ' INNER JOIN dbo.KanjiRads ON krad_idkanji = kanji_id'
It seems that you are differentiating columns in different tables be calling
them differently. If you have a kanj_id in different table give it the same
name, to differential them in a query use a table alias e.g.
SET @.tables = ' INNER JOIN dbo.KanjiRads r ON r.kanji_id = k.kanji_id'
r is the table alias for KanjiRads and k would be the table alias for Kanjis.
Add some error checking see http://www.sommarskog.se/error-handling-II.html
and http://www.sommarskog.se/error-handling-I.html
I would put the "-- select basic info" query into a different procedure.
Query Analyser has it's own debugging facilities (right click the stored
procedure in the object browser and choose debug), using this would mean that
you can leave you do not have to change live code to debug it. Currently you
will need to change the @.debug value to stop the statement being displayed.
John
"ibiza" wrote:
> Hello again!
> Thanks for the detailed explanations, now it is all clear, and I
> appreciate it :)
> > The second select statement seems to be very similar to each other of which
> > there is the basic information and full information:
> >
> > SELECT kanji_kanji, kanji_meaning
> > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
> >
> > or
> >
> > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > kanji_nanori, kanji_meaning,
> > lk_filename, lk_idlesson,
> > lesson_idlevel, lesson_idlesson,
> > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
> Yes, you were right, I wonder why I didn't realize and understand what
> you meant sooner, I'm sorry! Now, my new query takes that in account by
> putting these SELECT at the dispatcher level (and the detailed one now
> has its own proc).
> I still separate the two resultsets (basic and detailed) however,
> because as you noted it, it is not a good idea to return more fields
> than needed. Is it still good practice?
> Here is my new query that allows multiple search conditions (and
> works!). Can you give me some comments about it? I also chose dynamic
> SQL over static for this one, as I wrote the same procedure, but with
> static code, which was much slower (maybe it's the way I wrote it...I
> can post code if you'd like)
> ALTER PROCEDURE [dbo].[DicKanjiSearch3] (@.detailed bit, @.type tinyint,
> @.search nvarchar(200) = '',
> @.search2 nvarchar(200) = '', @.sc tinyint = 0, @.rad tinyint = 0, @.grade
> tinyint = 0)
> WITH RECOMPILE
> AS
> -- EXAMPLE SEARCH
> -- Detailed resultset about kanji that has
> -- a pronunciation of 'ã?¢' (or 'ã'')
> -- 11 strokes
> -- a radical # of 31
> --EXECUTE [DicKanjiSearch3] 1, 3, N'ã?¢', N'ã'',11,31
> -- EXAMPLE SEARCH
> -- Basic resultset about kanji that has
> -- 15 strokes
> -- a radical # of 115
> -- is learned in grade 8
> --EXECUTE [DicKanjiSearch3] 0, 0, N'', N'',15,115,8
> -- @.type determines the kind of text @.search contains:
> -- 0 = no search by text
> -- 1 = kanji
> -- 2 = english
> -- 3 = pronunciation (on'yomi / kun'yomi / nanori)
> -- @.search : main text search query
> -- @.search2 is there in case we have to search by pronunciation, we
> must
> -- be able to search in hiragana and katakana. In that case,
> -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> @.search2.
> -- @.sc : if not 0, search by strokes count = @.sc
> -- @.rad : if not 0, search by radical number = @.rad
> -- @.grade : if not 0, search by grade level = @.grade
> BEGIN
> SET NOCOUNT ON
> DECLARE @.debug bit
> SET @.debug = 1
> DECLARE @.sql nvarchar(4000)
> DECLARE @.tables nvarchar(4000)
> SET @.tables = N''
> DECLARE @.where nvarchar(4000)
> SET @.where = N'1 = 1'
> DECLARE @.params nvarchar(4000)
> SET @.params = N'@.search nvarchar(200), @.search2 nvarchar(200), @.sc
> tinyint, @.rad tinyint, @.grade tinyint'
> -- temp table to hold ids of the multiple searches
> CREATE TABLE #Ids (id int PRIMARY KEY)
> -- query by kanji
> IF @.type = 1
> BEGIN
> SET @.where = @.where + N' AND kanji_kanji = @.search'
> END
> -- query by english
> ELSE IF @.type = 2
> SET @.where = @.where + N' AND FREETEXT(kanji_meaning, @.search)'
> -- query by pronunciation
> ELSE IF @.type = 3
> SET @.where = @.where + N' AND (FREETEXT(kanji_on, @.search) OR
> FREETEXT(kanji_kun, @.search2) OR
> FREETEXT(kanji_nanori, @.search2))'
> -- query by strokes count
> IF @.sc <> 0
> SET @.where = @.where + N' AND kanji_strokecount = @.sc'
> -- query by radical
> IF @.rad <> 0
> BEGIN
> SET @.where = @.where + N' AND krad_idradical = @.rad'
> SET @.tables = ' INNER JOIN KanjiRads ON krad_idkanji = kanji_id'
> END
> -- query by grade
> IF @.grade <> 0
> SET @.where = @.where + N' AND kanji_grade = @.grade'
> -- select kanji ids that satisfy all queries
> SET @.sql = N'
> INSERT INTO #Ids (id)
> SELECT kanji_id
> FROM Kanjis' + @.tables + '
> WHERE ' + @.where
> EXEC sp_executesql @.sql, @.params, @.search, @.search2, @.sc, @.rad, @.grade
> IF @.debug = 1 PRINT @.sql
> -- return fields for detailed listing
> IF @.detailed = 1
> EXEC DictKanjiGetDetailed
> ELSE
> -- select basic info
> SELECT kanji_kanji, kanji_meaning
> FROM Kanjis INNER JOIN #Ids ON kanji_id = id
> ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
> END
> thanks again!
> Bruno
> John Bell wrote:
> > Hi
> >
> > The second select statement seems to be very similar to each other of which
> > there is the basic information and full information:
> >
> > SELECT kanji_kanji, kanji_meaning
> > FROM Kanjis INNER JOIN #BasicKanjiIdsByKanji ON kanji_id = id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
> >
> > or
> >
> > SELECT id, kanji_kanji, kanji_strokecount, kanji_on, kanji_kun,
> > kanji_nanori, kanji_meaning,
> > lk_filename, lk_idlesson,
> > lesson_idlevel, lesson_idlesson,
> > kanji_unicode, kanji_grade, kanji_strokemiscounts, kanji_freq
> > FROM #KanjiIdsByKanji INNER JOIN Kanjis ON kanji_id = id
> > LEFT OUTER JOIN LessonKanji ON lk_idkanji = kanji_id
> > LEFT OUTER JOIN Lessons ON lk_idlesson = lesson_id
> > ORDER BY kanji_isjouyou DESC, COALESCE(kanji_freq, 9999) ASC,
> > COALESCE(kanji_grade, 99) ASC, COALESCE(LEN(kanji_meaning), 201) ASC
> >
> > Rather than repeating this in multiple procedures they could be put at a
> > level where the code would only be written once (helping maintainance). If
> > you do this then the procedures that populate the temporary tables can just
> > contain single select statement and you can use INSERT.. EXEC e.g.
> >
> > INSERT #Tmptable (id)
> > EXECUTE [DicKanjiSearch] N'å'', 1, N'', 0
> >
> > As the basic listing is a subset of the full listing, then you may want to
> > return the full listing result set each time and throw away the columns not
> > used to try and simplify the client code (although normally you would not
> > want to return excessively wide result sets!).
> >
> > To get the 'AND' result you can do something like:
> >
> > CREATE TABLE #Tmptable (id int)
> >
> > INSERT #Tmptable (id)
> > EXECUTE [DicKanjiSearch] N'å'', 1, N'', 0
> >
> > INSERT #Tmptable (id)
> > EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> >
> > -- And results will be have two entried one from each insert
> > SELECT id
> > FROM #Tmptable
> > GROUP BY id
> > HAVING COUNT(*) > 1
> >
> > I hope that is clearer?
> >
> > John
> >
> >
> > "ibiza" wrote:
> >
> > > :S sorry I'd need examples to understand better what you mean.
> > > I suppose 'the second select statement' you refer to are any select
> > > after the
> > >
> > > SELECT kanji_id as id
> > > INTO #TempTable
> > > FROM dbo.Kanjis
> > > WHERE (condition)
> > >
> > > into each sub-procedure? How come you say that it seems to be in two
> > > flavours?
> > >
> > > thanks,
> > > Bruno
> > >
> > > John Bell wrote:
> > > > Hi
> > > >
> > > > The second select statement seems to be in two flavours based on Kanji and
> > > > LessonKanji! If these are always exclusive then you may want to separate
> > > > these into two different procedures.
> > > >
> > > > To get an AND condition you can select the ids where there is more than one
> > > > occurence using a group by id and a having clause, providing each time you
> > > > add the id you only insert distinct ones.
> > > >
> > > > John
> > > >
> > > > "ibiza" wrote:
> > > >
> > > > > Hello,
> > > > >
> > > > > > To implement multiple criteria you could separate your procedures into two
> > > > > > bring the second select into DicKanjiSearch, you can then call the various
> > > > > > procedures to add new ids to the temporary table multiple times.
> > > > >
> > > > > I don't really understand. Separating which procedures into two?
> > > > > Also, multiple criteria would mean a sort of AND implemantation, so
> > > > > that does not mean "add new ids to the temporary table multiple times"
> > > > > (this would be an OR, am I correct?)
> > > > >
> > > > > thanks for your time
> > > > >
> > > > > John Bell wrote:
> > > > > > Hi
> > > > > >
> > > > > > The only way to see if dynamic SQL is quicker would be to implement it. In
> > > > > > general I would only do that if you think your current solution is too slow.
> > > > > >
> > > > > > To implement multiple criteria you could separate your procedures into two
> > > > > > bring the second select into DicKanjiSearch, you can then call the various
> > > > > > procedures to add new ids to the temporary table multiple times.
> > > > > >
> > > > > > John
> > > > > >
> > > > > > "ibiza" wrote:
> > > > > >
> > > > > > > Hi,
> > > > > > >
> > > > > > > yes, in fact each of my temporary tables strictly contains an id. Each
> > > > > > > IF branch (now splitted into sub-procedures) fills that temp table with
> > > > > > > the ids related to the search, then do many select queries based on
> > > > > > > those ids.
> > > > > > >
> > > > > > > I've change the SELECT INTO and splitted each IF into sub-procedures,
> > > > > > > here it is now (with some commented example search at the beginning) :
> > > > > > >
> > > > > > > --EXECUTE [DicKanjiSearch] N'å'', 1, N'', 0
> > > > > > > --EXECUTE [DicKanjiSearch] N'å'', 1, N'', 1
> > > > > > > --EXECUTE [DicKanjiSearch] N'test', 2, N'', 0
> > > > > > > --EXECUTE [DicKanjiSearch] N'sun', 2, N'', 1
> > > > > > > --EXECUTE [DicKanjiSearch] N'ã?¸ã?¥ã?¦', 3, N'ã'ã'ã'', 0
> > > > > > > --EXECUTE [DicKanjiSearch] N'ã?³ã?³ã?', 3, N'ã'ã'ã'', 1
> > > > > > > --EXECUTE [DicKanjiSearch] N'3', 4, N'', 0
> > > > > > > --EXECUTE [DicKanjiSearch] N'5', 4, N'', 1
> > > > > > > ALTER PROCEDURE [dbo].[DicKanjiSearch] @.search nvarchar(200), @.type
> > > > > > > tinyint,
> > > > > > >
> > > > > > > @.search2 nvarchar(200) = '', @.detailed bit AS
> > > > > > > -- @.type : 1 = kanji
> > > > > > > -- 2 = english
> > > > > > > -- 3 = hiragana or katakana : pronunciation (on'yomi / kun'yomi
> > > > > > > / nanori)
> > > > > > > -- @.search2 is there in case we have to search by pronunciation, we
> > > > > > > must
> > > > > > > -- be able to search in hiragana and katakana. In that case,
> > > > > > > -- on'yomi (chinese) pronunciation is in KATAKANA ans is @.search.
> > > > > > > -- kun'yomi (japanese) pronunciation is in HIRAGANA ans is @.search2.
> > > > > > > -- nanori (name) pronunciation is also in HIRAGANA ans therefore is
> > > > > > > @.search2.
> > > > > > > BEGIN
> > > > > > > SET NOCOUNT ON
> > > > > > >
> > > > > > > -- temp table to hold ids