Results 1 to 5 of 5

Thread: SQL Question Statement "With"

  1. #1

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    830

    SQL Question Statement "With"

    Good Afternoon Guys

    i have the Following Query

    Code:
    SELECT * 
    INTO #Rows
    FROM OPENXML ( @doc , '/Timetable/Rows' , 2) 
    WITH ( 	ID int,
    	Descr varchar(64),
    	StartTime smalldatetime,
    	Duration int,
    	Offs varchar(64)
    )


    I understand the First Part

    Code:
    SELECT * INTO #Rows FROM OPENXML ( @doc , '/Timetable/Rows' , 2)
    but i dont understand Why With ? can someone Explain what is With Doig with Columns

    Thank you
    Last edited by vuyiswamb; Dec 12th, 2008 at 06:42 AM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Question Statement "With"

    My guess would be that's the table definition of the #ROWS temp table - right?

    Did you pre-create that table prior to executing that statement?

    Where did you see this statement - was it in MSDN documentation?

    This is MS SQL Server - right?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: SQL Question Statement "With"

    In T-SQL the WITH statement is used to create CTEs (Common Table Expressions) allowing you to access a temporary named resultset.

    I believe the WITH statement is only available on SQL Server 2005+.

    MSDN article on CTEs: WITH common_table_expression (Transact-SQL)

    Another MSDN article: Using Common Table Expressions

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Question Statement "With"

    Welcome to the forum!

    Your second link states the syntax of a CTE is

    The basic syntax structure for a CTE is:

    WITH expression_name [ ( column_name [,...n] ) ]

    AS

    ( CTE_query_definition )
    The example posted by the OP doesn't have that format - it's not a NAMED expression...

    The WITH keyword is used in many places in T-SQL - in this case it's supplying METADATA to the OPENXML.

    http://msdn.microsoft.com/en-us/libr...8(SQL.90).aspx

    OPENXML( idoc int [ in] , rowpattern nvarchar [ in ] , [ flags byte [ in ] ] )
    [ WITH ( SchemaDeclaration | TableName ) ]
    WITH is optional with OPENXML - from that same link above

    The WITH clause provides a rowset format (and additional mapping information as required) by using either SchemaDeclaration or specifying an existing TableName. If the optional WITH clause is not specified, the results are returned in an edge table format. Edge tables represent the fine-grained XML document structure (such as element/attribute names, the document hierarchy, the namespaces, PIs, and son on) in a single table.
    That's why I asked the OP where they found this syntax - because the doc at MSDN is already pretty clear on it's use and purpose related to OPENXML

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: SQL Question Statement "With"

    Quote Originally Posted by szlamany
    The WITH keyword is used in many places in T-SQL - in this case it's supplying METADATA to the OPENXML.
    Oops my brain completely missed the OPENXML in the initial post. Yes, the WITH statement provides the document structure for OPENXML in this case.

    Thank you for the correction.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width