Showing posts with label SQL Server 2005. Show all posts
Showing posts with label SQL Server 2005. Show all posts

20100129

Processing QBXML In SQL Server

I've been working with QuickBooks again, and this time I need to parse some QBXML in the database so I can do some data syncronization on a schedule.

SQL Server 2005+ has some nice XML tools that make things a lot easier.  I was having trouble getting things to show up, but eventually found some pretty basic examples.

I'm parsing an Invoice Query response.

Here is what I came up with:
-- variable to hold document reference
DECLARE @docHandle int

-- variable to hold actual XML
DECLARE @xmlDocument nvarchar(max) -- or xml type

-- Fill XML variable
SELECT TOP 1 @xmlDocument = QBXML FROM QBBridge ORDER BY QBBridgeID DESC

-- Use built in Procedure to process the XML into Data
EXEC sp_xml_preparedocument @docHandle OUTPUT, @xmlDocument;

    DECLARE @ParentID int
    DECLARE qbXMLCursor CURSOR FOR
        -- Get All InvoiceRet Objects.
        SELECT ID FROM OPENXML(@docHandle, N'/QBXML/QBXMLMsgsRs') WHERE NodeType = 1 AND LocalName = 'InvoiceRet'

    -- cycle through InvoiceRets and compile Sub Data.
    OPEN qbXMLCursor
    FETCH NEXT FROM qbXMLCursor INTO @ParentID

   
    WHILE @@FETCH_STATUS = 0
    BEGIN

        -- Use Common Table Expression to compile values to names of parent nodes
        -- I named my table columns <ParentName>.<ParentName>.<Name> so I'm sitting pretty for an insert from this.
        WITH InvoiceValues ([LocalName], [Value], [Level], [ID]) AS
        (
            SELECT    LocalName,
                    [text],
                    1 AS [Level],
                    [ID]
            FROM    OPENXML(@docHandle, N'/QBXML/QBXMLMsgsRs')
            WHERE ParentID = @ParentID
            UNION ALL
            SELECT    CASE WHEN SubValues.LocalName = '#text' THEN
                        InvoiceValues.LocalName
                    ELSE
                     InvoiceValues.LocalName + '.' + SubValues.LocalName
                     END As LocalName,
                    [text],
                    InvoiceValues.[Level] + 1 AS [Level],
                    SubValues.[ID]
            FROM    InvoiceValues
                    INNER JOIN OPENXML(@docHandle, N'/QBXML/QBXMLMsgsRs') As SubValues
                    ON InvoiceValues.ID = SubValues.ParentID
        )
        SELECT    [LocalName], [Value]
        FROM    InvoiceValues
        WHERE Value IS NOT NULL
           
        FETCH NEXT FROM qbXMLCursor INTO @ParentID
    END
    CLOSE qbXMLCursor
    DEALLOCATE qbXMLCursor

EXEC sp_xml_removedocument @docHandle


20091030

SQL 2005 FTS Thesaurus Doesn't Allow Duplicate Terms PROOF!

Was trying to find a way to use '&' and 'and' as expansions for my SQL 2005 FTS Thesaurus, and found this gem:
Because the thesaurus file already contains the term "Windows," the thesaurus file now contains duplicate terms. Therefore, the full-text thesaurus component does not load the thesaurus file.
From: http://support.microsoft.com/kb/923317

Again, would have been nice to know three Days ago.


20091029

SQL 2005 FTS Thesaurus Difficulties

Had my list of names all setup, dropped it into the file, it was all formatted correctly, then no test searches used of the expansions.  Pared the file down to just the original test I was working with, got it to work again, added another group of names, nothing.  went through and found some duplicate lines, removed the duplicates, got two sets of expansions to work, so I think if I remove all duplicates from my file it will work.  I can understand not allowing duplicates in the file, I just wish it would have let me know that was the problem.

As far as I can tell:  DUPLICATES ARE NOT ALLOWED IN THE THESAURUS FILE

UPDATE: Duplicates are not Allowed, see SQL 2005 FTS Thesaurus Doesn't Allow Duplicate Terms PROOF!

20091028

SQL Server 2005 Full Text Search FTS Thesaurus tsENU.xml issues

Been spending time reading up on SQL Server 2005 FTS Thesaurus files here is what I've learned:
  1. You have to make your own Thesaurus file, can't seem to find one with common expansions.
  2. When you have finished your new file, you need to restart SQL 2005 (if it's 2008 there's a stored procedure to reload it "EXEC sys.sp_fulltext_load_thesaurus_file 1033;" 1033 for the language in this case ENU (US English).
  3. Make sure your search terms are not surrounded by double quotes because you thought they were necessary from a previous hacked together project using SQL FTS.
  4. Run tests and query comparisons to make sure it's working the way you think it should.
  5. Wonder about all the other predicates you can add to your search terms because you can't find a definitive list. 
    FREETEXT(<FieldNames>,formsof(thesaurus,<searchterms>))
    FREETEXT(<FieldNames>,'"<SearchTerm_1>" NEAR "<SearchTerm_2>"')
    FREETEXT(<FieldNames>,'isabout("<SearchTerm_1>" weight(<DecimalWeightValue_1>), <SearchTerm_2> weight(DecimalWeightValue_2>))')
    FREETEXT(<FieldNames>, '<SearchTerm>', LANGUAGE <LanguageCode>)    [for a list of language codes "select [name], alias, lcid from master.sys.syslanguages", use the lcid field]
  6. If you're having problems getting the Thesaurus to appear to work, try using these to look at language settings
    exec sp_configure 'default language'
    SELECT @@language, @@langid
    select [name], alias, lcid from master.sys.syslanguages
  7. You may end up with lots of web pages open, maybe some of them are actually helpful:
    http://www.mssqltips.com/tip.asp?tip=1491
    http://msdn.microsoft.com/en-us/library/ms345187.aspx
    http://msdn.microsoft.com/en-us/library/ms345186.aspx
    http://www.mssqltips.com/tip.asp?tip=1353
    http://www.mssqltips.com/tip.asp?tip=1342
    http://www.mssqltips.com/tip.asp?tip=1332
    http://www.ureader.com/msg/1147186.aspx
    http://msdn.microsoft.com/en-us/library/cc280598.aspx
    http://msdn.microsoft.com/en-us/library/ms176076.aspx
    http://blogs.geekdojo.net/richard/archive/2006/09/01/13805.aspx  (this one is funny as well, which can be nice after not finding anything that seems useful)
    http://www.eggheadcafe.com/community/aspnet/13/10024815/need-help-on-sql-server-2.aspx  (this one seems useful until you realize that there are no replies with answers)
    http://arcanecode.com/2008/05/28/creating-custom-thesaurus-entries-in-sql-server-2005-and-2008-full-text-search/
    http://arcanecode.com/2008/04/29/sql-server-full-text-search-the-fulltextcatalogproperty-function/
    http://www.simple-talk.com/sql/learn-sql-server/sql-server-full-text-search-language-features/
  8. You finally get a sample working, then you realize you need to build a full list of all the Expansions and Substitutions you want.










20091023

SQL Full Text Search FTS And Wildcards

Looks like SQL FTS doesn't allow wildcards at the beginning of a search term. I ran into this a few days ago while trying to get FTS to work for a project, didn't understand why it wasn't working, but here is the answer from Microsoft(R) SQL Server 2005 Unleashed:
"However, SQL Server FTS does not allow a wildcard at the beginning of a word; for these types of prefix-based searches, you still have to use a LIKE clause."
Knowing this 4 days ago would have been extremely beneficial.